Skip to content

Commit 1a6a20c

Browse files
authored
[flutter_tools] Include mode in app.start event, and forward app.start to DAP clients (#121239)
[flutter_tools] Include mode in app.start event, and forward app.start to DAP clients
1 parent 9235eb6 commit 1a6a20c

File tree

7 files changed

+59
-6
lines changed

7 files changed

+59
-6
lines changed

packages/flutter_tools/doc/daemon.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ The `stop()` command takes one parameter, `appId`. It returns a `bool` to indica
136136

137137
#### app.start
138138

139-
This is sent when an app is starting. The `params` field will be a map with the fields `appId`, `directory`, `deviceId`, and `launchMode`.
139+
This is sent when an app is starting. The `params` field will be a map with the fields `appId`, `directory`, `deviceId`, `launchMode` (`run`/`attach`) and `mode` (`debug`, `profile`, `release`, `jit_release`).
140140

141141
#### app.debugPort
142142

packages/flutter_tools/lib/src/commands/daemon.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ class AppDomain extends Domain {
613613
'directory': projectDirectory,
614614
'supportsRestart': isRestartSupported(enableHotReload, device),
615615
'launchMode': launchMode.toString(),
616+
'mode': runner.debuggingOptions.buildInfo.modeName,
616617
});
617618

618619
Completer<DebugConnectionInfo>? connectionInfoCompleter;

packages/flutter_tools/lib/src/debug_adapters/flutter_adapter.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,16 @@ class FlutterDebugAdapter extends FlutterBaseDebugAdapter {
393393
// session (which is much slower, but required for profile/release mode).
394394
final bool supportsRestart = (params['supportsRestart'] as bool?) ?? false;
395395
sendEvent(CapabilitiesEventBody(capabilities: Capabilities(supportsRestartRequest: supportsRestart)));
396+
397+
// Send a custom event so the editor has info about the app starting.
398+
//
399+
// This message contains things like the `deviceId` and `mode` that the
400+
// client might not know about if they were inferred or set by users custom
401+
// args.
402+
sendEvent(
403+
RawEventBody(params),
404+
eventType: 'flutter.appStart',
405+
);
396406
}
397407

398408
/// Handles the app.started event from Flutter.

packages/flutter_tools/test/general.shard/dap/mocks.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,18 @@ class MockFlutterDebugAdapter extends FlutterDebugAdapter {
107107
// Simulate the app starting by triggering handling of events that Flutter
108108
// would usually write to stdout.
109109
if (simulateAppStarted) {
110-
simulateStdoutMessage(<String, Object?>{
111-
'event': 'app.started',
112-
});
113110
simulateStdoutMessage(<String, Object?>{
114111
'event': 'app.start',
115112
'params': <String, Object?>{
116113
'appId': 'TEST',
117114
'supportsRestart': supportsRestart,
115+
'deviceId': 'flutter-tester',
116+
'mode': 'debug',
118117
}
119118
});
119+
simulateStdoutMessage(<String, Object?>{
120+
'event': 'app.started',
121+
});
120122
}
121123
}
122124

packages/flutter_tools/test/integration.shard/debug_adapter/flutter_adapter_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,30 @@ void main() {
452452
await dap.client.terminate();
453453
});
454454

455+
testWithoutContext('provides appStarted events to the client', () async {
456+
final BasicProject project = BasicProject();
457+
await project.setUpIn(tempDir);
458+
459+
// Launch the app and wait for it to send a 'flutter.appStart' event.
460+
final Future<Event> appStartFuture = dap.client.event('flutter.appStart');
461+
await Future.wait(<Future<void>>[
462+
appStartFuture,
463+
dap.client.start(
464+
launch: () => dap.client.launch(
465+
cwd: project.dir.path,
466+
toolArgs: <String>['-d', 'flutter-tester'],
467+
),
468+
),
469+
], eagerError: true);
470+
471+
await dap.client.terminate();
472+
473+
final Event appStart = await appStartFuture;
474+
final Map<String, Object?> params = appStart.body! as Map<String, Object?>;
475+
expect(params['deviceId'], 'flutter-tester');
476+
expect(params['mode'], 'debug');
477+
});
478+
455479
testWithoutContext('provides appStarted events to the client', () async {
456480
final BasicProject project = BasicProject();
457481
await project.setUpIn(tempDir);

packages/flutter_tools/test/integration.shard/flutter_run_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,10 @@ void main() {
6868
matches: isNotEmpty,
6969
);
7070
});
71+
72+
testWithoutContext('reports deviceId and mode in app.start event', () async {
73+
await flutter.run();
74+
expect(flutter.currentRunningDeviceId, 'flutter-tester');
75+
expect(flutter.currentRunningMode, 'debug');
76+
});
7177
}

packages/flutter_tools/test/integration.shard/test_driver.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,11 @@ class FlutterRunTestDriver extends FlutterTestDriver {
496496
});
497497

498498
String? _currentRunningAppId;
499+
String? _currentRunningDeviceId;
500+
String? _currentRunningMode;
501+
502+
String? get currentRunningDeviceId => _currentRunningDeviceId;
503+
String? get currentRunningMode => _currentRunningMode;
499504

500505
Future<void> run({
501506
bool withDebugger = false,
@@ -611,6 +616,7 @@ class FlutterRunTestDriver extends FlutterTestDriver {
611616

612617
// Set this up now, but we don't wait it yet. We want to make sure we don't
613618
// miss it while waiting for debugPort below.
619+
final Future<Map<String, Object?>> start = _waitFor(event: 'app.start', timeout: appStartTimeout);
614620
final Future<Map<String, Object?>> started = _waitFor(event: 'app.started', timeout: appStartTimeout);
615621

616622
if (withDebugger) {
@@ -630,9 +636,13 @@ class FlutterRunTestDriver extends FlutterTestDriver {
630636
_attachPort = attachPort;
631637
}
632638

633-
// Now await the started event; if it had already happened the future will
639+
// Now await the start/started events; if it had already happened the future will
634640
// have already completed.
635-
_currentRunningAppId = ((await started)['params'] as Map<String, Object?>?)?['appId'] as String?;
641+
final Map<String, Object?>? startParams = (await start)['params'] as Map<String, Object?>?;
642+
final Map<String, Object?>? startedParams = (await started)['params'] as Map<String, Object?>?;
643+
_currentRunningAppId = startedParams?['appId'] as String?;
644+
_currentRunningDeviceId = startParams?['deviceId'] as String?;
645+
_currentRunningMode = startParams?['mode'] as String?;
636646
prematureExitGuard.complete();
637647
} on Exception catch (error, stackTrace) {
638648
prematureExitGuard.completeError(Exception(error.toString()), stackTrace);

0 commit comments

Comments
 (0)