Skip to content
Open
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 @@ -630,8 +630,8 @@ private static ModuleThreadContext execModuleFile(
}
});

injectRepos(injectedRepositories, context, thread);
compiledRootModuleFile.runOnThread(thread);
injectRepos(injectedRepositories, context, thread);
} catch (EvalException e) {
eventHandler.handle(Event.error(e.getInnermostLocation(), e.getMessageWithStack()));
throw errorf(Code.BAD_MODULE, "error executing MODULE.bazel file for %s", moduleKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,12 @@ Location location() {
public void addRepoNameUsage(
String repoName, String how, ImmutableList<StarlarkThread.CallStackEntry> stack)
throws EvalException {
RepoNameUsage collision = repoNameUsages.put(repoName, new RepoNameUsage(how, stack));
RepoNameUsage incoming = new RepoNameUsage(how, stack);
RepoNameUsage collision = repoNameUsages.put(repoName, incoming);
if (collision != null) {
throw Starlark.errorf(
"The repo name '%s' is already being used %s at %s",
repoName, collision.how(), collision.location());
"The repo name '%s' cannot be defined %s at %s as it is already defined %s at %s",
repoName, incoming.how(), incoming.location(), collision.how(), collision.location());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,8 @@ public void testRootModule_include_bad_repoNameCollision() throws Exception {
evaluator.evaluate(
ImmutableList.of(ModuleFileValue.KEY_FOR_ROOT_MODULE), evaluationContext);
assertThat(result.hasError()).isTrue();
assertContainsEvent("The repo name 'foo' is already being used");
assertContainsEvent("The repo name 'foo' cannot be defined");
assertContainsEvent("as it is already defined");
}

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

assertContainsEvent(
"The repo name 'mymod' is already being used as the current module name at");
assertContainsEvent("The repo name 'mymod' cannot be defined");
assertContainsEvent("as it is already defined as the current module name");
}

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

assertContainsEvent("The repo name 'bbb' is already being used as the module's own repo name");
assertContainsEvent("The repo name 'bbb' cannot be defined");
assertContainsEvent("as it is already defined as the module's own repo name");
}

@Test
Expand Down
54 changes: 52 additions & 2 deletions src/test/py/bazel/bzlmod/bazel_overrides_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,11 +720,61 @@ def testInjectRepositoryOnExistingRepo(self):
allow_failure=True,
)
self.AssertNotExitCode(exit_code, 0, stderr)
stderr = "\n".join(stderr)
self.assertIn(
"Error in use_repo: The repo name 'my_repo' is already being used by"
' --inject_repository at <builtin>',
"ERROR: The repo name 'my_repo' cannot be defined by"
' --inject_repository at <builtin> as it is already defined by'
' a use_repo() call at',
stderr,
)
self.assertIn(
'MODULE.bazel:2:9',
stderr,
)

def testInjectRepositoryRepoNameSideEffects(self):
self.ScratchFile(
'MODULE.bazel',
[
'local_repository = use_repo_rule("@//:defs.bzl", "local_repository")',
f'local_repository(name = "repo")',
],
)
self.ScratchFile(
'defs.bzl',
[
'def _impl(ctx):',
' ctx.file("BUILD")',
# Deliberate collision with `@bazel_tools//tools/build_defs/repo:local.bzl%local_repository`
'local_repository = repository_rule(implementation=_impl)',
],
)
self.ScratchFile('BUILD')

self.ScratchFile('other_module/REPO.bazel')
self.ScratchFile('other_repo/BUILD', ['filegroup(name="target")'])

_, stdout, _ = self.RunBazel(['mod', 'dump_repo_mapping', ''])
self.assertIn(
'"+local_repository+repo"',
"\n".join(stdout),
)

# --inject_repository _must not_ affect `use_repo_rule` generated repo names.
_, stdout, _ = self.RunBazel([
'mod',
'dump_repo_mapping',
'',
'--inject_repository=injected_repo=%workspace%/other_repo',
])
self.assertIn(
'"+local_repository+repo"',
"\n".join(stdout),
)
self.assertIn(
'"+local_repository2+injected_repo"',
"\n".join(stdout),
)

def testOverrideRepositoryOnNonExistentRepo(self):
self.ScratchFile('other_repo/REPO.bazel')
Expand Down