diff --git a/packages/flutter_tools/lib/src/base/dds.dart b/packages/flutter_tools/lib/src/base/dds.dart index feaf2bb3e8..d0ce5a8392 100644 --- a/packages/flutter_tools/lib/src/base/dds.dart +++ b/packages/flutter_tools/lib/src/base/dds.dart @@ -72,14 +72,17 @@ class DartDevelopmentService { e.message.split(' ').firstWhere((String e) => e.startsWith('http')) ); } on StateError { + if (e.message.contains('Existing VM service clients prevent DDS from taking control.')) { + throwToolExit('${e.message}. Please rebuild your application with a newer version of Flutter.'); + return; + } logger.printError( 'DDS has failed to start and there is not an existing DDS instance ' - 'available to connect to. Please comment on ' - 'https://github.com/flutter/flutter/issues/72385 with output from ' - '"flutter doctor -v" and the following error message:\n\n ${e.message}.' + 'available to connect to. Please file an issue at https://github.com/flutter/flutter/issues ' + 'with the following error message:\n\n ${e.message}.' ); - // Wrap the DDS error message in a StateError so it can be collected - // by the crash handler. + // DDS was unable to start for an unknown reason. Raise a StateError + // so it can be reported by the crash reporter. throw StateError(e.message); } } diff --git a/packages/flutter_tools/lib/src/resident_runner.dart b/packages/flutter_tools/lib/src/resident_runner.dart index ba5182a99d..005ac07f4b 100644 --- a/packages/flutter_tools/lib/src/resident_runner.dart +++ b/packages/flutter_tools/lib/src/resident_runner.dart @@ -274,6 +274,8 @@ class FlutterDevice { } else { existingDds = true; } + } on ToolExit { + rethrow; } on Exception catch (e, st) { handleError(e, st); return; diff --git a/packages/flutter_tools/test/general.shard/resident_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_runner_test.dart index cc84ad38f7..70c730c262 100644 --- a/packages/flutter_tools/test/general.shard/resident_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_runner_test.dart @@ -2953,6 +2953,47 @@ void main() { }) async => mockVMService, })); + testUsingContext('Handle existing VM service clients DDS error', () => testbed.run(() async { + fakeVmServiceHost = FakeVmServiceHost(requests: []); + final MockDevice mockDevice = MockDevice(); + when(mockDevice.dds).thenReturn(DartDevelopmentService(logger: testLogger)); + ddsLauncherCallback = (Uri uri, {bool enableAuthCodes, bool ipv6, Uri serviceUri}) { + throw FakeDartDevelopmentServiceException(message: + 'Existing VM service clients prevent DDS from taking control.', + ); + }; + final TestFlutterDevice flutterDevice = TestFlutterDevice( + mockDevice, + observatoryUris: Stream.value(testUri), + ); + bool caught = false; + final Completerdone = Completer(); + runZonedGuarded(() { + flutterDevice.connect(allowExistingDdsInstance: true).then((_) => done.complete()); + }, (Object e, StackTrace st) { + expect(e is ToolExit, true); + expect((e as ToolExit).message, + contains('Existing VM service clients prevent DDS from taking control.', + )); + done.complete(); + caught = true; + }); + await done.future; + if (!caught) { + fail('Expected ToolExit to be thrown.'); + } + }, overrides: { + VMServiceConnector: () => (Uri httpUri, { + ReloadSources reloadSources, + Restart restart, + CompileExpression compileExpression, + GetSkSLMethod getSkSLMethod, + PrintStructuredErrorLogMethod printStructuredErrorLogMethod, + io.CompressionOptions compression, + Device device, + }) async => mockVMService, + })); + testUsingContext('Failed DDS start outputs error message', () => testbed.run(() async { // See https://github.com/flutter/flutter/issues/72385 for context. fakeVmServiceHost = FakeVmServiceHost(requests: []);