Skip to content

Commit e2e56b0

Browse files
Inject repositories after root module file processed
1 parent d7f6d29 commit e2e56b0

File tree

4 files changed

+63
-10
lines changed

4 files changed

+63
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,8 @@ private static ModuleThreadContext execModuleFile(
630630
}
631631
});
632632

633-
injectRepos(injectedRepositories, context, thread);
634633
compiledRootModuleFile.runOnThread(thread);
634+
injectRepos(injectedRepositories, context, thread);
635635
} catch (EvalException e) {
636636
eventHandler.handle(Event.error(e.getInnermostLocation(), e.getMessageWithStack()));
637637
throw errorf(Code.BAD_MODULE, "error executing MODULE.bazel file for %s", moduleKey);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,12 @@ Location location() {
108108
public void addRepoNameUsage(
109109
String repoName, String how, ImmutableList<StarlarkThread.CallStackEntry> stack)
110110
throws EvalException {
111-
RepoNameUsage collision = repoNameUsages.put(repoName, new RepoNameUsage(how, stack));
111+
RepoNameUsage incoming = new RepoNameUsage(how, stack);
112+
RepoNameUsage collision = repoNameUsages.put(repoName, incoming);
112113
if (collision != null) {
113114
throw Starlark.errorf(
114-
"The repo name '%s' is already being used %s at %s",
115-
repoName, collision.how(), collision.location());
115+
"The repo name '%s' cannot be defined %s at %s as it is already defined %s at %s",
116+
repoName, incoming.how(), incoming.location(), collision.how(), collision.location());
116117
}
117118
}
118119

src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunctionTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,8 @@ public void testRootModule_include_bad_repoNameCollision() throws Exception {
522522
evaluator.evaluate(
523523
ImmutableList.of(ModuleFileValue.KEY_FOR_ROOT_MODULE), evaluationContext);
524524
assertThat(result.hasError()).isTrue();
525-
assertContainsEvent("The repo name 'foo' is already being used");
525+
assertContainsEvent("The repo name 'foo' cannot be defined");
526+
assertContainsEvent("as it is already defined");
526527
}
527528

528529
@Test
@@ -1144,8 +1145,8 @@ public void testModuleExtensions_repoNameCollision_localRepoName() throws Except
11441145
reporter.removeHandler(failFastHandler); // expect failures
11451146
evaluator.evaluate(ImmutableList.of(skyKey), evaluationContext);
11461147

1147-
assertContainsEvent(
1148-
"The repo name 'mymod' is already being used as the current module name at");
1148+
assertContainsEvent("The repo name 'mymod' cannot be defined");
1149+
assertContainsEvent("as it is already defined as the current module name");
11491150
}
11501151

11511152
@Test
@@ -1491,7 +1492,8 @@ public void moduleRepoName_conflict() throws Exception {
14911492
reporter.removeHandler(failFastHandler); // expect failures
14921493
evaluator.evaluate(ImmutableList.of(ModuleFileValue.KEY_FOR_ROOT_MODULE), evaluationContext);
14931494

1494-
assertContainsEvent("The repo name 'bbb' is already being used as the module's own repo name");
1495+
assertContainsEvent("The repo name 'bbb' cannot be defined");
1496+
assertContainsEvent("as it is already defined as the module's own repo name");
14951497
}
14961498

14971499
@Test

src/test/py/bazel/bzlmod/bazel_overrides_test.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,11 +720,61 @@ def testInjectRepositoryOnExistingRepo(self):
720720
allow_failure=True,
721721
)
722722
self.AssertNotExitCode(exit_code, 0, stderr)
723+
stderr = "\n".join(stderr)
723724
self.assertIn(
724-
"Error in use_repo: The repo name 'my_repo' is already being used by"
725-
' --inject_repository at <builtin>',
725+
"ERROR: The repo name 'my_repo' cannot be defined by"
726+
' --inject_repository at <builtin> as it is already defined by'
727+
' a use_repo() call at',
726728
stderr,
727729
)
730+
self.assertIn(
731+
'MODULE.bazel:2:9',
732+
stderr,
733+
)
734+
735+
def testInjectRepositoryRepoNameSideEffects(self):
736+
self.ScratchFile(
737+
'MODULE.bazel',
738+
[
739+
'local_repository = use_repo_rule("@//:defs.bzl", "local_repository")',
740+
f'local_repository(name = "repo")',
741+
],
742+
)
743+
self.ScratchFile(
744+
'defs.bzl',
745+
[
746+
'def _impl(ctx):',
747+
' ctx.file("BUILD")',
748+
# Deliberate collision with `@bazel_tools//tools/build_defs/repo:local.bzl%local_repository`
749+
'local_repository = repository_rule(implementation=_impl)',
750+
],
751+
)
752+
self.ScratchFile('BUILD')
753+
754+
self.ScratchFile('other_module/REPO.bazel')
755+
self.ScratchFile('other_repo/BUILD', ['filegroup(name="target")'])
756+
757+
_, stdout, _ = self.RunBazel(['mod', 'dump_repo_mapping', ''])
758+
self.assertIn(
759+
'"+local_repository+repo"',
760+
"\n".join(stdout),
761+
)
762+
763+
# --inject_repository _must not_ affect `use_repo_rule` generated repo names.
764+
_, stdout, _ = self.RunBazel([
765+
'mod',
766+
'dump_repo_mapping',
767+
'',
768+
'--inject_repository=injected_repo=%workspace%/other_repo',
769+
])
770+
self.assertIn(
771+
'"+local_repository+repo"',
772+
"\n".join(stdout),
773+
)
774+
self.assertIn(
775+
'"+local_repository2+injected_repo"',
776+
"\n".join(stdout),
777+
)
728778

729779
def testOverrideRepositoryOnNonExistentRepo(self):
730780
self.ScratchFile('other_repo/REPO.bazel')

0 commit comments

Comments
 (0)