Revert "[flutter_tools] Disable analytics for more bots (#50579)" (#50622)
This reverts commit 0a38f29c0df5181d027c61ed9e8e49b94a305faa.
diff --git a/packages/flutter_tools/lib/runner.dart b/packages/flutter_tools/lib/runner.dart
index 4dd5631..010a0c0 100644
--- a/packages/flutter_tools/lib/runner.dart
+++ b/packages/flutter_tools/lib/runner.dart
@@ -9,6 +9,7 @@
import 'package:intl/intl_standalone.dart' as intl_standalone;
import 'package:meta/meta.dart';
+import 'src/base/bot_detector.dart';
import 'src/base/common.dart';
import 'src/base/context.dart';
import 'src/base/file_system.dart';
@@ -34,8 +35,8 @@
bool reportCrashes,
String flutterVersion,
Map<Type, Generator> overrides,
-}) async {
- reportCrashes ??= !await globals.isRunningOnBot;
+}) {
+ reportCrashes ??= !isRunningOnBot(globals.platform);
if (muteCommandLogging) {
// Remove the verbose option; for help and doctor, users don't need to see
diff --git a/packages/flutter_tools/lib/src/base/bot_detector.dart b/packages/flutter_tools/lib/src/base/bot_detector.dart
index 6c3de38..9660d67 100644
--- a/packages/flutter_tools/lib/src/base/bot_detector.dart
+++ b/packages/flutter_tools/lib/src/base/bot_detector.dart
@@ -5,109 +5,56 @@
import 'package:meta/meta.dart';
import 'package:platform/platform.dart';
-import 'io.dart';
-import 'net.dart';
+import 'context.dart';
class BotDetector {
BotDetector({
- @required HttpClientFactory httpClientFactory,
@required Platform platform,
- }) :
- _platform = platform,
- _azureDetector = AzureDetector(
- httpClientFactory: httpClientFactory,
- );
+ }) : _platform = platform;
final Platform _platform;
- final AzureDetector _azureDetector;
- bool _isRunningOnBot;
-
- Future<bool> get isRunningOnBot async {
- if (_isRunningOnBot != null) {
- return _isRunningOnBot;
- }
+ bool get isRunningOnBot {
if (
- // Explicitly stated to not be a bot.
- _platform.environment['BOT'] == 'false'
+ // Explicitly stated to not be a bot.
+ _platform.environment['BOT'] == 'false'
- // Set by the IDEs to the IDE name, so a strong signal that this is not a bot.
- || _platform.environment.containsKey('FLUTTER_HOST')
- // When set, GA logs to a local file (normally for tests) so we don't need to filter.
- || _platform.environment.containsKey('FLUTTER_ANALYTICS_LOG_FILE')
+ // Set by the IDEs to the IDE name, so a strong signal that this is not a bot.
+ || _platform.environment.containsKey('FLUTTER_HOST')
+ // When set, GA logs to a local file (normally for tests) so we don't need to filter.
+ || _platform.environment.containsKey('FLUTTER_ANALYTICS_LOG_FILE')
) {
- return _isRunningOnBot = false;
+ return false;
}
- return _isRunningOnBot = _platform.environment['BOT'] == 'true'
+ return _platform.environment['BOT'] == 'true'
- // https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables
- || _platform.environment['TRAVIS'] == 'true'
- || _platform.environment['CONTINUOUS_INTEGRATION'] == 'true'
- || _platform.environment.containsKey('CI') // Travis and AppVeyor
+ // https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables
+ || _platform.environment['TRAVIS'] == 'true'
+ || _platform.environment['CONTINUOUS_INTEGRATION'] == 'true'
+ || _platform.environment.containsKey('CI') // Travis and AppVeyor
- // https://www.appveyor.com/docs/environment-variables/
- || _platform.environment.containsKey('APPVEYOR')
+ // https://www.appveyor.com/docs/environment-variables/
+ || _platform.environment.containsKey('APPVEYOR')
- // https://cirrus-ci.org/guide/writing-tasks/#environment-variables
- || _platform.environment.containsKey('CIRRUS_CI')
+ // https://cirrus-ci.org/guide/writing-tasks/#environment-variables
+ || _platform.environment.containsKey('CIRRUS_CI')
- // https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html
- || (_platform.environment.containsKey('AWS_REGION') &&
- _platform.environment.containsKey('CODEBUILD_INITIATOR'))
+ // https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html
+ || (_platform.environment.containsKey('AWS_REGION') &&
+ _platform.environment.containsKey('CODEBUILD_INITIATOR'))
- // https://wiki.jenkins.io/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-belowJenkinsSetEnvironmentVariables
- || _platform.environment.containsKey('JENKINS_URL')
+ // https://wiki.jenkins.io/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-belowJenkinsSetEnvironmentVariables
+ || _platform.environment.containsKey('JENKINS_URL')
- // Properties on Flutter's Chrome Infra bots.
- || _platform.environment['CHROME_HEADLESS'] == '1'
- || _platform.environment.containsKey('BUILDBOT_BUILDERNAME')
- || _platform.environment.containsKey('SWARMING_TASK_ID')
- || await _azureDetector.isRunningOnAzure;
+ // Properties on Flutter's Chrome Infra bots.
+ || _platform.environment['CHROME_HEADLESS'] == '1'
+ || _platform.environment.containsKey('BUILDBOT_BUILDERNAME')
+ || _platform.environment.containsKey('SWARMING_TASK_ID');
}
}
-// Are we running on Azure?
-// https://docs.microsoft.com/en-us/azure/virtual-machines/linux/instance-metadata-service
-@visibleForTesting
-class AzureDetector {
- AzureDetector({
- @required HttpClientFactory httpClientFactory,
- }) : _httpClientFactory = httpClientFactory;
-
- static const String _serviceUrl = 'http://169.254.169.254/metadata/instance';
-
- final HttpClientFactory _httpClientFactory;
-
- bool _isRunningOnAzure;
-
- Future<bool> get isRunningOnAzure async {
- if (_isRunningOnAzure != null) {
- return _isRunningOnAzure;
- }
- final HttpClient client = _httpClientFactory()
- ..connectionTimeout = const Duration(seconds: 1);
- try {
- final HttpClientRequest request = await client.getUrl(
- Uri.parse(_serviceUrl),
- );
- request.headers.add('Metadata', true);
- await request.close();
- } on SocketException catch (e) {
- if (e.toString().contains('HTTP connection timed out')) {
- // If the connection attempt times out, assume that the service is not
- // available, and that we are not running on Azure.
- return _isRunningOnAzure = false;
- }
- // If it's some other socket exception, then there is a bug in the
- // detection code that should go to crash logging.
- rethrow;
- } on HttpException {
- // If the connection gets set up, but encounters an error condition, it
- // still means we're on Azure.
- return _isRunningOnAzure = true;
- }
- // We got a response, we're running on Azure.
- return _isRunningOnAzure = true;
- }
+bool isRunningOnBot(Platform platform) {
+ final BotDetector botDetector = context.get<BotDetector>() ?? BotDetector(platform: platform);
+ return botDetector.isRunningOnBot;
}
diff --git a/packages/flutter_tools/lib/src/context_runner.dart b/packages/flutter_tools/lib/src/context_runner.dart
index f159906..78b4754 100644
--- a/packages/flutter_tools/lib/src/context_runner.dart
+++ b/packages/flutter_tools/lib/src/context_runner.dart
@@ -61,18 +61,9 @@
FutureOr<T> runner(), {
Map<Type, Generator> overrides,
}) async {
-
- // Wrap runner with any asynchronous initialization that should run with the
- // overrides and callbacks.
- bool runningOnBot;
- FutureOr<T> runnerWrapper() async {
- runningOnBot = await globals.isRunningOnBot;
- return runner();
- }
-
return await context.run<T>(
name: 'global fallbacks',
- body: runnerWrapper,
+ body: runner,
overrides: overrides,
fallbacks: <Type, Generator>{
AndroidLicenseValidator: () => AndroidLicenseValidator(),
@@ -168,9 +159,7 @@
Stdio: () => const Stdio(),
SystemClock: () => const SystemClock(),
TimeoutConfiguration: () => const TimeoutConfiguration(),
- Usage: () => Usage(
- runningOnBot: runningOnBot,
- ),
+ Usage: () => Usage(),
UserMessages: () => UserMessages(),
VisualStudio: () => VisualStudio(),
VisualStudioValidator: () => const VisualStudioValidator(),
diff --git a/packages/flutter_tools/lib/src/dart/pub.dart b/packages/flutter_tools/lib/src/dart/pub.dart
index ff0ef4c..b4d45db 100644
--- a/packages/flutter_tools/lib/src/dart/pub.dart
+++ b/packages/flutter_tools/lib/src/dart/pub.dart
@@ -6,6 +6,7 @@
import 'package:meta/meta.dart';
+import '../base/bot_detector.dart';
import '../base/common.dart';
import '../base/context.dart';
import '../base/file_system.dart';
@@ -231,7 +232,7 @@
@required bool retry,
bool showTraceForErrors,
}) async {
- showTraceForErrors ??= await globals.isRunningOnBot;
+ showTraceForErrors ??= isRunningOnBot(globals.platform);
String lastPubMessage = 'no message';
bool versionSolvingFailed = false;
@@ -258,7 +259,7 @@
_pubCommand(arguments),
workingDirectory: directory,
mapFunction: filterWrapper, // may set versionSolvingFailed, lastPubMessage
- environment: await _createPubEnvironment(context),
+ environment: _createPubEnvironment(context),
);
String message;
switch (code) {
@@ -303,7 +304,7 @@
final io.Process process = await processUtils.start(
_pubCommand(arguments),
workingDirectory: directory,
- environment: await _createPubEnvironment(PubContext.interactive),
+ environment: _createPubEnvironment(PubContext.interactive),
);
// Pipe the Flutter tool stdin to the pub stdin.
@@ -347,10 +348,10 @@
///
/// [context] provides extra information to package server requests to
/// understand usage.
-Future<Map<String, String>> _createPubEnvironment(PubContext context) async {
+Map<String, String> _createPubEnvironment(PubContext context) {
final Map<String, String> environment = <String, String>{
'FLUTTER_ROOT': Cache.flutterRoot,
- _pubEnvironmentKey: await _getPubEnvironmentValue(context),
+ _pubEnvironmentKey: _getPubEnvironmentValue(context),
};
final String pubCache = _getRootPubCacheIfAvailable();
if (pubCache != null) {
@@ -373,13 +374,13 @@
///
/// [context] provides extra information to package server requests to
/// understand usage.
-Future<String> _getPubEnvironmentValue(PubContext pubContext) async {
+String _getPubEnvironmentValue(PubContext pubContext) {
// DO NOT update this function without contacting kevmoo.
// We have server-side tooling that assumes the values are consistent.
final String existing = globals.platform.environment[_pubEnvironmentKey];
final List<String> values = <String>[
if (existing != null && existing.isNotEmpty) existing,
- if (await globals.isRunningOnBot) 'flutter_bot',
+ if (isRunningOnBot(globals.platform)) 'flutter_bot',
'flutter_cli',
...pubContext._values,
];
diff --git a/packages/flutter_tools/lib/src/globals.dart b/packages/flutter_tools/lib/src/globals.dart
index bea01e8..1f05bd5 100644
--- a/packages/flutter_tools/lib/src/globals.dart
+++ b/packages/flutter_tools/lib/src/globals.dart
@@ -8,14 +8,12 @@
import 'android/android_sdk.dart';
import 'android/android_studio.dart';
import 'artifacts.dart';
-import 'base/bot_detector.dart';
import 'base/config.dart';
import 'base/context.dart';
import 'base/error_handling_file_system.dart';
import 'base/file_system.dart';
import 'base/io.dart';
import 'base/logger.dart';
-import 'base/net.dart';
import 'base/os.dart';
import 'base/terminal.dart';
import 'base/user_messages.dart';
@@ -67,15 +65,6 @@
XCDevice get xcdevice => context.get<XCDevice>();
-final BotDetector _defaultBotDetector = BotDetector(
- httpClientFactory: context.get<HttpClientFactory>() ?? () => HttpClient(),
- platform: platform,
-);
-
-BotDetector get botDetector => context.get<BotDetector>() ?? _defaultBotDetector;
-
-Future<bool> get isRunningOnBot => botDetector.isRunningOnBot;
-
/// Display an error level message to the user. Commands should use this if they
/// fail in some way.
///
diff --git a/packages/flutter_tools/lib/src/reporting/reporting.dart b/packages/flutter_tools/lib/src/reporting/reporting.dart
index 1b9ba70..13fe6a1 100644
--- a/packages/flutter_tools/lib/src/reporting/reporting.dart
+++ b/packages/flutter_tools/lib/src/reporting/reporting.dart
@@ -10,6 +10,7 @@
import 'package:meta/meta.dart';
import 'package:usage/usage_io.dart';
+import '../base/bot_detector.dart';
import '../base/context.dart';
import '../base/file_system.dart';
import '../base/io.dart';
diff --git a/packages/flutter_tools/lib/src/reporting/usage.dart b/packages/flutter_tools/lib/src/reporting/usage.dart
index 7255976..4bb3497 100644
--- a/packages/flutter_tools/lib/src/reporting/usage.dart
+++ b/packages/flutter_tools/lib/src/reporting/usage.dart
@@ -76,12 +76,10 @@
String versionOverride,
String configDirOverride,
String logFile,
- @required bool runningOnBot,
}) => _DefaultUsage(settingsName: settingsName,
versionOverride: versionOverride,
configDirOverride: configDirOverride,
- logFile: logFile,
- runningOnBot: runningOnBot);
+ logFile: logFile);
/// Returns [Usage] active in the current app context.
static Usage get instance => context.get<Usage>();
@@ -163,7 +161,6 @@
String versionOverride,
String configDirOverride,
String logFile,
- @required bool runningOnBot,
}) {
final FlutterVersion flutterVersion = globals.flutterVersion;
final String version = versionOverride ?? flutterVersion.getVersionString(redactUnknownBranches: true);
@@ -179,7 +176,7 @@
// Many CI systems don't do a full git checkout.
version.endsWith('/unknown') ||
// Ignore bots.
- runningOnBot ||
+ isRunningOnBot(globals.platform) ||
// Ignore when suppressed by FLUTTER_SUPPRESS_ANALYTICS.
suppressEnvFlag
)) {
diff --git a/packages/flutter_tools/test/commands.shard/permeable/packages_test.dart b/packages/flutter_tools/test/commands.shard/permeable/packages_test.dart
index 8a64a0e..8d110ce 100644
--- a/packages/flutter_tools/test/commands.shard/permeable/packages_test.dart
+++ b/packages/flutter_tools/test/commands.shard/permeable/packages_test.dart
@@ -25,7 +25,7 @@
const AlwaysTrueBotDetector();
@override
- Future<bool> get isRunningOnBot async => true;
+ bool get isRunningOnBot => true;
}
@@ -33,7 +33,7 @@
const AlwaysFalseBotDetector();
@override
- Future<bool> get isRunningOnBot async => false;
+ bool get isRunningOnBot => false;
}
diff --git a/packages/flutter_tools/test/general.shard/analytics_test.dart b/packages/flutter_tools/test/general.shard/analytics_test.dart
index e837c6c..cdff1fb 100644
--- a/packages/flutter_tools/test/general.shard/analytics_test.dart
+++ b/packages/flutter_tools/test/general.shard/analytics_test.dart
@@ -69,7 +69,6 @@
Usage: () => Usage(
configDirOverride: tempDir.path,
logFile: tempDir.childFile('analytics.log').path,
- runningOnBot: true,
),
});
@@ -92,14 +91,13 @@
Usage: () => Usage(
configDirOverride: tempDir.path,
logFile: tempDir.childFile('analytics.log').path,
- runningOnBot: true,
),
});
testUsingContext('Usage records one feature in experiment setting', () async {
when<bool>(mockFlutterConfig.getValue(flutterWebFeature.configSetting) as bool)
.thenReturn(true);
- final Usage usage = Usage(runningOnBot: true);
+ final Usage usage = Usage();
usage.sendCommand('test');
final String featuresKey = cdKey(CustomDimensions.enabledFlutterFeatures);
@@ -121,7 +119,7 @@
.thenReturn(true);
when<bool>(mockFlutterConfig.getValue(flutterMacOSDesktopFeature.configSetting) as bool)
.thenReturn(true);
- final Usage usage = Usage(runningOnBot: true);
+ final Usage usage = Usage();
usage.sendCommand('test');
final String featuresKey = cdKey(CustomDimensions.enabledFlutterFeatures);
@@ -215,10 +213,7 @@
mockTimes = <int>[kMillis];
// Since FLUTTER_ANALYTICS_LOG_FILE is set in the environment, analytics
// will be written to a file.
- final Usage usage = Usage(
- versionOverride: 'test',
- runningOnBot: true,
- );
+ final Usage usage = Usage(versionOverride: 'test');
usage.suppressAnalytics = false;
usage.enabled = true;
@@ -244,10 +239,7 @@
mockTimes = <int>[kMillis];
// Since FLUTTER_ANALYTICS_LOG_FILE is set in the environment, analytics
// will be written to a file.
- final Usage usage = Usage(
- versionOverride: 'test',
- runningOnBot: true,
- );
+ final Usage usage = Usage(versionOverride: 'test');
usage.suppressAnalytics = false;
usage.enabled = true;
@@ -280,7 +272,7 @@
tryToDelete(tempDir);
});
- testUsingContext("don't send on bots with unknown version", () async {
+ testUsingContext("don't send on bots", () async {
int count = 0;
flutterUsage.onSend.listen((Map<String, dynamic> data) => count++);
@@ -291,7 +283,6 @@
settingsName: 'flutter_bot_test',
versionOverride: 'dev/unknown',
configDirOverride: tempDir.path,
- runningOnBot: false,
),
});
@@ -307,7 +298,6 @@
settingsName: 'flutter_bot_test',
versionOverride: 'dev/unknown',
configDirOverride: tempDir.path,
- runningOnBot: false,
),
});
});
diff --git a/packages/flutter_tools/test/general.shard/base/bot_detector_test.dart b/packages/flutter_tools/test/general.shard/base/bot_detector_test.dart
index e44489d..023df27 100644
--- a/packages/flutter_tools/test/general.shard/base/bot_detector_test.dart
+++ b/packages/flutter_tools/test/general.shard/base/bot_detector_test.dart
@@ -3,8 +3,6 @@
// found in the LICENSE file.
import 'package:flutter_tools/src/base/bot_detector.dart';
-import 'package:flutter_tools/src/base/io.dart';
-import 'package:mockito/mockito.dart';
import 'package:platform/platform.dart';
import '../../src/common.dart';
@@ -14,94 +12,39 @@
group('BotDetector', () {
FakePlatform fakePlatform;
MockStdio mockStdio;
- MockHttpClient mockHttpClient;
- MockHttpClientRequest mockHttpClientRequest;
- MockHttpHeaders mockHttpHeaders;
BotDetector botDetector;
setUp(() {
fakePlatform = FakePlatform()..environment = <String, String>{};
mockStdio = MockStdio();
- mockHttpClient = MockHttpClient();
- mockHttpClientRequest = MockHttpClientRequest();
- mockHttpHeaders = MockHttpHeaders();
- botDetector = BotDetector(
- platform: fakePlatform,
- httpClientFactory: () => mockHttpClient,
- );
+ botDetector = BotDetector(platform: fakePlatform);
});
group('isRunningOnBot', () {
testWithoutContext('returns false unconditionally if BOT=false is set', () async {
fakePlatform.environment['BOT'] = 'false';
fakePlatform.environment['TRAVIS'] = 'true';
- expect(await botDetector.isRunningOnBot, isFalse);
+ expect(botDetector.isRunningOnBot, isFalse);
});
testWithoutContext('returns false unconditionally if FLUTTER_HOST is set', () async {
fakePlatform.environment['FLUTTER_HOST'] = 'foo';
fakePlatform.environment['TRAVIS'] = 'true';
- expect(await botDetector.isRunningOnBot, isFalse);
+ expect(botDetector.isRunningOnBot, isFalse);
});
testWithoutContext('returns false with and without a terminal attached', () async {
- when(mockHttpClient.getUrl(any)).thenAnswer((_) {
- throw const SocketException('HTTP connection timed out');
- });
mockStdio.stdout.hasTerminal = true;
- expect(await botDetector.isRunningOnBot, isFalse);
+ expect(botDetector.isRunningOnBot, isFalse);
mockStdio.stdout.hasTerminal = false;
- expect(await botDetector.isRunningOnBot, isFalse);
+ expect(botDetector.isRunningOnBot, isFalse);
});
testWithoutContext('can test analytics outputs on bots when outputting to a file', () async {
fakePlatform.environment['TRAVIS'] = 'true';
fakePlatform.environment['FLUTTER_ANALYTICS_LOG_FILE'] = '/some/file';
- expect(await botDetector.isRunningOnBot, isFalse);
+ expect(botDetector.isRunningOnBot, isFalse);
});
-
- testWithoutContext('returns true when azure metadata is reachable', () async {
- when(mockHttpClient.getUrl(any)).thenAnswer((_) {
- return Future<HttpClientRequest>.value(mockHttpClientRequest);
- });
- when(mockHttpClientRequest.headers).thenReturn(mockHttpHeaders);
- expect(await botDetector.isRunningOnBot, isTrue);
- });
- });
- });
-
- group('AzureDetector', () {
- AzureDetector azureDetector;
- MockHttpClient mockHttpClient;
- MockHttpClientRequest mockHttpClientRequest;
- MockHttpHeaders mockHttpHeaders;
-
- setUp(() {
- mockHttpClient = MockHttpClient();
- mockHttpClientRequest = MockHttpClientRequest();
- mockHttpHeaders = MockHttpHeaders();
- azureDetector = AzureDetector(
- httpClientFactory: () => mockHttpClient,
- );
- });
-
- testWithoutContext('isRunningOnAzure returns false when connection times out', () async {
- when(mockHttpClient.getUrl(any)).thenAnswer((_) {
- throw const SocketException('HTTP connection timed out');
- });
- expect(await azureDetector.isRunningOnAzure, isFalse);
- });
-
- testWithoutContext('isRunningOnAzure returns true when azure metadata is reachable', () async {
- when(mockHttpClient.getUrl(any)).thenAnswer((_) {
- return Future<HttpClientRequest>.value(mockHttpClientRequest);
- });
- when(mockHttpClientRequest.headers).thenReturn(mockHttpHeaders);
- expect(await azureDetector.isRunningOnAzure, isTrue);
});
});
}
-
-class MockHttpClient extends Mock implements HttpClient {}
-class MockHttpClientRequest extends Mock implements HttpClientRequest {}
-class MockHttpHeaders extends Mock implements HttpHeaders {}
diff --git a/packages/flutter_tools/test/general.shard/dart/pub_get_test.dart b/packages/flutter_tools/test/general.shard/dart/pub_get_test.dart
index cd51b15..f820969 100644
--- a/packages/flutter_tools/test/general.shard/dart/pub_get_test.dart
+++ b/packages/flutter_tools/test/general.shard/dart/pub_get_test.dart
@@ -365,7 +365,7 @@
class BotDetectorAlwaysNo implements BotDetector {
const BotDetectorAlwaysNo();
@override
- Future<bool> get isRunningOnBot async => false;
+ bool get isRunningOnBot => false;
}
typedef StartCallback = void Function(List<dynamic> command);
diff --git a/packages/flutter_tools/test/general.shard/runner/runner_test.dart b/packages/flutter_tools/test/general.shard/runner/runner_test.dart
index 77dd0fb..354a370a 100644
--- a/packages/flutter_tools/test/general.shard/runner/runner_test.dart
+++ b/packages/flutter_tools/test/general.shard/runner/runner_test.dart
@@ -147,10 +147,7 @@
}
class CrashingUsage implements Usage {
- CrashingUsage() : _impl = Usage(
- versionOverride: '[user-branch]',
- runningOnBot: true,
- );
+ CrashingUsage() : _impl = Usage(versionOverride: '[user-branch]');
final Usage _impl;