Skip to content

Commit e165366

Browse files
authored
[flutter_tools] Prevent deletion of shared native asset hooks outputs as stale (flutter#191272)
When switching branches or build configurations where native assets hooks are not used, the build system's stale-output cleanup was deleting shared hook outputs located in `.dart_tool/hooks_runner/shared/`. This caused subsequent builds that *do* use native assets hooks to fail because the hook runner skipped execution (since metadata was up-to-date) while the actual binary files on disk had been deleted. This change introduces a helper function `_deleteStaleOutputs` to preserve files in `.dart_tool/hooks_runner/shared/` during stale output cleanup in both `FlutterBuildSystem.trackSharedBuildDirectory` and `_BuildInstance._invokeInternal`. Fixes flutter#191020 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 46c1609 commit e165366

2 files changed

Lines changed: 140 additions & 19 deletions

File tree

‎packages/flutter_tools/lib/src/build_system/build_system.dart‎

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,6 @@ class FlutterBuildSystem extends BuildSystem {
788788
/// files, if these were not already covered by the built-in cleanup. This
789789
/// cleanup is only necessary when multiple different build configurations
790790
/// output to the same directory.
791-
@visibleForTesting
792791
void trackSharedBuildDirectory(
793792
Environment environment,
794793
FileSystem fileSystem,
@@ -830,15 +829,56 @@ class FlutterBuildSystem extends BuildSystem {
830829
}
831830
final List<String> lastOutputs = (json.decode(outputsFile.readAsStringSync()) as List<Object?>)
832831
.cast<String>();
833-
for (final lastOutput in lastOutputs) {
834-
if (!currentOutputs.containsKey(lastOutput)) {
835-
final File lastOutputFile = fileSystem.file(lastOutput);
836-
if (preservedOutputFilePaths.contains(lastOutputFile.path)) {
837-
continue;
838-
}
839-
ErrorHandlingFileSystem.deleteIfExists(lastOutputFile);
840-
}
832+
_deleteStaleOutputs(
833+
currentOutputs: currentOutputs,
834+
environment: environment,
835+
fileSystem: fileSystem,
836+
preservedOutputFilePaths: preservedOutputFilePaths,
837+
previousOutputs: lastOutputs,
838+
);
839+
}
840+
}
841+
842+
/// Deletes stale output files from previous builds or build stages that are no
843+
/// longer part of the current build configuration or output graph.
844+
///
845+
/// Files located in the shared hooks runner output directory
846+
/// (`.dart_tool/hooks_runner/shared/`) and files in [preservedOutputFilePaths]
847+
/// are explicitly spared from deletion. Shared native asset hook outputs manage
848+
/// their own caching and invalidation lifecycles independently, so cleaning
849+
/// them up when switching to build configurations or stages that do not invoke
850+
/// native assets hooks would incorrectly cause subsequent hook-enabled builds
851+
/// to skip running hooks (due to valid metadata) while failing to locate the
852+
/// deleted output files.
853+
void _deleteStaleOutputs({
854+
required Map<String, File> currentOutputs,
855+
required Environment environment,
856+
required FileSystem fileSystem,
857+
required Set<String> preservedOutputFilePaths,
858+
required Iterable<String> previousOutputs,
859+
}) {
860+
String projectDirPath;
861+
try {
862+
projectDirPath = environment.projectDir.resolveSymbolicLinksSync();
863+
} on FileSystemException {
864+
projectDirPath = environment.projectDir.absolute.path;
865+
}
866+
final String sharedHooksPath = fileSystem.path.canonicalize(
867+
fileSystem.path.join(projectDirPath, '.dart_tool', 'hooks_runner', 'shared'),
868+
);
869+
for (final previousOutput in previousOutputs) {
870+
if (currentOutputs.containsKey(previousOutput)) {
871+
continue;
841872
}
873+
final File previousFile = fileSystem.file(previousOutput);
874+
if (preservedOutputFilePaths.contains(previousFile.path) ||
875+
fileSystem.path.isWithin(
876+
sharedHooksPath,
877+
fileSystem.path.canonicalize(previousFile.path),
878+
)) {
879+
continue;
880+
}
881+
ErrorHandlingFileSystem.deleteIfExists(previousFile);
842882
}
843883
}
844884

@@ -953,16 +993,13 @@ class _BuildInstance {
953993

954994
// Delete outputs from previous stages that are no longer a part of the
955995
// build.
956-
for (final String previousOutput in node.previousOutputs) {
957-
if (outputFiles.containsKey(previousOutput)) {
958-
continue;
959-
}
960-
final File previousFile = fileSystem.file(previousOutput);
961-
if (preservedOutputFilePaths.contains(previousFile.path)) {
962-
continue;
963-
}
964-
ErrorHandlingFileSystem.deleteIfExists(previousFile);
965-
}
996+
_deleteStaleOutputs(
997+
currentOutputs: outputFiles,
998+
environment: environment,
999+
fileSystem: fileSystem,
1000+
preservedOutputFilePaths: preservedOutputFilePaths,
1001+
previousOutputs: node.previousOutputs,
1002+
);
9661003
} on Exception catch (exception, stackTrace) {
9671004
node.target.clearStamp(environment);
9681005
succeeded = false;

‎packages/flutter_tools/test/general.shard/build_system/build_system_test.dart‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,34 @@ void main() {
757757
expect(environment.outputDir.childFile('preserved'), exists);
758758
});
759759

760+
testWithoutContext('trackSharedBuildDirectory does not delete shared hooks outputs', () {
761+
environment.outputDir.childFile('.last_build_id').writeAsStringSync('foo');
762+
final Directory otherBuildDir = environment.buildDir.parent.childDirectory('foo')
763+
..createSync(recursive: true);
764+
final Directory sharedHooksDir =
765+
environment.projectDir
766+
.childDirectory('.dart_tool')
767+
.childDirectory('hooks_runner')
768+
.childDirectory('shared')
769+
..createSync(recursive: true);
770+
final File sharedHookOutputFile = sharedHooksDir.childFile('some_output_file')..createSync();
771+
772+
otherBuildDir
773+
.childFile('outputs.json')
774+
.writeAsStringSync(json.encode(<String>[sharedHookOutputFile.absolute.path]));
775+
FlutterBuildSystem(
776+
fileSystem: fileSystem,
777+
logger: BufferLogger.test(),
778+
platform: FakePlatform(),
779+
).trackSharedBuildDirectory(environment, fileSystem, <String, File>{}, {});
780+
781+
expect(
782+
environment.outputDir.childFile('.last_build_id').readAsStringSync(),
783+
'6666cd76f96956469e7be39d750cc7d9',
784+
);
785+
expect(sharedHookOutputFile, exists);
786+
});
787+
760788
testWithoutContext(
761789
'multiple builds to the same output directory do no leave stale artifacts',
762790
() async {
@@ -884,6 +912,62 @@ void main() {
884912
expect(result.success, false);
885913
expect(result.exceptions.keys, containsAll(<String>['B', 'C']));
886914
});
915+
916+
testWithoutContext(
917+
'BuildSystem.build does not delete shared hooks outputs from previous runs of the same target',
918+
() async {
919+
final fileSystem = MemoryFileSystem.test();
920+
final environment = Environment.test(
921+
fileSystem.currentDirectory,
922+
artifacts: Artifacts.test(),
923+
processManager: FakeProcessManager.any(),
924+
fileSystem: fileSystem,
925+
logger: BufferLogger.test(),
926+
);
927+
928+
final Directory sharedHooksDir =
929+
environment.projectDir
930+
.childDirectory('.dart_tool')
931+
.childDirectory('hooks_runner')
932+
.childDirectory('shared')
933+
..createSync(recursive: true);
934+
final File sharedHookOutputFile = sharedHooksDir.childFile('some_output_file')..createSync();
935+
936+
environment.buildDir.createSync(recursive: true);
937+
final File normalOutputFile = environment.buildDir.childFile('normal_output')..createSync();
938+
939+
final target =
940+
TestTarget((Environment environment) async {
941+
sharedHooksDir.childFile('some_output_file').createSync(recursive: true);
942+
environment.buildDir.childFile('normal_output').createSync(recursive: true);
943+
})
944+
..name = 'test_target'
945+
..outputs = <Source>[
946+
const Source.pattern('{PROJECT_DIR}/.dart_tool/hooks_runner/shared/some_output_file'),
947+
const Source.pattern('{BUILD_DIR}/normal_output'),
948+
];
949+
950+
final BuildSystem buildSystem = setUpBuildSystem(fileSystem);
951+
952+
// First build.
953+
BuildResult result = await buildSystem.build(target, environment);
954+
expect(result.success, true);
955+
expect(sharedHookOutputFile, exists);
956+
expect(normalOutputFile, exists);
957+
958+
// Modify target to not have outputs.
959+
target.outputs = <Source>[];
960+
961+
// Second build.
962+
result = await buildSystem.build(target, environment);
963+
expect(result.success, true);
964+
965+
// Normal output should be deleted.
966+
expect(normalOutputFile, isNot(exists));
967+
// Shared hook output should NOT be deleted.
968+
expect(sharedHookOutputFile, exists);
969+
},
970+
);
887971
}
888972

889973
BuildSystem setUpBuildSystem(FileSystem fileSystem, {FakePlatform? platform, Logger? logger}) {

0 commit comments

Comments
 (0)