diff --git a/packages/flutter_tools/lib/src/bundle.dart b/packages/flutter_tools/lib/src/bundle.dart index 6b0de883df..cca243e980 100644 --- a/packages/flutter_tools/lib/src/bundle.dart +++ b/packages/flutter_tools/lib/src/bundle.dart @@ -4,6 +4,8 @@ import 'dart:async'; +import 'package:convert/convert.dart'; +import 'package:crypto/crypto.dart'; import 'package:meta/meta.dart'; import 'package:pool/pool.dart'; @@ -17,6 +19,7 @@ import 'build_system/depfile.dart'; import 'build_system/targets/common.dart'; import 'build_system/targets/icon_tree_shaker.dart'; import 'cache.dart'; +import 'convert.dart'; import 'dart/package_map.dart'; import 'devfs.dart'; import 'globals.dart' as globals; @@ -34,9 +37,20 @@ String getDefaultApplicationKernelPath({ @required bool trackWidgetCreation }) { ); } -String getDefaultCachedKernelPath({ @required bool trackWidgetCreation }) { +String getDefaultCachedKernelPath({ + @required bool trackWidgetCreation, + @required List dartDefines, +}) { + final StringBuffer buffer = StringBuffer(); + buffer.writeAll(dartDefines); + String buildPrefix = ''; + if (buffer.isNotEmpty) { + final String output = buffer.toString(); + final Digest digest = md5.convert(utf8.encode(output)); + buildPrefix = '${hex.encode(digest.bytes)}.'; + } return getKernelPathForTransformerOptions( - globals.fs.path.join(getBuildDirectory(), 'cache.dill'), + globals.fs.path.join(getBuildDirectory(), '${buildPrefix}cache.dill'), trackWidgetCreation: trackWidgetCreation, ); } diff --git a/packages/flutter_tools/lib/src/resident_runner.dart b/packages/flutter_tools/lib/src/resident_runner.dart index 573467bbcd..992b82dd32 100644 --- a/packages/flutter_tools/lib/src/resident_runner.dart +++ b/packages/flutter_tools/lib/src/resident_runner.dart @@ -104,6 +104,7 @@ class FlutterDevice { globals.printTrace(message), initializeFromDill: getDefaultCachedKernelPath( trackWidgetCreation: buildInfo.trackWidgetCreation, + dartDefines: buildInfo.dartDefines, ), targetModel: TargetModel.dartdevc, extraFrontEndOptions: buildInfo.extraFrontEndOptions, @@ -131,6 +132,7 @@ class FlutterDevice { extraFrontEndOptions: buildInfo.extraFrontEndOptions, initializeFromDill: getDefaultCachedKernelPath( trackWidgetCreation: buildInfo.trackWidgetCreation, + dartDefines: buildInfo.dartDefines, ), packagesPath: globalPackagesPath, ); @@ -1062,6 +1064,7 @@ abstract class ResidentRunner { if (outputDill.existsSync()) { final String copyPath = getDefaultCachedKernelPath( trackWidgetCreation: trackWidgetCreation, + dartDefines: debuggingOptions.buildInfo.dartDefines, ); globals.fs .file(copyPath) 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 7737f6cb99..9bd3eb3f5a 100644 --- a/packages/flutter_tools/test/general.shard/resident_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_runner_test.dart @@ -1165,6 +1165,40 @@ void main() { expect(await globals.fs.file(globals.fs.path.join('build', 'cache.dill')).readAsString(), 'ABC'); })); + testUsingContext('HotRunner copies compiled app.dill to cache during startup with dart defines', () => testbed.run(() async { + fakeVmServiceHost = FakeVmServiceHost(requests: [ + listViews, + listViews, + ]); + setWsAddress(testUri, fakeVmServiceHost.vmService); + globals.fs.file(globals.fs.path.join('lib', 'main.dart')).createSync(recursive: true); + residentRunner = HotRunner( + [ + mockFlutterDevice, + ], + stayResident: false, + debuggingOptions: DebuggingOptions.enabled( + const BuildInfo( + BuildMode.debug, + '', + treeShakeIcons: false, + dartDefines: ['a', 'b'], + ) + ), + ); + residentRunner.artifactDirectory.childFile('app.dill').writeAsStringSync('ABC'); + when(mockFlutterDevice.runHot( + hotRunner: anyNamed('hotRunner'), + route: anyNamed('route'), + )).thenAnswer((Invocation invocation) async { + return 0; + }); + await residentRunner.run(); + + expect(await globals.fs.file(globals.fs.path.join( + 'build', '187ef4436122d1cc2f40dc2b92f0eba0.cache.dill')).readAsString(), 'ABC'); + })); + testUsingContext('HotRunner does not copy app.dill if a dillOutputPath is given', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ listViews,