Skip to content

Commit 458c576

Browse files
authored
[flutter_tools] Implement Diagnostics extension slice and doctor integration (flutter#191162)
## Description This PR implements Step 3 of Flutter Tools Extensibility: the Diagnostics extension slice and doctor integration. ### Architectural Overview 1. **`package:flutter_tools_core`**: - Defines core, platform-agnostic diagnostic models: `ValidationType`, `ValidationMessage`, and `ValidationResult`. 2. **`package:flutter_tools_extension`**: - Defines the `DiagnosticsExtension` interface and service contract (`getTitle()`, `runDiagnostics()`). 3. **`package:flutter_tools_extension_linux_prototype`**: - Implements `LinuxExtensionDiagnostics` validator in the Linux prototype extension. 4. **`packages/flutter_tools`**: - Implements `DiagnosticsExtensionClient` to query isolate extensions over RPC. - Introduces `ExtensionDoctorValidator` which adapts extension diagnostic results into Flutter tool `DoctorValidator` instances. - Wires `ExtensionDoctorValidator` into `Doctor.validators` via `ExtensionManager`. - Deprecates duplicate diagnostic types in `doctor_validator.dart` in favor of `package:flutter_tools_core` exports. ## Related Issues Part of flutter#190692 ## Tests - Added `packages/flutter_tools/packages/flutter_tools_core/test/diagnostics_test.dart` - Added `packages/flutter_tools/packages/flutter_tools_extension_linux_prototype/test/linux_diagnostics_test.dart` - Added `packages/flutter_tools/test/general.shard/extension_protocol/diagnostics_service_test.dart` - Added `packages/flutter_tools/test/commands.shard/hermetic/tool_extensions_integration_test.dart` - Added `packages/flutter_tools/test/integration.shard/tool_extensions_test.dart` - Updated existing doctor and validator tests in `commands.shard` and `general.shard`.
1 parent 47ec0eb commit 458c576

42 files changed

Lines changed: 931 additions & 173 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/flutter_tools/lib/executable.dart‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'package:args/args.dart';
6+
import 'package:flutter_tools_extension_linux_prototype/flutter_tools_extension_linux_prototype.dart';
67
import 'package:meta/meta.dart';
78

89
import 'runner.dart' as runner;
@@ -49,6 +50,8 @@ import 'src/commands/upgrade.dart';
4950
import 'src/commands/widget_preview.dart';
5051
import 'src/context/tool_dependencies.dart';
5152
import 'src/devtools_launcher.dart';
53+
import 'src/experimental/extension_discovery.dart';
54+
import 'src/experimental/extension_manager.dart';
5255
import 'src/features.dart';
5356
import 'src/globals.dart' as globals;
5457
// Files in `isolated` are intentionally excluded from google3 tooling.
@@ -106,11 +109,20 @@ Future<void> main(List<String> args) async {
106109

107110
await runner.run(
108111
args,
109-
(ToolDependencies toolDependencies) => generateCommands(
110-
toolDependencies: toolDependencies,
111-
verbose: verbose,
112-
verboseHelp: verboseHelp,
113-
),
112+
(ToolDependencies toolDependencies) {
113+
final manager = ExtensionManager(
114+
hostPlatform: globals.os.hostPlatform,
115+
logger: globals.logger,
116+
entryPoints: <ExtensionEntryPoint>[linuxExtensionEntryPoint],
117+
featureFlags: featureFlags,
118+
);
119+
return generateCommands(
120+
toolDependencies: toolDependencies,
121+
verboseHelp: verboseHelp,
122+
verbose: verbose,
123+
extensionManager: manager,
124+
);
125+
},
114126
verbose: verbose,
115127
muteCommandLogging: muteCommandLogging,
116128
verboseHelp: verboseHelp,
@@ -187,6 +199,7 @@ List<FlutterCommand> generateCommands({
187199
required ToolDependencies toolDependencies,
188200
required bool verbose,
189201
required bool verboseHelp,
202+
ExtensionManager? extensionManager,
190203
}) => <FlutterCommand>[
191204
AnalyzeCommand(
192205
verboseHelp: verboseHelp,
@@ -256,7 +269,7 @@ List<FlutterCommand> generateCommands({
256269
DaemonCommand(hidden: !verboseHelp),
257270
DebugAdapterCommand(verboseHelp: verboseHelp),
258271
DevicesCommand(verboseHelp: verboseHelp),
259-
DoctorCommand(verbose: verbose),
272+
DoctorCommand(verbose: verbose, extensionManager: extensionManager),
260273
DowngradeCommand(verboseHelp: verboseHelp, logger: toolDependencies.toolContext.logger),
261274
DriveCommand(
262275
verboseHelp: verboseHelp,

‎packages/flutter_tools/lib/src/android/android_workflow.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:async';
66

7+
import 'package:flutter_tools_core/flutter_tools_core.dart';
78
import 'package:process/process.dart';
89

910
import '../base/common.dart';

‎packages/flutter_tools/lib/src/commands/build_ios.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:typed_data';
66

77
import 'package:crypto/crypto.dart';
8+
import 'package:flutter_tools_core/flutter_tools_core.dart';
89
import 'package:meta/meta.dart';
910
import 'package:unified_analytics/unified_analytics.dart';
1011

‎packages/flutter_tools/lib/src/commands/doctor.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
import '../android/android_workflow.dart';
66
import '../base/common.dart';
7+
import '../experimental/extension_manager.dart';
78
import '../globals.dart' as globals;
89
import '../runner/flutter_command.dart';
910

1011
class DoctorCommand extends FlutterCommand {
11-
DoctorCommand({this.verbose = false}) {
12+
DoctorCommand({this.verbose = false, this.extensionManager}) {
1213
argParser.addFlag(
1314
'android-licenses',
1415
negatable: false,
@@ -25,6 +26,7 @@ class DoctorCommand extends FlutterCommand {
2526
}
2627

2728
final bool verbose;
29+
final ExtensionManager? extensionManager;
2830

2931
@override
3032
final name = 'doctor';
@@ -60,6 +62,7 @@ class DoctorCommand extends FlutterCommand {
6062
androidLicenses: boolArg('android-licenses'),
6163
verbose: verbose,
6264
androidLicenseValidator: androidLicenseValidator,
65+
extensionManager: extensionManager,
6366
) ??
6467
false;
6568
return FlutterCommandResult(success ? ExitStatus.success : ExitStatus.warning);

‎packages/flutter_tools/lib/src/doctor.dart‎

Lines changed: 82 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import 'dart:async';
66

7+
import 'package:flutter_tools_core/flutter_tools_core.dart';
8+
import 'package:flutter_tools_extension/flutter_tools_extension.dart';
79
import 'package:meta/meta.dart';
810
import 'package:process/process.dart';
911
import 'package:unified_analytics/unified_analytics.dart';
@@ -25,6 +27,8 @@ import 'cache.dart';
2527
import 'custom_devices/custom_device_workflow.dart';
2628
import 'device.dart';
2729
import 'doctor_validator.dart';
30+
import 'experimental/diagnostics.dart';
31+
import 'experimental/extension_manager.dart';
2832
import 'features.dart';
2933
import 'globals.dart' as globals;
3034
import 'http_host_validator.dart';
@@ -48,12 +52,30 @@ const _kIntelMacWarning = ValidationMessage.hint(
4852
);
4953

5054
abstract class DoctorValidatorsProvider {
55+
/// Create a [DoctorValidatorsProvider] with explicit options.
56+
factory DoctorValidatorsProvider.create({
57+
Platform? platform,
58+
required FeatureFlags featureFlags,
59+
ExtensionManager? extensionManager,
60+
}) {
61+
return _DefaultDoctorValidatorsProvider(
62+
platform: platform ?? globals.platform,
63+
featureFlags: featureFlags,
64+
extensionManager: extensionManager,
65+
);
66+
}
67+
5168
// Allow tests to construct a [_DefaultDoctorValidatorsProvider] with explicit
5269
// [FeatureFlags].
53-
factory DoctorValidatorsProvider.test({Platform? platform, required FeatureFlags featureFlags}) {
70+
factory DoctorValidatorsProvider.test({
71+
Platform? platform,
72+
required FeatureFlags featureFlags,
73+
ExtensionManager? extensionManager,
74+
}) {
5475
return _DefaultDoctorValidatorsProvider(
5576
featureFlags: featureFlags,
5677
platform: platform ?? FakePlatform(),
78+
extensionManager: extensionManager,
5779
);
5880
}
5981

@@ -70,12 +92,17 @@ abstract class DoctorValidatorsProvider {
7092
}
7193

7294
class _DefaultDoctorValidatorsProvider implements DoctorValidatorsProvider {
73-
_DefaultDoctorValidatorsProvider({required this.platform, required this.featureFlags});
95+
_DefaultDoctorValidatorsProvider({
96+
required this.platform,
97+
required this.featureFlags,
98+
this.extensionManager,
99+
});
74100

75101
List<DoctorValidator>? _validators;
76102
List<Workflow>? _workflows;
77103
final Platform platform;
78104
final FeatureFlags featureFlags;
105+
final ExtensionManager? extensionManager;
79106

80107
late final linuxWorkflow = LinuxWorkflow(platform: platform, featureFlags: featureFlags);
81108

@@ -141,6 +168,9 @@ class _DefaultDoctorValidatorsProvider implements DoctorValidatorsProvider {
141168
processManager: globals.processManager,
142169
userMessages: globals.userMessages,
143170
),
171+
if (extensionManager case final ExtensionManager manager)
172+
for (final DiagnosticsExtension extension in manager.diagnosticsExtensions)
173+
ExtensionDoctorValidator(extension: extension, logger: globals.logger),
144174
if (windowsWorkflow!.appliesToHostPlatform) visualStudioValidator!,
145175
if (proxyValidator.shouldShow) proxyValidator,
146176
if (globals.deviceManager?.canListAnything ?? false)
@@ -183,40 +213,52 @@ class Doctor {
183213

184214
/// Return a list of [ValidatorTask] objects and starts validation on all
185215
/// objects in [validators].
186-
List<ValidatorTask> startValidatorTasks() => <ValidatorTask>[
187-
for (final DoctorValidator validator in validators)
188-
ValidatorTask(
189-
validator,
190-
// We use an asyncGuard() here to be absolutely certain that
191-
// DoctorValidators do not result in an uncaught exception. Since the
192-
// Future returned by the asyncGuard() is not awaited, we pass an
193-
// onError callback to it and translate errors into ValidationResults.
194-
asyncGuard<ValidationResult>(
195-
() {
196-
final timeoutCompleter = Completer<ValidationResult>();
197-
final timer = Timer(doctorDuration, () {
198-
timeoutCompleter.completeError(
199-
Exception(
200-
'${validator.title} exceeded maximum allowed duration of $doctorDuration',
201-
),
202-
);
203-
});
204-
final Future<ValidationResult> validatorFuture = validator.validate();
205-
return Future.any<ValidationResult>(<Future<ValidationResult>>[
206-
validatorFuture,
207-
// This future can only complete with an error
208-
timeoutCompleter.future,
209-
]).then((ValidationResult result) async {
210-
timer.cancel();
211-
return result;
212-
});
213-
},
214-
onError: (Object exception, StackTrace stackTrace) {
215-
return ValidationResult.crash(exception, stackTrace);
216-
},
216+
Future<List<ValidatorTask>> startValidatorTasks({ExtensionManager? extensionManager}) async {
217+
if (extensionManager != null) {
218+
await extensionManager.ensureInitialized();
219+
}
220+
final List<DoctorValidator> validatorList = extensionManager != null
221+
? DoctorValidatorsProvider.create(
222+
platform: globals.platform,
223+
featureFlags: featureFlags,
224+
extensionManager: extensionManager,
225+
).validators
226+
: validators;
227+
return <ValidatorTask>[
228+
for (final DoctorValidator validator in validatorList)
229+
ValidatorTask(
230+
validator,
231+
// We use an asyncGuard() here to be absolutely certain that
232+
// DoctorValidators do not result in an uncaught exception. Since the
233+
// Future returned by the asyncGuard() is not awaited, we pass an
234+
// onError callback to it and translate errors into ValidationResults.
235+
asyncGuard<ValidationResult>(
236+
() {
237+
final timeoutCompleter = Completer<ValidationResult>();
238+
final timer = Timer(doctorDuration, () {
239+
timeoutCompleter.completeError(
240+
Exception(
241+
'${validator.title} exceeded maximum allowed duration of $doctorDuration',
242+
),
243+
);
244+
});
245+
final Future<ValidationResult> validatorFuture = validator.validate();
246+
return Future.any<ValidationResult>(<Future<ValidationResult>>[
247+
validatorFuture,
248+
// This future can only complete with an error
249+
timeoutCompleter.future,
250+
]).then((ValidationResult result) async {
251+
timer.cancel();
252+
return result;
253+
});
254+
},
255+
onError: (Object exception, StackTrace stackTrace) {
256+
return ValidationResult.crash(exception, stackTrace);
257+
},
258+
),
217259
),
218-
),
219-
];
260+
];
261+
}
220262

221263
List<Workflow> get workflows {
222264
return DoctorValidatorsProvider._instance.workflows;
@@ -313,6 +355,7 @@ class Doctor {
313355
bool showPii = true,
314356
List<ValidatorTask>? startedValidatorTasks,
315357
bool sendEvent = true,
358+
ExtensionManager? extensionManager,
316359
}) async {
317360
final bool showColor = globals.terminal.supportsColor;
318361
if (androidLicenses && androidLicenseValidator != null) {
@@ -329,7 +372,8 @@ class Doctor {
329372
// were sent for each doctor validator and its result
330373
final int analyticsTimestamp = _clock.now().millisecondsSinceEpoch;
331374

332-
for (final ValidatorTask validatorTask in startedValidatorTasks ?? startValidatorTasks()) {
375+
for (final ValidatorTask validatorTask
376+
in startedValidatorTasks ?? await startValidatorTasks(extensionManager: extensionManager)) {
333377
final DoctorValidator validator = validatorTask.validator;
334378
final Status status = _logger.startSpinner(
335379
timeout: validator.slowWarningDuration,
@@ -831,12 +875,12 @@ class DoctorText {
831875
late final Future<String> piiStrippedText = _runDiagnosis(false);
832876

833877
// Start the validator tasks only once.
834-
late final List<ValidatorTask> _validatorTasks = _doctor.startValidatorTasks();
878+
late final Future<List<ValidatorTask>> _validatorTasks = _doctor.startValidatorTasks();
835879

836880
Future<String> _runDiagnosis(bool showPii) async {
837881
try {
838882
await _doctor.diagnose(
839-
startedValidatorTasks: _validatorTasks,
883+
startedValidatorTasks: await _validatorTasks,
840884
showPii: showPii,
841885
sendEvent: _sendDoctorEvent,
842886
verbose: _logger.isVerbose,

0 commit comments

Comments
 (0)