Skip to content

Commit c565726

Browse files
Wyveraldbazel-io
authored andcommitted
Allow any canonical repo name to be used with bazel mod show_repo
Previously we enforced that the canonical repo name had to correspond to a module (i.e. not an extension-generated repo). This makes no sense, so this change simple deletes that enforcement. To compensate for the lack of checks whether the repo exists at all, we add an additional check after requesting the BzlmodRepoRuleValues that all requested repos must exist. Fixes bazelbuild#21621 PiperOrigin-RevId: 615733963 Change-Id: Icf706b36f0e8d3c775bc26528948e5102910d4a0
1 parent 7c7c5c3 commit c565726

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

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

+1-12
Original file line numberDiff line numberDiff line change
@@ -334,18 +334,7 @@ public ImmutableMap<String, RepositoryName> resolveToRepoNames(
334334
ImmutableMap<String, ImmutableSet<ModuleKey>> modulesIndex,
335335
ImmutableMap<ModuleKey, AugmentedModule> depGraph,
336336
ImmutableMap<ModuleKey, RepositoryName> moduleKeyToCanonicalNames,
337-
RepositoryMapping mapping)
338-
throws InvalidArgumentException {
339-
if (depGraph.values().stream()
340-
.filter(m -> moduleKeyToCanonicalNames.get(m.getKey()).equals(repoName()) && m.isUsed())
341-
.findAny()
342-
.isEmpty()) {
343-
throw new InvalidArgumentException(
344-
String.format(
345-
"No module with the canonical repo name @@%s exists in the dependency graph",
346-
repoName().getName()),
347-
Code.INVALID_ARGUMENTS);
348-
}
337+
RepositoryMapping mapping) {
349338
return ImmutableMap.of(toString(), repoName());
350339
}
351340

src/main/java/com/google/devtools/build/lib/bazel/commands/ModCommand.java

+9
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
import java.util.ArrayList;
9595
import java.util.Collection;
9696
import java.util.List;
97+
import java.util.Map;
9798
import java.util.Map.Entry;
9899
import java.util.Objects;
99100
import java.util.Optional;
@@ -494,6 +495,14 @@ private BlazeCommandResult execInternal(CommandEnvironment env, OptionsParsingRe
494495
e ->
495496
(BzlmodRepoRuleValue)
496497
result.get(BzlmodRepoRuleValue.key(e.getValue()))));
498+
for (Map.Entry<String, BzlmodRepoRuleValue> entry : targetRepoRuleValues.entrySet()) {
499+
if (entry.getValue() == BzlmodRepoRuleValue.REPO_RULE_NOT_FOUND_VALUE) {
500+
return reportAndCreateFailureResult(
501+
env,
502+
String.format("In repo argument %s: no such repo", entry.getKey()),
503+
Code.INVALID_ARGUMENTS);
504+
}
505+
}
497506
}
498507
} catch (InterruptedException e) {
499508
String errorMessage = "mod command interrupted: " + e.getMessage();

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

+7-8
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,10 @@ public void resolve_canonicalRepoName_notFound() throws Exception {
303303
baseModuleUnusedDeps,
304304
/* includeUnused= */ true,
305305
/* warnUnused= */ true));
306-
assertThrows(
307-
InvalidArgumentException.class,
308-
() ->
309-
arg.resolveToRepoNames(modulesIndex, depGraph, moduleKeyToCanonicalNames, rootMapping));
306+
// The repo need not exist in the "repo -> repo" case.
307+
assertThat(
308+
arg.resolveToRepoNames(modulesIndex, depGraph, moduleKeyToCanonicalNames, rootMapping))
309+
.containsExactly("@@bar~1.0", RepositoryName.create("bar~1.0"));
310310
}
311311

312312
@Test
@@ -341,9 +341,8 @@ public void resolve_canonicalRepoName_unused() throws Exception {
341341
.containsExactly(foo1);
342342

343343
// resolving to repo names doesn't care about unused deps.
344-
assertThrows(
345-
InvalidArgumentException.class,
346-
() ->
347-
arg.resolveToRepoNames(modulesIndex, depGraph, moduleKeyToCanonicalNames, rootMapping));
344+
assertThat(
345+
arg.resolveToRepoNames(modulesIndex, depGraph, moduleKeyToCanonicalNames, rootMapping))
346+
.containsExactly("@@foo~1.0", RepositoryName.create("foo~1.0"));
348347
}
349348
}

src/test/py/bazel/bzlmod/mod_command_test.py

+12
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,18 @@ def testShowRepoThrowsUnusedModule(self):
465465
stderr,
466466
)
467467

468+
def testShowRepoThrowsNonexistentRepo(self):
469+
_, _, stderr = self.RunBazel(
470+
['mod', 'show_repo', '@@lol'],
471+
allow_failure=True,
472+
rstrip=True,
473+
)
474+
self.assertIn(
475+
"ERROR: In repo argument @@lol: no such repo. Type 'bazel help mod' "
476+
'for syntax and help.',
477+
stderr,
478+
)
479+
468480
def testDumpRepoMapping(self):
469481
_, stdout, _ = self.RunBazel(
470482
[

0 commit comments

Comments
 (0)