Skip to content

Commit

Permalink
Fail early if use_extension has a bad label
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 520627028
Change-Id: Ib42df77b02674b3ea55639e163e369afbedebce9
  • Loading branch information
Wyverald authored and Copybara-Service committed Mar 30, 2023
1 parent 9353955 commit 109b290
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Expand Up @@ -69,11 +69,11 @@ public SkyValue compute(SkyKey skyKey, Environment env)
PackageIdentifier.create(module.getCanonicalRepoName(), PathFragment.EMPTY_FRAGMENT),
module.getRepoMappingWithBazelDepsOnly());
for (ModuleExtensionUsage usage : module.getExtensionUsages()) {
ModuleExtensionId moduleExtensionId;
try {
ModuleExtensionId moduleExtensionId =
moduleExtensionId =
ModuleExtensionId.create(
labelConverter.convert(usage.getExtensionBzlFile()), usage.getExtensionName());
extensionUsagesTableBuilder.put(moduleExtensionId, module.getKey(), usage);
} catch (LabelSyntaxException e) {
throw new BazelModuleResolutionFunctionException(
ExternalDepsException.withCauseAndMessage(
Expand All @@ -83,6 +83,16 @@ public SkyValue compute(SkyKey skyKey, Environment env)
usage.getLocation()),
Transience.PERSISTENT);
}
if (!moduleExtensionId.getBzlFileLabel().getRepository().isVisible()) {
throw new BazelModuleResolutionFunctionException(
ExternalDepsException.withMessage(
Code.BAD_MODULE,
"invalid label for module extension found at %s: no repo visible as '@%s' here",
usage.getLocation(),
moduleExtensionId.getBzlFileLabel().getRepository().getName()),
Transience.PERSISTENT);
}
extensionUsagesTableBuilder.put(moduleExtensionId, module.getKey(), usage);
}
}
ImmutableTable<ModuleExtensionId, ModuleKey, ModuleExtensionUsage> extensionUsagesById =
Expand Down
Expand Up @@ -329,6 +329,23 @@ public void createValue_moduleExtensions() throws Exception {
"dep~2.0~myext2~myext"));
}

@Test
public void useExtensionBadLabelFails() throws Exception {
Module root =
Module.builder()
.addExtensionUsage(createModuleExtensionUsage("@foo//:defs.bzl", "bar"))
.build();
ImmutableMap<ModuleKey, Module> depGraph = ImmutableMap.of(ModuleKey.ROOT, root);

resolutionFunctionMock.setDepGraph(depGraph);
EvaluationResult<BazelDepGraphValue> result =
evaluator.evaluate(ImmutableList.of(BazelDepGraphValue.KEY), evaluationContext);
if (!result.hasError()) {
fail("expected error about @foo not being visible, but succeeded");
}
assertThat(result.getError().toString()).contains("no repo visible as '@foo' here");
}

private static class BazelModuleResolutionFunctionMock implements SkyFunction {

private ImmutableMap<ModuleKey, Module> depGraph = ImmutableMap.of();
Expand Down

0 comments on commit 109b290

Please sign in to comment.