Skip to content

Commit

Permalink
Friendlier error message for bazel_deps without version
Browse files Browse the repository at this point in the history
Ideally we would also include the location of the offending bazel_dep, but that requires a (slightly?) bigger change to record the locations of all bazel_deps.

Fixes #17126

PiperOrigin-RevId: 554406088
Change-Id: I46865c31dc983de18ac64a37e8ee15ebec624937
  • Loading branch information
Wyverald authored and bazel-io committed Aug 7, 2023
1 parent 90e8ffd commit b8a5704
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,15 @@ private GetModuleFileResult getModuleFile(
}

// Otherwise, we should get the module file from a registry.
if (key.getVersion().isEmpty()) {
// Print a friendlier error message if the user forgets to specify a version *and* doesn't
// have a non-registry override.
throw errorf(
Code.MODULE_NOT_FOUND,
"bad bazel_dep on module '%s' with no version. Did you forget to specify a version, or a"
+ " non-registry override?",
key.getName());
}
// TODO(wyv): Move registry object creation to BazelRepositoryModule so we don't repeatedly
// create them, and we can better report the error (is it a flag error or override error?).
List<String> registries = Objects.requireNonNull(REGISTRIES.get(env));
Expand All @@ -361,6 +370,7 @@ private GetModuleFileResult getModuleFile(
}
} else if (override != null) {
// This should never happen.
// TODO(wyv): make ModuleOverride a sealed interface so this is checked at compile time.
throw new IllegalStateException(
String.format(
"unrecognized override type %s for module %s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,19 @@ public void testRootModule_badSelfOverride() throws Exception {
assertThat(result.getError().toString()).contains("invalid override for the root module");
}

@Test
public void forgotVersion() throws Exception {
FakeRegistry registry = registryFactory.newFakeRegistry("/foo");
ModuleFileFunction.REGISTRIES.set(differencer, ImmutableList.of(registry.getUrl()));

SkyKey skyKey = ModuleFileValue.key(createModuleKey("bbb", ""), null);
EvaluationResult<ModuleFileValue> result =
evaluator.evaluate(ImmutableList.of(skyKey), evaluationContext);
assertThat(result.hasError()).isTrue();
assertThat(result.getError().toString())
.contains("bad bazel_dep on module 'bbb' with no version");
}

@Test
public void testRegistriesCascade() throws Exception {
// Registry1 has no module B@1.0; registry2 and registry3 both have it. We should be using the
Expand Down

0 comments on commit b8a5704

Please sign in to comment.