From 19c0c809abbe4bc70d3d6b493ff966dd41c54768 Mon Sep 17 00:00:00 2001 From: Salma Samy Date: Wed, 30 Aug 2023 03:24:12 -0700 Subject: [PATCH] Ensure lockfile is updated after reset to pre-build state The events that update the lockfile are important for incremental correctness and thus need to return `true` from `storeForReplay`. Before this change, if a build creates the lockfile because it didn't exist and the lockfile is then deleted, subsequent builds did not regenerate it as the relevant SkyFunctions wouldn't rerun and the events were not replayed. Closes #19343. PiperOrigin-RevId: 561287438 Change-Id: I549f99b896a0095e8ffc35b7bacc8a841a44219a --- .../bzlmod/BazelModuleResolutionEvent.java | 5 +++ .../ModuleExtensionResolutionEvent.java | 5 +++ .../py/bazel/bzlmod/bazel_lockfile_test.py | 42 +++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelModuleResolutionEvent.java b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelModuleResolutionEvent.java index 81a0dbaa144b9c..b2e0f6f3a5b40f 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelModuleResolutionEvent.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelModuleResolutionEvent.java @@ -36,4 +36,9 @@ public static BazelModuleResolutionEvent create( public abstract ImmutableTable getExtensionUsagesById(); + + @Override + public boolean storeForReplay() { + return true; + } } diff --git a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionResolutionEvent.java b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionResolutionEvent.java index 4013e7bb4c5965..cb82ae039f08fe 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionResolutionEvent.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionResolutionEvent.java @@ -33,4 +33,9 @@ public static ModuleExtensionResolutionEvent create( public abstract ModuleExtensionId getExtensionId(); public abstract LockFileModuleExtension getModuleExtension(); + + @Override + public boolean storeForReplay() { + return true; + } } diff --git a/src/test/py/bazel/bzlmod/bazel_lockfile_test.py b/src/test/py/bazel/bzlmod/bazel_lockfile_test.py index 102ad544f397a0..b90283e387a48d 100644 --- a/src/test/py/bazel/bzlmod/bazel_lockfile_test.py +++ b/src/test/py/bazel/bzlmod/bazel_lockfile_test.py @@ -964,6 +964,48 @@ def testExtensionEvaluationDoesNotRerunOnChangedImports(self): stderr, ) + def testLockfileRecreatedAfterDeletion(self): + self.ScratchFile( + 'MODULE.bazel', + [ + 'lockfile_ext = use_extension("extension.bzl", "lockfile_ext")', + 'use_repo(lockfile_ext, "hello")', + ], + ) + self.ScratchFile('BUILD.bazel') + self.ScratchFile( + 'extension.bzl', + [ + 'def _repo_rule_impl(ctx):', + ' ctx.file("WORKSPACE")', + ' ctx.file("BUILD", "filegroup(name=\'lala\')")', + '', + 'repo_rule = repository_rule(implementation=_repo_rule_impl)', + '', + 'def _module_ext_impl(ctx):', + ' repo_rule(name="hello")', + '', + 'lockfile_ext = module_extension(', + ' implementation=_module_ext_impl,', + ')', + ], + ) + + self.RunBazel(['build', '@hello//:all']) + + # Return the lockfile to the state it had before the + # previous build: it didn't exist. + with open('MODULE.bazel.lock', 'r') as lock_file: + old_data = lock_file.read() + os.remove('MODULE.bazel.lock') + + self.RunBazel(['build', '@hello//:all']) + + with open('MODULE.bazel.lock', 'r') as lock_file: + new_data = lock_file.read() + + self.assertEqual(old_data, new_data) + if __name__ == '__main__': unittest.main()