From 2288c9c5d466553fa0d98662b28e29ebb120a7a5 Mon Sep 17 00:00:00 2001 From: "bazel.build machine account" <15028808+bazel-io@users.noreply.github.com> Date: Thu, 14 Mar 2024 17:35:27 +0100 Subject: [PATCH] [7.1.1] Allow any canonical repo name to be used with `bazel mod show_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 https://github.com/bazelbuild/bazel/issues/21621 Commit https://github.com/bazelbuild/bazel/commit/5c2b37e56e77e9c96b1e5a6c99d803e31e17a03c PiperOrigin-RevId: 615733963 Change-Id: Icf706b36f0e8d3c775bc26528948e5102910d4a0 Co-authored-by: Googler --- .../lib/bazel/bzlmod/modcommand/ModuleArg.java | 13 +------------ .../build/lib/bazel/commands/ModCommand.java | 9 +++++++++ .../bazel/bzlmod/modcommand/ModuleArgTest.java | 15 +++++++-------- src/test/py/bazel/bzlmod/mod_command_test.py | 12 ++++++++++++ 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/modcommand/ModuleArg.java b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/modcommand/ModuleArg.java index 359f6204bbc8b1..1b754658d94f21 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/modcommand/ModuleArg.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/modcommand/ModuleArg.java @@ -334,18 +334,7 @@ public ImmutableMap resolveToRepoNames( ImmutableMap> modulesIndex, ImmutableMap depGraph, ImmutableMap 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()); } diff --git a/src/main/java/com/google/devtools/build/lib/bazel/commands/ModCommand.java b/src/main/java/com/google/devtools/build/lib/bazel/commands/ModCommand.java index d6df9854b00297..5f472982783f3b 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/commands/ModCommand.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/commands/ModCommand.java @@ -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; @@ -494,6 +495,14 @@ private BlazeCommandResult execInternal(CommandEnvironment env, OptionsParsingRe e -> (BzlmodRepoRuleValue) result.get(BzlmodRepoRuleValue.key(e.getValue())))); + for (Map.Entry 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(); diff --git a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/modcommand/ModuleArgTest.java b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/modcommand/ModuleArgTest.java index 6a9079a889e399..ebb2dd02a0335c 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/modcommand/ModuleArgTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/modcommand/ModuleArgTest.java @@ -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 @@ -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")); } } diff --git a/src/test/py/bazel/bzlmod/mod_command_test.py b/src/test/py/bazel/bzlmod/mod_command_test.py index ef2097279f7196..3f6853dee97d59 100644 --- a/src/test/py/bazel/bzlmod/mod_command_test.py +++ b/src/test/py/bazel/bzlmod/mod_command_test.py @@ -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( [