[analytics] propagate analytics environment variables to subtools

Updates the main `dartdev` command runner to read and propagate the unified analytics environment variables (`DASH__SUPPRESS_ANALYTICS` and `DASH__TOOL`) to all spawned isolates and child processes using the handy new `VmInteropHandler.setEnvironmentVariable` support (see: https://dart-review.googlesource.com/c/sdk/+/499300).

Fixes: https://github.com/dart-lang/sdk/issues/62876



Change-Id: Iae68790a2cf861dd01edbea81f9faf7d7529f5f2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/504321
Reviewed-by: Ben Konyi <bkonyi@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
This commit is contained in:
pq
2026-05-19 11:57:42 -07:00
committed by dart-scoped@luci-project-accounts.iam.gserviceaccount.com
parent 039aef728a
commit 04c6612781
2 changed files with 133 additions and 1 deletions
+28 -1
View File
@@ -185,10 +185,13 @@ class DartdevRunner extends CommandRunner<int> {
// We don't want to run analytics when we're running in a CI environment
// unless we're explicitly testing analytics for dartdev.
final implicitlySuppressAnalytics = isBot() && !_isAnalyticsTest;
final envSuppressAnalytics =
io.Platform.environment[DashEnvVar.suppressAnalytics.name] == 'true';
bool suppressAnalytics =
!topLevelResults.flag('analytics') ||
topLevelResults.flag('suppress-analytics') ||
implicitlySuppressAnalytics;
implicitlySuppressAnalytics ||
envSuppressAnalytics;
if (topLevelResults.wasParsed('analytics')) {
io.stderr.writeln(
@@ -200,6 +203,7 @@ class DartdevRunner extends CommandRunner<int> {
final disableAnalytics = topLevelResults.flag('disable-analytics');
if (!implicitlySuppressAnalytics &&
!envSuppressAnalytics &&
suppressAnalytics &&
(enableAnalytics || disableAnalytics)) {
// This isn't an error if we're implicitly disabling analytics because
@@ -210,6 +214,29 @@ class DartdevRunner extends CommandRunner<int> {
);
return 254;
}
// Propagate analytics environment variables to subtools.
// Since VmInteropHandler.setEnvironmentVariable is non-overwriting by design
// in C++, we unset the variable first to ensure the explicitly resolved
// value takes precedence.
VmInteropHandler.setEnvironmentVariable(
DashEnvVar.suppressAnalytics.name,
null,
);
VmInteropHandler.setEnvironmentVariable(
DashEnvVar.suppressAnalytics.name,
suppressAnalytics.toString(),
);
final envTool = io.Platform.environment[DashEnvVar.tool.name];
if (envTool == null) {
VmInteropHandler.setEnvironmentVariable(
DashEnvVar.tool.name,
DashTool.dartTool.label,
);
}
// The Analytics instance used to report information back to Google Analytics;
// see lib/src/unified_analytics.dart.
_unifiedAnalytics ??= createUnifiedAnalytics(
+105
View File
@@ -55,5 +55,110 @@ void main() {
// so dartdev should not overwrite it.
expect(result.stdout, contains('DART_ROOT: original_value'));
});
test('run command sets DASH__TOOL and DASH__SUPPRESS_ANALYTICS', () async {
final p = project(
mainSrc: '''
import 'dart:io';
void main() {
print('DASH__TOOL: \${Platform.environment['DASH__TOOL']}');
print('DASH__SUPPRESS_ANALYTICS: '
'\${Platform.environment['DASH__SUPPRESS_ANALYTICS']}');
}
''',
);
final result = await Process.run(
Platform.resolvedExecutable,
['run', p.relativeFilePath],
workingDirectory: p.dir.path,
environment: {
'PUB_CACHE': p.pubCachePath,
// Force 'BOT': 'false' to ensure that the test is not affected if the
// test suite itself is running in a CI / bot environment, which would
// otherwise implicitly suppress analytics.
'BOT': 'false',
},
);
expect(result.exitCode, 0);
expect(result.stdout, contains('DASH__TOOL: dart-tool'));
expect(result.stdout, contains('DASH__SUPPRESS_ANALYTICS: false'));
});
test('run command preserves existing DASH__TOOL', () async {
final p = project(
mainSrc: '''
import 'dart:io';
void main() {
print('DASH__TOOL: \${Platform.environment['DASH__TOOL']}');
}
''',
);
final result = await Process.run(
Platform.resolvedExecutable,
['run', p.relativeFilePath],
workingDirectory: p.dir.path,
environment: {
'PUB_CACHE': p.pubCachePath,
'DASH__TOOL': 'flutter-tool',
},
);
expect(result.exitCode, 0);
expect(result.stdout, contains('DASH__TOOL: flutter-tool'));
});
test(
'--suppress-analytics propagates DASH__SUPPRESS_ANALYTICS=true',
() async {
final p = project(
mainSrc: '''
import 'dart:io';
void main() {
print('DASH__SUPPRESS_ANALYTICS: '
'\${Platform.environment['DASH__SUPPRESS_ANALYTICS']}');
}
''',
);
final result = await Process.run(
Platform.resolvedExecutable,
['--suppress-analytics', 'run', p.relativeFilePath],
workingDirectory: p.dir.path,
environment: {
'PUB_CACHE': p.pubCachePath,
},
);
expect(result.exitCode, 0);
expect(result.stdout, contains('DASH__SUPPRESS_ANALYTICS: true'));
},
);
test(
'parent env DASH__SUPPRESS_ANALYTICS=true propagates as true',
() async {
final p = project(
mainSrc: '''
import 'dart:io';
void main() {
print('DASH__SUPPRESS_ANALYTICS: '
'\${Platform.environment['DASH__SUPPRESS_ANALYTICS']}');
}
''',
);
final result = await Process.run(
Platform.resolvedExecutable,
['run', p.relativeFilePath],
workingDirectory: p.dir.path,
environment: {
'PUB_CACHE': p.pubCachePath,
'DASH__SUPPRESS_ANALYTICS': 'true',
},
);
expect(result.exitCode, 0);
expect(result.stdout, contains('DASH__SUPPRESS_ANALYTICS: true'));
},
);
});
}