Skip to content

Commit aa3b2d2

Browse files
SalmaSamycopybara-github
authored andcommitted
Fix NPE in bazel mod graph with extension_filter
Fixes: #27839 PiperOrigin-RevId: 840250358 Change-Id: I7edf039575a93b28926b194523d6a698b7dc74a0
1 parent 965265e commit aa3b2d2

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,17 @@ ImmutableMap<ModuleKey, ResultNode> expandPathsToTargets(
275275
}
276276

277277
ModuleKey currentModuleKey = queue.pop();
278+
279+
if (targets.contains(currentModuleKey)
280+
&& !(findSinglePath && foundTargets.contains(currentModuleKey))) {
281+
addPathToResultGraph(
282+
resultGraph, bfsParentMap, bfsParentMap.get(currentModuleKey), currentModuleKey);
283+
foundTargets.add(currentModuleKey);
284+
if (findSinglePath && foundTargets.containsAll(targets)) {
285+
break;
286+
}
287+
}
288+
278289
AugmentedModule module = depGraph.get(currentModuleKey);
279290
ImmutableSortedSet<ModuleKey> dependencies =
280291
module.getAllDeps(options.includeUnused).keySet().stream()

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,4 +1015,66 @@ public void testModCommandGraph_withCycle() throws ParseException, IOException {
10151015
"<root> ([email protected])", "└───[email protected] ", " └───[email protected] ", " └───[email protected] (cycle) ", "")
10161016
.inOrder();
10171017
}
1018+
1019+
@Test
1020+
public void testGraphWithExtensionFilterOnRoot() throws Exception {
1021+
ImmutableMap<ModuleKey, AugmentedModule> depGraph =
1022+
new ImmutableMap.Builder<ModuleKey, AugmentedModule>()
1023+
.put(
1024+
buildAugmentedModule(ModuleKey.ROOT, "main", Version.parse("1.0"), true)
1025+
.buildEntry())
1026+
.buildOrThrow();
1027+
1028+
ModuleExtensionId mavenId = createExtensionId("extensions", "maven");
1029+
ImmutableTable<ModuleExtensionId, ModuleKey, ModuleExtensionUsage> extensionUsages =
1030+
new ImmutableTable.Builder<ModuleExtensionId, ModuleKey, ModuleExtensionUsage>()
1031+
.put(
1032+
mavenId,
1033+
ModuleKey.ROOT,
1034+
ModuleExtensionUsage.builder()
1035+
.setExtensionBzlFile("//extensions:extensions.bzl")
1036+
.setExtensionName("maven")
1037+
.setRepoOverrides(ImmutableMap.of())
1038+
.addProxy(
1039+
ModuleExtensionUsage.Proxy.builder()
1040+
.setLocation(Location.fromFileLineColumn("MODULE.bazel", 1, 1))
1041+
.setImports(ImmutableBiMap.of("repo1", "repo1"))
1042+
.setDevDependency(false)
1043+
.setContainingModuleFilePath(LabelConstants.MODULE_DOT_BAZEL_FILE_NAME)
1044+
.build())
1045+
.build())
1046+
.buildOrThrow();
1047+
1048+
ImmutableSetMultimap<ModuleExtensionId, String> extensionRepos =
1049+
new ImmutableSetMultimap.Builder<ModuleExtensionId, String>()
1050+
.putAll(mavenId, ImmutableSet.of("repo1"))
1051+
.build();
1052+
1053+
ModOptions options = ModOptions.getDefaultOptions();
1054+
options.outputFormat = OutputFormat.TEXT;
1055+
options.extensionInfo = ExtensionShow.ALL;
1056+
1057+
File file = File.createTempFile("output_text_repro", "txt");
1058+
file.deleteOnExit();
1059+
Writer writer = new OutputStreamWriter(new FileOutputStream(file), UTF_8);
1060+
1061+
ModExecutor executor =
1062+
new ModExecutor(
1063+
depGraph,
1064+
extensionUsages,
1065+
extensionRepos,
1066+
Optional.of(MaybeCompleteSet.copyOf(ImmutableSet.of(mavenId))),
1067+
options,
1068+
writer);
1069+
1070+
// This should not throw NPE
1071+
executor.graph(ImmutableSet.of(ModuleKey.ROOT));
1072+
writer.close();
1073+
1074+
List<String> textOutput = Files.readAllLines(file.toPath());
1075+
assertThat(textOutput)
1076+
.containsExactly(
1077+
"<root> ([email protected])", "└───$@@//extensions:extensions%maven ", " └───repo1", "")
1078+
.inOrder();
1079+
}
10181080
}

0 commit comments

Comments
 (0)