Skip to content

Commit 21508b1

Browse files
Wyveraldcopybara-github
authored andcommitted
Disregard WORKSPACE while verifying lockfile repo mapping entries in extension eval
See code comment and linked issue for more information. Fixes #20942. Closes #20982. PiperOrigin-RevId: 600856392 Change-Id: I5b8a0ed3a38e37ab51ffb49b19a59f2e161b9a33
1 parent d6db03b commit 21508b1

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/SingleExtensionEvalFunction.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,19 @@ public String getRecordedDiffMessages() {
342342
private static boolean didRepoMappingsChange(
343343
Environment env, ImmutableTable<RepositoryName, String, RepositoryName> recordedRepoMappings)
344344
throws InterruptedException, NeedsSkyframeRestartException {
345+
// Request repo mappings for any 'source repos' in the recorded mapping entries.
346+
// Note specially that the main repo needs to be treated differently: if any .bzl file from the
347+
// main repo was used for module extension eval, it _has_ to be before WORKSPACE is evaluated
348+
// (see relevant code in BzlLoadFunction#getRepositoryMapping), so we only request the main repo
349+
// mapping _without_ WORKSPACE repos. See #20942 for more information.
345350
SkyframeLookupResult result =
346351
env.getValuesAndExceptions(
347352
recordedRepoMappings.rowKeySet().stream()
348-
.map(RepositoryMappingValue::key)
353+
.map(
354+
repoName ->
355+
repoName.isMain()
356+
? RepositoryMappingValue.KEY_FOR_ROOT_MODULE_WITHOUT_WORKSPACE_REPOS
357+
: RepositoryMappingValue.key(repoName))
349358
.collect(toImmutableSet()));
350359
if (env.valuesMissing()) {
351360
// This likely means that one of the 'source repos' in the recorded mapping entries is no
@@ -354,7 +363,11 @@ private static boolean didRepoMappingsChange(
354363
}
355364
for (Table.Cell<RepositoryName, String, RepositoryName> cell : recordedRepoMappings.cellSet()) {
356365
RepositoryMappingValue repoMappingValue =
357-
(RepositoryMappingValue) result.get(RepositoryMappingValue.key(cell.getRowKey()));
366+
(RepositoryMappingValue)
367+
result.get(
368+
cell.getRowKey().isMain()
369+
? RepositoryMappingValue.KEY_FOR_ROOT_MODULE_WITHOUT_WORKSPACE_REPOS
370+
: RepositoryMappingValue.key(cell.getRowKey()));
358371
if (repoMappingValue == null) {
359372
throw new NeedsSkyframeRestartException();
360373
}

src/test/py/bazel/bzlmod/bazel_lockfile_test.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,50 @@ def testExtensionRepoMappingChange_sourceRepoNoLongerExistent(self):
18331833
self.assertIn('ran the extension!', '\n'.join(stderr))
18341834
self.assertIn('STR=@@quux~1.0//:quux.h', '\n'.join(stderr))
18351835

1836+
def testExtensionRepoMappingChange_mainRepoEvalCycleWithWorkspace(self):
1837+
# Regression test for #20942
1838+
self.main_registry.createCcModule('foo', '1.0')
1839+
self.ScratchFile(
1840+
'MODULE.bazel',
1841+
[
1842+
'bazel_dep(name="foo",version="1.0")',
1843+
'ext = use_extension(":ext.bzl", "ext")',
1844+
'use_repo(ext, "repo")',
1845+
],
1846+
)
1847+
self.ScratchFile(
1848+
'BUILD.bazel',
1849+
[
1850+
'load("@repo//:defs.bzl", "STR")',
1851+
'print("STR="+STR)',
1852+
'filegroup(name="lol")',
1853+
],
1854+
)
1855+
self.ScratchFile(
1856+
'ext.bzl',
1857+
[
1858+
'def _repo_impl(rctx):',
1859+
' rctx.file("BUILD")',
1860+
' rctx.file("defs.bzl", "STR = " + repr(str(rctx.attr.value)))',
1861+
'repo = repository_rule(_repo_impl,attrs={"value":attr.label()})',
1862+
'def _ext_impl(mctx):',
1863+
' print("ran the extension!")',
1864+
' repo(name = "repo", value = Label("@foo//:lib_foo"))',
1865+
'ext = module_extension(_ext_impl)',
1866+
],
1867+
)
1868+
# any `load` in WORKSPACE should trigger the bug
1869+
self.ScratchFile('WORKSPACE.bzlmod', ['load("@repo//:defs.bzl","STR")'])
1870+
1871+
_, _, stderr = self.RunBazel(['build', '--enable_workspace', ':lol'])
1872+
self.assertIn('STR=@@foo~1.0//:lib_foo', '\n'.join(stderr))
1873+
1874+
# Shutdown bazel to make sure we rely on the lockfile and not skyframe
1875+
self.RunBazel(['shutdown'])
1876+
# Build again. This should _NOT_ trigger a failure!
1877+
_, _, stderr = self.RunBazel(['build', '--enable_workspace', ':lol'])
1878+
self.assertNotIn('ran the extension!', '\n'.join(stderr))
1879+
18361880

18371881
if __name__ == '__main__':
18381882
absltest.main()

0 commit comments

Comments
 (0)