From c11633e84efa14753e9f6e201a03b9c521b1e3b0 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Fri, 9 Nov 2018 10:33:22 -0800 Subject: [PATCH] Separate hot reload and hot restart capabilities. (#24122) --- .../lib/src/android/android_device.dart | 5 +- .../lib/src/commands/attach.dart | 4 +- .../lib/src/commands/daemon.dart | 2 +- .../flutter_tools/lib/src/commands/run.dart | 4 +- packages/flutter_tools/lib/src/device.dart | 7 +- .../lib/src/fuchsia/fuchsia_device.dart | 5 +- .../flutter_tools/lib/src/ios/devices.dart | 5 +- .../flutter_tools/lib/src/ios/simulators.dart | 5 +- .../lib/src/resident_runner.dart | 11 ++ packages/flutter_tools/lib/src/run_hot.dart | 21 +++- packages/flutter_tools/test/hot_test.dart | 118 ++++++++++++++++-- 11 files changed, 167 insertions(+), 20 deletions(-) diff --git a/packages/flutter_tools/lib/src/android/android_device.dart b/packages/flutter_tools/lib/src/android/android_device.dart index 94b165e963..601b04a02f 100644 --- a/packages/flutter_tools/lib/src/android/android_device.dart +++ b/packages/flutter_tools/lib/src/android/android_device.dart @@ -469,7 +469,10 @@ class AndroidDevice extends Device { } @override - bool get supportsHotMode => true; + bool get supportsHotReload => true; + + @override + bool get supportsHotRestart => true; @override Future stopApp(ApplicationPackage app) { diff --git a/packages/flutter_tools/lib/src/commands/attach.dart b/packages/flutter_tools/lib/src/commands/attach.dart index b2282c1f78..a4d9d6c65a 100644 --- a/packages/flutter_tools/lib/src/commands/attach.dart +++ b/packages/flutter_tools/lib/src/commands/attach.dart @@ -209,7 +209,9 @@ class AttachCommand extends FlutterCommand { } } finally { final List ports = device.portForwarder.forwardedPorts.toList(); - ports.forEach(device.portForwarder.unforward); + for (ForwardedPort port in ports) { + await device.portForwarder.unforward(port); + } } return null; } diff --git a/packages/flutter_tools/lib/src/commands/daemon.dart b/packages/flutter_tools/lib/src/commands/daemon.dart index f8799ae747..5bf05f4e1a 100644 --- a/packages/flutter_tools/lib/src/commands/daemon.dart +++ b/packages/flutter_tools/lib/src/commands/daemon.dart @@ -458,7 +458,7 @@ class AppDomain extends Domain { } bool isRestartSupported(bool enableHotReload, Device device) => - enableHotReload && device.supportsHotMode; + enableHotReload && device.supportsHotRestart; Future _inProgressHotReload; diff --git a/packages/flutter_tools/lib/src/commands/run.dart b/packages/flutter_tools/lib/src/commands/run.dart index 461c4fc16f..cb0e66e41b 100644 --- a/packages/flutter_tools/lib/src/commands/run.dart +++ b/packages/flutter_tools/lib/src/commands/run.dart @@ -336,8 +336,8 @@ class RunCommand extends RunCommandBase { if (hotMode) { for (Device device in devices) { - if (!device.supportsHotMode) - throwToolExit('Hot mode is not supported by ${device.name}. Run with --no-hot.'); + if (!device.supportsHotReload) + throwToolExit('Hot reload is not supported by ${device.name}. Run with --no-hot.'); } } diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index 8225f4fa26..3c6d378e67 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -270,8 +270,11 @@ abstract class Device { bool ipv6 = false, }); - /// Does this device implement support for hot reloading / restarting? - bool get supportsHotMode => true; + /// Whether this device implements support for hot reload. + bool get supportsHotReload => true; + + /// Whether this device implements support for hot restart. + bool get supportsHotRestart => true; /// Stop an app package on the current device. Future stopApp(ApplicationPackage app); diff --git a/packages/flutter_tools/lib/src/fuchsia/fuchsia_device.dart b/packages/flutter_tools/lib/src/fuchsia/fuchsia_device.dart index 42ea084d7f..dba2a3ae92 100644 --- a/packages/flutter_tools/lib/src/fuchsia/fuchsia_device.dart +++ b/packages/flutter_tools/lib/src/fuchsia/fuchsia_device.dart @@ -97,7 +97,10 @@ class FuchsiaDevice extends Device { FuchsiaDevice(String id, { this.name }) : super(id); @override - bool get supportsHotMode => true; + bool get supportsHotReload => true; + + @override + bool get supportsHotRestart => false; @override final String name; diff --git a/packages/flutter_tools/lib/src/ios/devices.dart b/packages/flutter_tools/lib/src/ios/devices.dart index d65689f820..c55cb85337 100644 --- a/packages/flutter_tools/lib/src/ios/devices.dart +++ b/packages/flutter_tools/lib/src/ios/devices.dart @@ -124,7 +124,10 @@ class IOSDevice extends Device { final String _sdkVersion; @override - bool get supportsHotMode => true; + bool get supportsHotReload => true; + + @override + bool get supportsHotRestart => true; @override final String name; diff --git a/packages/flutter_tools/lib/src/ios/simulators.dart b/packages/flutter_tools/lib/src/ios/simulators.dart index 14b8dcb0b7..69c8a1cbaa 100644 --- a/packages/flutter_tools/lib/src/ios/simulators.dart +++ b/packages/flutter_tools/lib/src/ios/simulators.dart @@ -223,7 +223,10 @@ class IOSSimulator extends Device { Future get isLocalEmulator async => true; @override - bool get supportsHotMode => true; + bool get supportsHotReload => true; + + @override + bool get supportsHotRestart => true; Map _logReaders; _IOSSimulatorDevicePortForwarder _portForwarder; diff --git a/packages/flutter_tools/lib/src/resident_runner.dart b/packages/flutter_tools/lib/src/resident_runner.dart index 05c9562092..5be4191696 100644 --- a/packages/flutter_tools/lib/src/resident_runner.dart +++ b/packages/flutter_tools/lib/src/resident_runner.dart @@ -460,6 +460,17 @@ abstract class ResidentRunner { bool get isRunningRelease => debuggingOptions.buildInfo.isRelease; bool get supportsServiceProtocol => isRunningDebug || isRunningProfile; + /// Whether this runner can hot restart. + /// + /// To prevent scenarios where only a subset of devices are hot restarted, + /// the runner requires that all attached devices can support hot restart + /// before enabling it. + bool get canHotRestart { + return flutterDevices.every((FlutterDevice device) { + return device.device.supportsHotRestart; + }); + } + /// Start the app and keep the process running during its lifetime. Future run({ Completer connectionInfoCompleter, diff --git a/packages/flutter_tools/lib/src/run_hot.dart b/packages/flutter_tools/lib/src/run_hot.dart index b0de03aab2..ded7a25649 100644 --- a/packages/flutter_tools/lib/src/run_hot.dart +++ b/packages/flutter_tools/lib/src/run_hot.dart @@ -289,7 +289,16 @@ class HotRunner extends ResidentRunner { Future handleTerminalCommand(String code) async { final String lower = code.toLowerCase(); if (lower == 'r') { - final OperationResult result = await restart(fullRestart: code == 'R'); + OperationResult result; + if (code == 'R') { + // If hot restart is not supported for all devices, ignore the command. + if (!canHotRestart) { + return; + } + result = await restart(fullRestart: true); + } else { + result = await restart(fullRestart: false); + } if (!result.isOk) { // TODO(johnmccutchan): Attempt to determine the number of errors that // occurred and tighten this message. @@ -541,6 +550,9 @@ class HotRunner extends ResidentRunner { Future restart({ bool fullRestart = false, bool pauseAfterRestart = false, String reason }) async { final Stopwatch timer = Stopwatch()..start(); if (fullRestart) { + if (!canHotRestart) { + return OperationResult(1, 'hotRestart not supported'); + } final Status status = logger.startProgress( 'Performing hot restart...', progressId: 'hot.restart', @@ -780,9 +792,12 @@ class HotRunner extends ResidentRunner { @override void printHelp({ @required bool details }) { const String fire = '🔥'; + String rawMessage = ' To hot reload changes while running, press "r". '; + if (canHotRestart) { + rawMessage += 'To hot restart (and rebuild state), press "R".'; + } final String message = terminal.color( - fire + terminal.bolden(' To hot reload changes while running, press "r". ' - 'To hot restart (and rebuild state), press "R".'), + fire + terminal.bolden(rawMessage), TerminalColor.red, ); printStatus(message); diff --git a/packages/flutter_tools/test/hot_test.dart b/packages/flutter_tools/test/hot_test.dart index 4ee6ec4b68..a6f70b90ba 100644 --- a/packages/flutter_tools/test/hot_test.dart +++ b/packages/flutter_tools/test/hot_test.dart @@ -5,6 +5,7 @@ import 'dart:async'; import 'package:flutter_tools/src/artifacts.dart'; +import 'package:flutter_tools/src/devfs.dart'; import 'package:flutter_tools/src/device.dart'; import 'package:flutter_tools/src/resident_runner.dart'; import 'package:flutter_tools/src/run_hot.dart'; @@ -94,32 +95,113 @@ void main() { group('hotRestart', () { final MockResidentCompiler residentCompiler = MockResidentCompiler(); + final MockDevFs mockDevFs = MockDevFs(); MockLocalEngineArtifacts mockArtifacts; + when(mockDevFs.update( + mainPath: anyNamed('mainPath'), + target: anyNamed('target'), + bundle: anyNamed('bundle'), + firstBuildTime: anyNamed('firstBuildTime'), + bundleFirstUpload: anyNamed('bundleFirstUpload'), + bundleDirty: anyNamed('bundleDirty'), + fileFilter: anyNamed('fileFilter'), + generator: anyNamed('generator'), + fullRestart: anyNamed('fullRestart'), + dillOutputPath: anyNamed('dillOutputPath'), + trackWidgetCreation: anyNamed('trackWidgetCreation'), + projectRootPath: anyNamed('projectRootPath'), + pathToReload: anyNamed('pathToReload'), + )).thenAnswer((Invocation _) => Future.value(1000)); + when(mockDevFs.assetPathsToEvict).thenReturn(Set()); + when(mockDevFs.baseUri).thenReturn(Uri.file('test')); + setUp(() { mockArtifacts = MockLocalEngineArtifacts(); when(mockArtifacts.getArtifactPath(Artifact.flutterPatchedSdkPath)).thenReturn('some/path'); }); testUsingContext('no setup', () async { - final List devices = [FlutterDevice(MockDevice(), generator: residentCompiler, trackWidgetCreation: false)]; + final MockDevice mockDevice = MockDevice(); + when(mockDevice.supportsHotReload).thenReturn(true); + when(mockDevice.supportsHotRestart).thenReturn(true); + final List devices = [ + FlutterDevice(mockDevice, generator: residentCompiler, trackWidgetCreation: false), + ]; expect((await HotRunner(devices).restart(fullRestart: true)).isOk, false); }, overrides: { Artifacts: () => mockArtifacts, }); - testUsingContext('setup function succeeds', () async { - final List devices = [FlutterDevice(MockDevice(), generator: residentCompiler, trackWidgetCreation: false)]; + testUsingContext('Does not hot restart when device does not support it', () async { + // Setup mocks + final MockDevice mockDevice = MockDevice(); + when(mockDevice.supportsHotReload).thenReturn(true); + when(mockDevice.supportsHotRestart).thenReturn(false); + // Trigger hot restart. + final List devices = [ + FlutterDevice(mockDevice, generator: residentCompiler, trackWidgetCreation: false)..devFS = mockDevFs + ]; final OperationResult result = await HotRunner(devices).restart(fullRestart: true); + // Expect hot restart failed. expect(result.isOk, false); - expect(result.message, isNot('setupHotRestart failed')); + expect(result.message, 'hotRestart not supported'); }, overrides: { Artifacts: () => mockArtifacts, - HotRunnerConfig: () => TestHotRunnerConfig(successfulSetup: true), + HotRunnerConfig: () => TestHotRunnerConfig(successfulSetup: true, computeDartDependencies: false), + }); + + testUsingContext('Does not hot restart when one of many devices does not support it', () async { + // Setup mocks + final MockDevice mockDevice = MockDevice(); + final MockDevice mockHotDevice = MockDevice(); + when(mockDevice.supportsHotReload).thenReturn(true); + when(mockDevice.supportsHotRestart).thenReturn(false); + when(mockHotDevice.supportsHotReload).thenReturn(true); + when(mockHotDevice.supportsHotRestart).thenReturn(true); + // Trigger hot restart. + final List devices = [ + FlutterDevice(mockDevice, generator: residentCompiler, trackWidgetCreation: false)..devFS = mockDevFs, + FlutterDevice(mockHotDevice, generator: residentCompiler, trackWidgetCreation: false)..devFS = mockDevFs, + ]; + final OperationResult result = await HotRunner(devices).restart(fullRestart: true); + // Expect hot restart failed. + expect(result.isOk, false); + expect(result.message, 'hotRestart not supported'); + }, overrides: { + Artifacts: () => mockArtifacts, + HotRunnerConfig: () => TestHotRunnerConfig(successfulSetup: true, computeDartDependencies: false), + }); + + testUsingContext('Does hot restarts when all devices support it', () async { + // Setup mocks + final MockDevice mockDevice = MockDevice(); + final MockDevice mockHotDevice = MockDevice(); + when(mockDevice.supportsHotReload).thenReturn(true); + when(mockDevice.supportsHotRestart).thenReturn(true); + when(mockHotDevice.supportsHotReload).thenReturn(true); + when(mockHotDevice.supportsHotRestart).thenReturn(true); + // Trigger a restart. + final List devices = [ + FlutterDevice(mockDevice, generator: residentCompiler, trackWidgetCreation: false)..devFS = mockDevFs, + FlutterDevice(mockHotDevice, generator: residentCompiler, trackWidgetCreation: false)..devFS = mockDevFs, + ]; + final OperationResult result = await HotRunner(devices).restart(fullRestart: true); + // Expect hot restart was successful. + expect(result.isOk, true); + expect(result.message, isNot('hotRestart not supported')); + }, overrides: { + Artifacts: () => mockArtifacts, + HotRunnerConfig: () => TestHotRunnerConfig(successfulSetup: true, computeDartDependencies: false), }); testUsingContext('setup function fails', () async { - final List devices = [FlutterDevice(MockDevice(), generator: residentCompiler, trackWidgetCreation: false)]; + final MockDevice mockDevice = MockDevice(); + when(mockDevice.supportsHotReload).thenReturn(true); + when(mockDevice.supportsHotRestart).thenReturn(true); + final List devices = [ + FlutterDevice(mockDevice, generator: residentCompiler, trackWidgetCreation: false) + ]; final OperationResult result = await HotRunner(devices).restart(fullRestart: true); expect(result.isOk, false); expect(result.message, 'setupHotRestart failed'); @@ -127,9 +209,29 @@ void main() { Artifacts: () => mockArtifacts, HotRunnerConfig: () => TestHotRunnerConfig(successfulSetup: false), }); + + testUsingContext('hot restart supported', () async { + // Setup mocks + final MockDevice mockDevice = MockDevice(); + when(mockDevice.supportsHotReload).thenReturn(true); + when(mockDevice.supportsHotRestart).thenReturn(true); + // Trigger hot restart. + final List devices = [ + FlutterDevice(mockDevice, generator: residentCompiler, trackWidgetCreation: false)..devFS = mockDevFs + ]; + final OperationResult result = await HotRunner(devices).restart(fullRestart: true); + // Expect hot restart successful. + expect(result.isOk, true); + expect(result.message, isNot('setupHotRestart failed')); + }, overrides: { + Artifacts: () => mockArtifacts, + HotRunnerConfig: () => TestHotRunnerConfig(successfulSetup: true, computeDartDependencies: false), + }); }); } +class MockDevFs extends Mock implements DevFS {} + class MockLocalEngineArtifacts extends Mock implements LocalEngineArtifacts {} class MockDevice extends Mock implements Device { @@ -139,7 +241,9 @@ class MockDevice extends Mock implements Device { } class TestHotRunnerConfig extends HotRunnerConfig { - TestHotRunnerConfig({@required this.successfulSetup}); + TestHotRunnerConfig({@required this.successfulSetup, bool computeDartDependencies = true}) { + this.computeDartDependencies = computeDartDependencies; + } bool successfulSetup;