From 37eec862944aec29816a08931d5d94e6153696b4 Mon Sep 17 00:00:00 2001 From: Danny Tuppeny Date: Tue, 19 Nov 2019 20:11:53 +0000 Subject: [PATCH] Allow web server device to use extension if started with --start-paused (#44263) * Allow web server device to use extension if started with --start-paused * Fix comment * Remove browser-launch option that allowed using Chrome without launching Chrome The web-server device should now be used if you don't want to launch a browser. * Add tests for WebServerDevice with --start-paused * Fixes from rebase * More fixes from rebase * Fix from rebase * Fix condition for page refresh in non-debug builds * Make dwds conditions match with a new getter * Add missing .device --- .../src/build_runner/resident_web_runner.dart | 22 ++++++---- .../lib/src/build_runner/web_fs.dart | 9 ++-- .../flutter_tools/lib/src/commands/run.dart | 1 - packages/flutter_tools/lib/src/device.dart | 3 -- .../lib/src/runner/flutter_command.dart | 8 ---- .../flutter_tools/lib/src/web/web_device.dart | 23 +++++----- .../resident_web_runner_test.dart | 42 ++++++++++++++++++- 7 files changed, 69 insertions(+), 39 deletions(-) diff --git a/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart b/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart index a041b86806..856e25a3ab 100644 --- a/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart +++ b/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart @@ -96,12 +96,15 @@ abstract class ResidentWebRunner extends ResidentRunner { // Only the debug builds of the web support the service protocol. @override - bool get supportsServiceProtocol => - isRunningDebug && device.device is! WebServerDevice; + bool get supportsServiceProtocol => isRunningDebug && deviceIsDebuggable; @override - bool get debuggingEnabled => - isRunningDebug && device.device is! WebServerDevice; + bool get debuggingEnabled => isRunningDebug && deviceIsDebuggable; + + /// WebServer device is debuggable when running with --start-paused. + bool get deviceIsDebuggable => device.device is! WebServerDevice || debuggingOptions.startPaused; + + bool get _enableDwds => debuggingEnabled; WebFs _webFs; ConnectionResult _connectionResult; @@ -602,14 +605,14 @@ class _DwdsResidentWebRunner extends ResidentWebRunner { initializePlatform: debuggingOptions.initializePlatform, hostname: debuggingOptions.hostname, port: debuggingOptions.port, - skipDwds: device.device is WebServerDevice || !debuggingOptions.buildInfo.isDebug, + skipDwds: !_enableDwds, dartDefines: dartDefines, ); // When connecting to a browser, update the message with a seemsSlow notification // to handle the case where we fail to connect. buildStatus.stop(); statusActive = false; - if (debuggingOptions.browserLaunch && supportsServiceProtocol) { + if (supportsServiceProtocol) { buildStatus = logger.startProgress( 'Attempting to connect to browser instance..', timeout: const Duration(seconds: 30), @@ -624,8 +627,9 @@ class _DwdsResidentWebRunner extends ResidentWebRunner { 'uri': _webFs.uri, }, ); - if (supportsServiceProtocol) { - _connectionResult = await _webFs.connect(debuggingOptions); + if (_enableDwds) { + final bool useDebugExtension = device.device is WebServerDevice && debuggingOptions.startPaused; + _connectionResult = await _webFs.connect(useDebugExtension); unawaited(_connectionResult.debugConnection.onDone.whenComplete(_cleanupAndExit)); } if (statusActive) { @@ -741,7 +745,7 @@ class _DwdsResidentWebRunner extends ResidentWebRunner { } } // Allows browser refresh hot restart on non-debug builds. - if (device is ChromeDevice && debuggingOptions.browserLaunch) { + if (device.device is ChromeDevice && !isRunningDebug) { try { final Chrome chrome = await ChromeLauncher.connectedInstance; final ChromeTab chromeTab = await chrome.chromeConnection.getTab((ChromeTab chromeTab) { diff --git a/packages/flutter_tools/lib/src/build_runner/web_fs.dart b/packages/flutter_tools/lib/src/build_runner/web_fs.dart index 509c076870..ec2645a6a7 100644 --- a/packages/flutter_tools/lib/src/build_runner/web_fs.dart +++ b/packages/flutter_tools/lib/src/build_runner/web_fs.dart @@ -33,7 +33,6 @@ import '../bundle.dart'; import '../cache.dart'; import '../dart/package_map.dart'; import '../dart/pub.dart'; -import '../device.dart'; import '../globals.dart'; import '../platform_plugins.dart'; import '../plugins.dart'; @@ -131,12 +130,12 @@ class WebFs { /// Connect and retrieve the [DebugConnection] for the current application. /// /// Only calls [AppConnection.runMain] on the subsequent connections. - Future connect(DebuggingOptions debuggingOptions) { + Future connect(bool useDebugExtension) { final Completer firstConnection = Completer(); _connectedApps = _dwds.connectedApps.listen((AppConnection appConnection) async { - final DebugConnection debugConnection = debuggingOptions.browserLaunch - ? await _dwds.debugConnection(appConnection) - : await (_cachedExtensionFuture ??= _dwds.extensionDebugConnections.stream.first); + final DebugConnection debugConnection = useDebugExtension + ? await (_cachedExtensionFuture ??= _dwds.extensionDebugConnections.stream.first) + : await _dwds.debugConnection(appConnection); if (!firstConnection.isCompleted) { firstConnection.complete(ConnectionResult(appConnection, debugConnection)); } else { diff --git a/packages/flutter_tools/lib/src/commands/run.dart b/packages/flutter_tools/lib/src/commands/run.dart index aeafa6b018..6f40434f9a 100644 --- a/packages/flutter_tools/lib/src/commands/run.dart +++ b/packages/flutter_tools/lib/src/commands/run.dart @@ -321,7 +321,6 @@ class RunCommand extends RunCommandBase { initializePlatform: boolArg('web-initialize-platform'), hostname: featureFlags.isWebEnabled ? stringArg('web-hostname') : '', port: featureFlags.isWebEnabled ? stringArg('web-port') : '', - browserLaunch: featureFlags.isWebEnabled ? boolArg('web-browser-launch') : null, vmserviceOutFile: stringArg('vmservice-out-file'), ); } diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index fe268494fd..7e8e4e51b5 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -506,7 +506,6 @@ class DebuggingOptions { this.initializePlatform = true, this.hostname, this.port, - this.browserLaunch = true, this.vmserviceOutFile, }) : debuggingEnabled = true; @@ -524,7 +523,6 @@ class DebuggingOptions { verboseSystemLogs = false, hostVmServicePort = null, deviceVmServicePort = null, - browserLaunch = true, vmserviceOutFile = null; final bool debuggingEnabled; @@ -547,7 +545,6 @@ class DebuggingOptions { final int deviceVmServicePort; final String port; final String hostname; - final bool browserLaunch; /// A file where the vmservice uri should be written after the application is started. final String vmserviceOutFile; diff --git a/packages/flutter_tools/lib/src/runner/flutter_command.dart b/packages/flutter_tools/lib/src/runner/flutter_command.dart index 672007c084..3686e045db 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command.dart @@ -150,14 +150,6 @@ abstract class FlutterCommand extends Command { 'will select a random open port on the host.', hide: hide, ); - argParser.addFlag('web-browser-launch', - defaultsTo: true, - negatable: true, - help: 'Whether to automatically launch browsers for web devices ' - 'that do so. Setting this to true allows using the Dart debug extension ' - 'on Chrome and other browsers which support extensions.', - hide: hide, - ); } void usesTargetOption() { diff --git a/packages/flutter_tools/lib/src/web/web_device.dart b/packages/flutter_tools/lib/src/web/web_device.dart index e981e1c04c..34c72f2023 100644 --- a/packages/flutter_tools/lib/src/web/web_device.dart +++ b/packages/flutter_tools/lib/src/web/web_device.dart @@ -134,16 +134,13 @@ class ChromeDevice extends Device { // See [ResidentWebRunner.run] in flutter_tools/lib/src/resident_web_runner.dart // for the web initialization and server logic. final String url = platformArgs['uri'] as String; - if (debuggingOptions.browserLaunch) { - _chrome = await chromeLauncher.launch(url, - dataDir: fs.currentDirectory - .childDirectory('.dart_tool') - .childDirectory('chrome-device')); - logger.sendEvent('app.webLaunchUrl', {'url': url, 'launched': true}); - } else { - printStatus('Waiting for connection from Dart debug extension at $url', emphasis: true); - logger.sendEvent('app.webLaunchUrl', {'url': url, 'launched': false}); - } + _chrome = await chromeLauncher.launch(url, + dataDir: fs.currentDirectory + .childDirectory('.dart_tool') + .childDirectory('chrome-device')); + + logger.sendEvent('app.webLaunchUrl', {'url': url, 'launched': true}); + return LaunchResult.succeeded(observatoryUri: null); } @@ -253,7 +250,11 @@ class WebServerDevice extends Device { bool ipv6 = false, }) async { final String url = platformArgs['uri'] as String; - printStatus('$mainPath is being served at $url', emphasis: true); + if (debuggingOptions.startPaused) { + printStatus('Waiting for connection from Dart debug extension at $url', emphasis: true); + } else { + printStatus('$mainPath is being served at $url', emphasis: true); + } logger.sendEvent('app.webLaunchUrl', {'url': url, 'launched': false}); return LaunchResult.succeeded(observatoryUri: null); } diff --git a/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart index 0b03316148..a6d701bf58 100644 --- a/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart @@ -130,7 +130,7 @@ void main() { when(mockWipConnection.debugger).thenReturn(mockWipDebugger); } - test('runner with web server device does not support debugging', () => testbed.run(() async { + test('runner with web server device does not support debugging without --start-paused', () => testbed.run(() { when(mockFlutterDevice.device).thenReturn(WebServerDevice()); final ResidentRunner profileResidentWebRunner = DwdsWebRunnerFactory().createWebRunner( mockFlutterDevice, @@ -144,7 +144,6 @@ void main() { expect(profileResidentWebRunner.debuggingEnabled, false); when(mockFlutterDevice.device).thenReturn(MockChromeDevice()); - expect(residentWebRunner.debuggingEnabled, true); })); @@ -161,6 +160,45 @@ void main() { expect(didSkipDwds, true); })); + test('runner with web server device supports debugging with --start-paused', () => testbed.run(() { + when(mockFlutterDevice.device).thenReturn(WebServerDevice()); + final ResidentRunner profileResidentWebRunner = DwdsWebRunnerFactory().createWebRunner( + mockFlutterDevice, + flutterProject: FlutterProject.current(), + debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug, startPaused: true), + ipv6: true, + stayResident: true, + dartDefines: [], + ); + + expect(profileResidentWebRunner.debuggingEnabled, true); + })); + + test('runner with web server device uses debug extension with --start-paused', () => testbed.run(() async { + _setupMocks(); + when(mockFlutterDevice.device).thenReturn(WebServerDevice()); + final BufferLogger bufferLogger = logger; + + final ResidentWebRunner runner = DwdsWebRunnerFactory().createWebRunner( + mockFlutterDevice, + flutterProject: FlutterProject.current(), + debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug, startPaused: true), + ipv6: true, + stayResident: true, + dartDefines: [], + ); + + final Completer connectionInfoCompleter = Completer(); + unawaited(runner.run( + connectionInfoCompleter: connectionInfoCompleter, + )); + await connectionInfoCompleter.future; + + // Check connect() was told to use the debug extension. + verify(mockWebFs.connect(true)).called(1); + // And ensure the debug services was started. + expect(bufferLogger.statusText, contains('Debug service listening on')); + })); test('profile does not supportsServiceProtocol', () => testbed.run(() { when(mockFlutterDevice.device).thenReturn(mockChromeDevice);