Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disregard WORKSPACE while verifying lockfile repo mapping entries in extension eval #20982

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,17 @@ public String getRecordedDiffMessages() {
private static boolean didRepoMappingsChange(
Environment env, ImmutableTable<RepositoryName, String, RepositoryName> recordedRepoMappings)
throws InterruptedException, NeedsSkyframeRestartException {
// Request repo mappings for any 'source repos' in the recorded mapping entries.
// Note specially that the main repo needs to be treated differently: if any .bzl file from the
// main repo was used for module extension eval, it _has_ to be before WORKSPACE is evaluated
// (see relevant code in BzlLoadFunction#getRepositoryMapping), so we only request the main repo
// mapping _without_ WORKSPACE repos. See #20942 for more information.
SkyframeLookupResult result =
env.getValuesAndExceptions(
recordedRepoMappings.rowKeySet().stream()
.map(RepositoryMappingValue::key)
.map(repoName -> repoName.isMain()
? RepositoryMappingValue.KEY_FOR_ROOT_MODULE_WITHOUT_WORKSPACE_REPOS
: RepositoryMappingValue.key(repoName))
.collect(toImmutableSet()));
if (env.valuesMissing()) {
// This likely means that one of the 'source repos' in the recorded mapping entries is no
Expand All @@ -354,7 +361,9 @@ private static boolean didRepoMappingsChange(
}
for (Table.Cell<RepositoryName, String, RepositoryName> cell : recordedRepoMappings.cellSet()) {
RepositoryMappingValue repoMappingValue =
(RepositoryMappingValue) result.get(RepositoryMappingValue.key(cell.getRowKey()));
(RepositoryMappingValue) result.get(cell.getRowKey().isMain()
? RepositoryMappingValue.KEY_FOR_ROOT_MODULE_WITHOUT_WORKSPACE_REPOS
: RepositoryMappingValue.key(cell.getRowKey()));
if (repoMappingValue == null) {
throw new NeedsSkyframeRestartException();
}
Expand Down
44 changes: 44 additions & 0 deletions src/test/py/bazel/bzlmod/bazel_lockfile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,50 @@ def testExtensionRepoMappingChange_sourceRepoNoLongerExistent(self):
self.assertIn('ran the extension!', '\n'.join(stderr))
self.assertIn('STR=@@quux~1.0//:quux.h', '\n'.join(stderr))

def testExtensionRepoMappingChange_mainRepoEvalCycleWithWorkspace(self):
# Regression test for #20942
self.main_registry.createCcModule('foo', '1.0')
self.ScratchFile(
'MODULE.bazel',
[
'bazel_dep(name="foo",version="1.0")',
'ext = use_extension(":ext.bzl", "ext")',
'use_repo(ext, "repo")',
],
)
self.ScratchFile(
'BUILD.bazel',
[
'load("@repo//:defs.bzl", "STR")',
'print("STR="+STR)',
'filegroup(name="lol")',
],
)
self.ScratchFile(
'ext.bzl',
[
'def _repo_impl(rctx):',
' rctx.file("BUILD")',
' rctx.file("defs.bzl", "STR = " + repr(str(rctx.attr.value)))',
'repo = repository_rule(_repo_impl,attrs={"value":attr.label()})',
'def _ext_impl(mctx):',
' print("ran the extension!")',
' repo(name = "repo", value = Label("@foo//:lib_foo"))',
'ext = module_extension(_ext_impl)',
],
)
# any `load` in WORKSPACE should trigger the bug
self.ScratchFile('WORKSPACE.bzlmod', ['load("@repo//:defs.bzl","STR")'])

_, _, stderr = self.RunBazel(['build', '--enable_workspace', ':lol'])
self.assertIn('STR=@@foo~1.0//:lib_foo', '\n'.join(stderr))

# Shutdown bazel to make sure we rely on the lockfile and not skyframe
self.RunBazel(['shutdown'])
# Build again. This should _NOT_ trigger a failure!
_, _, stderr = self.RunBazel(['build', '--enable_workspace', ':lol'])
self.assertNotIn('ran the extension!', '\n'.join(stderr))


if __name__ == '__main__':
absltest.main()
Loading