Skip to content

Commit

Permalink
[7.1.1] Allow any canonical repo name to be used with `bazel mod show…
Browse files Browse the repository at this point in the history
…_repo` (#21694)

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 #21621

Commit
5c2b37e

PiperOrigin-RevId: 615733963
Change-Id: Icf706b36f0e8d3c775bc26528948e5102910d4a0

Co-authored-by: Googler <wyv@google.com>
  • Loading branch information
bazel-io and Wyverald authored Mar 14, 2024
1 parent 49166bd commit 2288c9c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,18 +334,7 @@ public ImmutableMap<String, RepositoryName> resolveToRepoNames(
ImmutableMap<String, ImmutableSet<ModuleKey>> modulesIndex,
ImmutableMap<ModuleKey, AugmentedModule> depGraph,
ImmutableMap<ModuleKey, RepositoryName> moduleKeyToCanonicalNames,
RepositoryMapping mapping)
throws InvalidArgumentException {
if (depGraph.values().stream()
.filter(m -> moduleKeyToCanonicalNames.get(m.getKey()).equals(repoName()) && m.isUsed())
.findAny()
.isEmpty()) {
throw new InvalidArgumentException(
String.format(
"No module with the canonical repo name @@%s exists in the dependency graph",
repoName().getName()),
Code.INVALID_ARGUMENTS);
}
RepositoryMapping mapping) {
return ImmutableMap.of(toString(), repoName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -494,6 +495,14 @@ private BlazeCommandResult execInternal(CommandEnvironment env, OptionsParsingRe
e ->
(BzlmodRepoRuleValue)
result.get(BzlmodRepoRuleValue.key(e.getValue()))));
for (Map.Entry<String, BzlmodRepoRuleValue> entry : targetRepoRuleValues.entrySet()) {
if (entry.getValue() == BzlmodRepoRuleValue.REPO_RULE_NOT_FOUND_VALUE) {
return reportAndCreateFailureResult(
env,
String.format("In repo argument %s: no such repo", entry.getKey()),
Code.INVALID_ARGUMENTS);
}
}
}
} catch (InterruptedException e) {
String errorMessage = "mod command interrupted: " + e.getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,10 @@ public void resolve_canonicalRepoName_notFound() throws Exception {
baseModuleUnusedDeps,
/* includeUnused= */ true,
/* warnUnused= */ true));
assertThrows(
InvalidArgumentException.class,
() ->
arg.resolveToRepoNames(modulesIndex, depGraph, moduleKeyToCanonicalNames, rootMapping));
// The repo need not exist in the "repo -> repo" case.
assertThat(
arg.resolveToRepoNames(modulesIndex, depGraph, moduleKeyToCanonicalNames, rootMapping))
.containsExactly("@@bar~1.0", RepositoryName.create("bar~1.0"));
}

@Test
Expand Down Expand Up @@ -341,9 +341,8 @@ public void resolve_canonicalRepoName_unused() throws Exception {
.containsExactly(foo1);

// resolving to repo names doesn't care about unused deps.
assertThrows(
InvalidArgumentException.class,
() ->
arg.resolveToRepoNames(modulesIndex, depGraph, moduleKeyToCanonicalNames, rootMapping));
assertThat(
arg.resolveToRepoNames(modulesIndex, depGraph, moduleKeyToCanonicalNames, rootMapping))
.containsExactly("@@foo~1.0", RepositoryName.create("foo~1.0"));
}
}
12 changes: 12 additions & 0 deletions src/test/py/bazel/bzlmod/mod_command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,18 @@ def testShowRepoThrowsUnusedModule(self):
stderr,
)

def testShowRepoThrowsNonexistentRepo(self):
_, _, stderr = self.RunBazel(
['mod', 'show_repo', '@@lol'],
allow_failure=True,
rstrip=True,
)
self.assertIn(
"ERROR: In repo argument @@lol: no such repo. Type 'bazel help mod' "
'for syntax and help.',
stderr,
)

def testDumpRepoMapping(self):
_, stdout, _ = self.RunBazel(
[
Expand Down

0 comments on commit 2288c9c

Please sign in to comment.