[flutter_tools] include dart-defines in cached kernel name (#59083)

This prevents using a cached kernel file with different defines, since --initialize-from-dill does not handle this correctly.

Fixes #58976
This commit is contained in:
Jonah Williams 2020-06-09 12:03:28 -07:00 committed by GitHub
parent 442422f0ac
commit e54f8f523f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 2 deletions

View file

@ -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<String> 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,
);
}

View file

@ -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)

View file

@ -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: <VmServiceExpectation>[
listViews,
listViews,
]);
setWsAddress(testUri, fakeVmServiceHost.vmService);
globals.fs.file(globals.fs.path.join('lib', 'main.dart')).createSync(recursive: true);
residentRunner = HotRunner(
<FlutterDevice>[
mockFlutterDevice,
],
stayResident: false,
debuggingOptions: DebuggingOptions.enabled(
const BuildInfo(
BuildMode.debug,
'',
treeShakeIcons: false,
dartDefines: <String>['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: <VmServiceExpectation>[
listViews,