Skip to content

Commit 1d3e70c

Browse files
authored
[native_assets_builder] Fix hook compile caching (#1933)
The `package_config.json` can update with irrelevant information (such as the timestamp). Don't recompile the hooks if only the timestamp changed. Testing: The existing tests started failing. Possibly after `dartdev` started running `pub get` more often automatically after https://dart-review.googlesource.com/c/sdk/+/404583.
1 parent ca5f3d2 commit 1d3e70c

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

pkgs/native_assets_builder/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.11.1
2+
3+
- Don't recompile hooks on `package_config.json` having an updated timestamp.
4+
15
## 0.11.0
26

37
- **Breaking change** Complete overhaul of the use of `NativeAssetsBuildRunner`

pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@ class NativeAssetsBuildRunner {
310310
_HookValidator validator,
311311
Uri? resources,
312312
) async {
313-
final packageConfigUri = packageLayout.packageConfigUri;
314313
final outDir = input.outputDirectory;
315314
return await runUnderDirectoriesLock(
316315
_fileSystem,
@@ -325,7 +324,6 @@ class NativeAssetsBuildRunner {
325324
input.packageName,
326325
input.outputDirectory,
327326
input.packageRoot.resolve('hook/${hook.scriptName}'),
328-
packageConfigUri,
329327
);
330328
if (hookCompileResult == null) {
331329
return null;
@@ -382,7 +380,6 @@ ${e.message}
382380
hook,
383381
input,
384382
validator,
385-
packageConfigUri,
386383
resources,
387384
hookKernelFile,
388385
hookEnvironment,
@@ -432,7 +429,6 @@ ${e.message}
432429
Hook hook,
433430
HookInput input,
434431
_HookValidator validator,
435-
Uri packageConfigUri,
436432
Uri? resources,
437433
File hookKernelFile,
438434
Map<String, String> environment,
@@ -458,7 +454,7 @@ ${e.message}
458454
}
459455

460456
final arguments = [
461-
'--packages=${packageConfigUri.toFilePath()}',
457+
'--packages=${packageLayout.packageConfigUri.toFilePath()}',
462458
hookKernelFile.path,
463459
'--config=${inputFile.toFilePath()}',
464460
if (resources != null) resources.toFilePath(),
@@ -584,10 +580,12 @@ ${e.message}
584580
String packageName,
585581
Uri outputDirectory,
586582
Uri scriptUri,
587-
Uri packageConfigUri,
588583
) async {
589584
// Don't invalidate cache with environment changes.
590585
final environmentForCaching = <String, String>{};
586+
final packageConfigHashable =
587+
outputDirectory.resolve('../package_config_hashable.json');
588+
await _makeHashablePackageConfig(packageConfigHashable);
591589
final kernelFile = _fileSystem.file(
592590
outputDirectory.resolve('../hook.dill'),
593591
);
@@ -620,7 +618,6 @@ ${e.message}
620618
final success = await _compileHookForPackage(
621619
packageName,
622620
scriptUri,
623-
packageConfigUri,
624621
kernelFile,
625622
depFile,
626623
);
@@ -629,9 +626,11 @@ ${e.message}
629626
}
630627

631628
final dartSources = await _readDepFile(depFile);
629+
632630
final modifiedDuringBuild = await dependenciesHashes.hashDependencies(
633631
[
634-
...dartSources,
632+
...dartSources.where((e) => e != packageLayout.packageConfigUri),
633+
packageConfigHashable,
635634
// If the Dart version changed, recompile.
636635
dartExecutable.resolve('../version'),
637636
],
@@ -645,22 +644,31 @@ ${e.message}
645644
return (kernelFile, dependenciesHashes);
646645
}
647646

647+
Future<void> _makeHashablePackageConfig(Uri uri) async {
648+
final contents =
649+
await _fileSystem.file(packageLayout.packageConfigUri).readAsString();
650+
final jsonData = jsonDecode(contents) as Map<String, Object?>;
651+
jsonData.remove('generated');
652+
final contentsSanitized =
653+
const JsonEncoder.withIndent(' ').convert(jsonData);
654+
await _fileSystem.file(uri).writeAsString(contentsSanitized);
655+
}
656+
648657
Future<bool> _compileHookForPackage(
649658
String packageName,
650659
Uri scriptUri,
651-
Uri packageConfigUri,
652660
File kernelFile,
653661
File depFile,
654662
) async {
655663
final compileArguments = [
656664
'compile',
657665
'kernel',
658-
'--packages=${packageConfigUri.toFilePath()}',
666+
'--packages=${packageLayout.packageConfigUri.toFilePath()}',
659667
'--output=${kernelFile.path}',
660668
'--depfile=${depFile.path}',
661669
scriptUri.toFilePath(),
662670
];
663-
final workingDirectory = packageConfigUri.resolve('../');
671+
final workingDirectory = packageLayout.packageConfigUri.resolve('../');
664672
final compileResult = await runProcess(
665673
filesystem: _fileSystem,
666674
workingDirectory: workingDirectory,

pkgs/native_assets_builder/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: native_assets_builder
22
description: >-
33
This package is the backend that invokes build hooks.
4-
version: 0.11.0
4+
version: 0.11.1
55
repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_builder
66

77
# publish_to: none

0 commit comments

Comments
 (0)