From 6d37867c4a4b443ba543dfe31bbdd965b1119485 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 11 Sep 2019 12:57:43 -0700 Subject: [PATCH] Place hot reload artifacts in a temp directory (#40171) --- .../lib/src/resident_runner.dart | 36 +++++++++++-------- packages/flutter_tools/lib/src/run_cold.dart | 1 + packages/flutter_tools/lib/src/run_hot.dart | 1 + .../general.shard/resident_runner_test.dart | 14 ++++++++ 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/packages/flutter_tools/lib/src/resident_runner.dart b/packages/flutter_tools/lib/src/resident_runner.dart index 770253d623..b373751c88 100644 --- a/packages/flutter_tools/lib/src/resident_runner.dart +++ b/packages/flutter_tools/lib/src/resident_runner.dart @@ -536,34 +536,33 @@ abstract class ResidentRunner { this.ipv6, this.stayResident = true, this.hotMode = true, - this.dillOutputPath, + String dillOutputPath, }) : mainPath = findMainDartFile(target), projectRootPath = projectRootPath ?? fs.currentDirectory.path, packagesFilePath = packagesFilePath ?? fs.path.absolute(PackageMap.globalPackagesPath), + _dillOutputPath = dillOutputPath, + artifactDirectory = dillOutputPath == null + ? fs.systemTempDirectory.createTempSync('_fluttter_tool') + : fs.file(dillOutputPath).parent, assetBundle = AssetBundleFactory.instance.createBundle() { - // TODO(jonahwilliams): this is transitionary logic to allow us to support - // platforms that are not yet using flutter assemble. In the "new world", - // builds are isolated based on a number of factors. Thus, we cannot assume - // that a debug build will create the expected `build/app.dill` file. For - // now, I'm working around this by just creating it if it is missing here. - // In the future, once build & run are more strongly separated, the build - // environment will be plumbed through so that it all comes from a single - // source of truth, the [Environment]. - final File dillOutput = fs.file(dillOutputPath ?? fs.path.join('build', 'app.dill')); - if (!dillOutput.existsSync()) { - dillOutput.createSync(recursive: true); + if (!artifactDirectory.existsSync()) { + artifactDirectory.createSync(recursive: true); } } @protected @visibleForTesting final List flutterDevices; + final String target; final DebuggingOptions debuggingOptions; final bool stayResident; final bool ipv6; + final String _dillOutputPath; + /// The parent location of the incremental artifacts. + @visibleForTesting + final Directory artifactDirectory; final Completer _finished = Completer(); - final String dillOutputPath; final String packagesFilePath; final String projectRootPath; final String mainPath; @@ -571,6 +570,8 @@ abstract class ResidentRunner { bool _exited = false; bool hotMode ; + + String get dillOutputPath => _dillOutputPath ?? fs.path.join(artifactDirectory.path, 'app.dill'); String getReloadPath({ bool fullRestart }) => mainPath + (fullRestart ? '' : '.incremental') + '.dill'; bool get debuggingEnabled => debuggingOptions.debuggingEnabled; @@ -716,6 +717,7 @@ abstract class ResidentRunner { /// Throws an [AssertionError] if [Devce.supportsScreenshot] is not true. Future screenshot(FlutterDevice device) async { assert(device.device.supportsScreenshot); + final Status status = logger.startProgress('Taking screenshot for ${device.device.name}...', timeout: timeoutConfiguration.fastOperation); final File outputFile = getUniqueFile(fs.currentDirectory, 'flutter', 'png'); try { @@ -850,7 +852,13 @@ abstract class ResidentRunner { return exitCode; } - Future preExit() async { } + @mustCallSuper + Future preExit() async { + // If _dillOutputPath is null, we created a temporary directory for the dill. + if (_dillOutputPath == null && artifactDirectory.existsSync()) { + artifactDirectory.deleteSync(recursive: true); + } + } Future exitApp() async { final List> futures = >[]; diff --git a/packages/flutter_tools/lib/src/run_cold.dart b/packages/flutter_tools/lib/src/run_cold.dart index b621c782bd..ddfde2e25b 100644 --- a/packages/flutter_tools/lib/src/run_cold.dart +++ b/packages/flutter_tools/lib/src/run_cold.dart @@ -206,5 +206,6 @@ class ColdRunner extends ResidentRunner { if (device.vmServices == null || device.vmServices.isEmpty) await device.device.stopApp(device.package); } + await super.preExit(); } } diff --git a/packages/flutter_tools/lib/src/run_hot.dart b/packages/flutter_tools/lib/src/run_hot.dart index 3be32536c2..f9b590a489 100644 --- a/packages/flutter_tools/lib/src/run_hot.dart +++ b/packages/flutter_tools/lib/src/run_hot.dart @@ -1010,6 +1010,7 @@ class HotRunner extends ResidentRunner { Future preExit() async { await _cleanupDevFS(); await hotRunnerConfig.runPreShutdownOperations(); + await super.preExit(); } @override 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 9a1a7f74ad..85cdca126a 100644 --- a/packages/flutter_tools/test/general.shard/resident_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_runner_test.dart @@ -298,6 +298,20 @@ void main() { Usage: () => MockUsage(), })); + test('ResidentRunner uses temp directory when there is no output dill path', () => testbed.run(() { + expect(residentRunner.artifactDirectory.path, contains('_fluttter_tool')); + + final ResidentRunner otherRunner = HotRunner( + [ + mockFlutterDevice, + ], + stayResident: false, + debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug), + dillOutputPath: fs.path.join('foobar', 'app.dill'), + ); + expect(otherRunner.artifactDirectory.path, contains('foobar')); + })); + test('ResidentRunner printHelpDetails', () => testbed.run(() { when(mockDevice.supportsHotRestart).thenReturn(true); when(mockDevice.supportsScreenshot).thenReturn(true);