Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.1.1] Allow any canonical repo name to be used with bazel mod show_repo #21694

Merged
merged 1 commit into from
Mar 14, 2024
Merged
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 @@ -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
Loading