Skip to content

Commit 525435b

Browse files
authored
[flutter_tools] Only pass on a valid root tool in DASH__TOOL (flutter#196228)
When flutter_tools starts a sub-process, it propagates DASH__TOOL so the sub-tool's analytics can attribute events to the user's entry point (the root tool). Until now it passed the parent's value through unchanged, even when that value was an unknown label or a shared component such as analysis-server or dart-mcp-server, which can never be a root tool. Since package:unified_analytics 8.4.0 (rolled in flutter#195950), the tool itself ignores such values and reports root_tool = flutter-tool, but its children would still receive the invalid value and disagree about the root tool. This change only passes on a DASH__TOOL that names a tool that can be a root tool. It prefers the parent's value, then a caller-supplied value, and otherwise uses flutter-tool. This matches how package:unified_analytics resolves the root tool. Valid values are passed through exactly as before. Fixes flutter#194983
1 parent 42cd7ee commit 525435b

2 files changed

Lines changed: 93 additions & 5 deletions

File tree

‎packages/flutter_tools/lib/src/base/error_handling_io.dart‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,14 +1251,30 @@ class ErrorHandlingProcessManager extends ProcessManager {
12511251
environmentResult[suppressKey] = (!analytics.okToSend).toString();
12521252
}
12531253

1254-
// Propagate the parent tool identifier down to the spawned process.
1255-
final String? parentTool = _platform.environment[DashEnvVar.tool.name];
1254+
// Propagate the root tool identifier down to the spawned process.
1255+
//
1256+
// This matches how `package:unified_analytics` resolves the root tool for
1257+
// this process (`DASH__TOOL` if it names a tool that can be a root tool,
1258+
// otherwise this tool), so the sub-tool reports the same root tool as this
1259+
// process. An invalid label or a shared component (for example
1260+
// `analysis-server`) is never passed on, and a caller-supplied value is
1261+
// ignored.
12561262
environmentResult[DashEnvVar.tool.name] =
1257-
parentTool ?? environmentResult[DashEnvVar.tool.name] ?? DashTool.flutterTool.label;
1263+
_asRootToolLabel(_platform.environment[DashEnvVar.tool.name]) ?? DashTool.flutterTool.label;
12581264

12591265
return environmentResult;
12601266
}
12611267

1268+
/// Returns [label] if it names a [DashTool] that can be a root tool, or
1269+
/// `null` otherwise.
1270+
static String? _asRootToolLabel(String? label) {
1271+
if (label == null) {
1272+
return null;
1273+
}
1274+
final DashTool? tool = DashTool.values.where((DashTool t) => t.label == label).firstOrNull;
1275+
return (tool?.canBeRootTool ?? false) ? label : null;
1276+
}
1277+
12621278
@override
12631279
Future<io.ProcessResult> run(
12641280
List<Object> command, {

‎packages/flutter_tools/test/general.shard/base/process_test.dart‎

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ void main() {
534534

535535
const expectedEnvWithParentTool = <String, String>{
536536
'DASH__SUPPRESS_ANALYTICS': 'true',
537-
'DASH__TOOL': 'parent-tool',
537+
'DASH__TOOL': 'vscode-plugins',
538538
};
539539

540540
const expectedEnvNotSuppressed = <String, String>{
@@ -600,7 +600,7 @@ void main() {
600600
'preserves parent DASH__TOOL if already specified in the environment',
601601
() async {
602602
final ProcessUtils localProcessUtils = createProcessUtils(
603-
platformEnvironment: const <String, String>{'DASH__TOOL': 'parent-tool'},
603+
platformEnvironment: const <String, String>{'DASH__TOOL': 'vscode-plugins'},
604604
);
605605

606606
fakeProcessManager.addCommand(
@@ -614,6 +614,78 @@ void main() {
614614
},
615615
);
616616

617+
for (final invalidTool in <String>['parent-tool', 'analysis-server']) {
618+
testWithoutContext(
619+
'does not pass on a parent DASH__TOOL of "$invalidTool", which cannot be a root tool',
620+
() async {
621+
final ProcessUtils localProcessUtils = createProcessUtils(
622+
platformEnvironment: <String, String>{'DASH__TOOL': invalidTool},
623+
);
624+
625+
fakeProcessManager.addCommand(
626+
const FakeCommand(
627+
command: <String>['whoohoo'],
628+
environment: expectedEnvWithDefaultTool,
629+
),
630+
);
631+
632+
await analytics.setTelemetry(false);
633+
634+
expect((await localProcessUtils.run(<String>['whoohoo'])).exitCode, 0);
635+
expect(fakeProcessManager, hasNoRemainingExpectations);
636+
},
637+
);
638+
}
639+
640+
for (final callerTool in <String>['dart-mcp-server', 'vscode-plugins']) {
641+
testWithoutContext(
642+
'ignores a caller-supplied DASH__TOOL of "$callerTool" when the parent has none',
643+
() async {
644+
fakeProcessManager.addCommand(
645+
const FakeCommand(
646+
command: <String>['whoohoo'],
647+
environment: expectedEnvWithDefaultTool,
648+
),
649+
);
650+
651+
await analytics.setTelemetry(false);
652+
653+
expect(
654+
(await processUtils.run(
655+
<String>['whoohoo'],
656+
environment: <String, String>{'DASH__TOOL': callerTool},
657+
)).exitCode,
658+
0,
659+
);
660+
expect(fakeProcessManager, hasNoRemainingExpectations);
661+
},
662+
);
663+
}
664+
665+
testWithoutContext(
666+
'prefers a valid parent DASH__TOOL over a different caller-supplied value',
667+
() async {
668+
final ProcessUtils localProcessUtils = createProcessUtils(
669+
platformEnvironment: const <String, String>{'DASH__TOOL': 'vscode-plugins'},
670+
);
671+
672+
fakeProcessManager.addCommand(
673+
const FakeCommand(command: <String>['whoohoo'], environment: expectedEnvWithParentTool),
674+
);
675+
676+
await analytics.setTelemetry(false);
677+
678+
expect(
679+
(await localProcessUtils.run(
680+
<String>['whoohoo'],
681+
environment: const <String, String>{'DASH__TOOL': 'intellij-plugins'},
682+
)).exitCode,
683+
0,
684+
);
685+
expect(fakeProcessManager, hasNoRemainingExpectations);
686+
},
687+
);
688+
617689
testWithoutContext('sets DASH__SUPPRESS_ANALYTICS to false when analytics can be sent and suppression is not requested', () async {
618690
expect(analytics.okToSend, isTrue);
619691
fakeProcessManager.addCommand(

0 commit comments

Comments
 (0)