Skip to content

Commit 109b290

Browse files
Wyveraldcopybara-github
authored andcommitted
Fail early if use_extension has a bad label
PiperOrigin-RevId: 520627028 Change-Id: Ib42df77b02674b3ea55639e163e369afbedebce9
1 parent 9353955 commit 109b290

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelDepGraphFunction.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ public SkyValue compute(SkyKey skyKey, Environment env)
6969
PackageIdentifier.create(module.getCanonicalRepoName(), PathFragment.EMPTY_FRAGMENT),
7070
module.getRepoMappingWithBazelDepsOnly());
7171
for (ModuleExtensionUsage usage : module.getExtensionUsages()) {
72+
ModuleExtensionId moduleExtensionId;
7273
try {
73-
ModuleExtensionId moduleExtensionId =
74+
moduleExtensionId =
7475
ModuleExtensionId.create(
7576
labelConverter.convert(usage.getExtensionBzlFile()), usage.getExtensionName());
76-
extensionUsagesTableBuilder.put(moduleExtensionId, module.getKey(), usage);
7777
} catch (LabelSyntaxException e) {
7878
throw new BazelModuleResolutionFunctionException(
7979
ExternalDepsException.withCauseAndMessage(
@@ -83,6 +83,16 @@ public SkyValue compute(SkyKey skyKey, Environment env)
8383
usage.getLocation()),
8484
Transience.PERSISTENT);
8585
}
86+
if (!moduleExtensionId.getBzlFileLabel().getRepository().isVisible()) {
87+
throw new BazelModuleResolutionFunctionException(
88+
ExternalDepsException.withMessage(
89+
Code.BAD_MODULE,
90+
"invalid label for module extension found at %s: no repo visible as '@%s' here",
91+
usage.getLocation(),
92+
moduleExtensionId.getBzlFileLabel().getRepository().getName()),
93+
Transience.PERSISTENT);
94+
}
95+
extensionUsagesTableBuilder.put(moduleExtensionId, module.getKey(), usage);
8696
}
8797
}
8898
ImmutableTable<ModuleExtensionId, ModuleKey, ModuleExtensionUsage> extensionUsagesById =

src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BazelDepGraphFunctionTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,23 @@ public void createValue_moduleExtensions() throws Exception {
329329
"dep~2.0~myext2~myext"));
330330
}
331331

332+
@Test
333+
public void useExtensionBadLabelFails() throws Exception {
334+
Module root =
335+
Module.builder()
336+
.addExtensionUsage(createModuleExtensionUsage("@foo//:defs.bzl", "bar"))
337+
.build();
338+
ImmutableMap<ModuleKey, Module> depGraph = ImmutableMap.of(ModuleKey.ROOT, root);
339+
340+
resolutionFunctionMock.setDepGraph(depGraph);
341+
EvaluationResult<BazelDepGraphValue> result =
342+
evaluator.evaluate(ImmutableList.of(BazelDepGraphValue.KEY), evaluationContext);
343+
if (!result.hasError()) {
344+
fail("expected error about @foo not being visible, but succeeded");
345+
}
346+
assertThat(result.getError().toString()).contains("no repo visible as '@foo' here");
347+
}
348+
332349
private static class BazelModuleResolutionFunctionMock implements SkyFunction {
333350

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

0 commit comments

Comments
 (0)