Skip to content

Commit 594962c

Browse files
ckolli5fmeum
andauthored
Add is_root struct field to bazel_module (bazelbuild#15815)
This allows module extensions to check whether a given module is the root module, which is necessary to implement the analogues of --check_direct_dependencies and archive_override/git_override for dependencies managed by extensions. Closes bazelbuild#15792. PiperOrigin-RevId: 459058350 Change-Id: I4a08ce80a636e2cb2791323476fdccddf4131de0 Co-authored-by: Fabian Meumertzheim <[email protected]>
1 parent d4663a1 commit 594962c

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class StarlarkBazelModule implements StarlarkValue {
4444
private final String name;
4545
private final String version;
4646
private final Tags tags;
47+
private final boolean isRootModule;
4748

4849
@StarlarkBuiltin(
4950
name = "bazel_module_tags",
@@ -82,10 +83,11 @@ public String getErrorMessageForUnknownField(String field) {
8283
}
8384
}
8485

85-
private StarlarkBazelModule(String name, String version, Tags tags) {
86+
private StarlarkBazelModule(String name, String version, Tags tags, boolean isRootModule) {
8687
this.name = name;
8788
this.version = version;
8889
this.tags = tags;
90+
this.isRootModule = isRootModule;
8991
}
9092

9193
/**
@@ -143,7 +145,8 @@ public static StarlarkBazelModule create(
143145
return new StarlarkBazelModule(
144146
module.getName(),
145147
module.getVersion().getOriginal(),
146-
new Tags(Maps.transformValues(typeCheckedTags, StarlarkList::immutableCopyOf)));
148+
new Tags(Maps.transformValues(typeCheckedTags, StarlarkList::immutableCopyOf)),
149+
module.getKey().equals(ModuleKey.ROOT));
147150
}
148151

149152
@Override
@@ -168,4 +171,12 @@ public String getVersion() {
168171
public Tags getTags() {
169172
return tags;
170173
}
174+
175+
@StarlarkMethod(
176+
name = "is_root",
177+
structField = true,
178+
doc = "Whether this module is the root module.")
179+
public boolean isRoot() {
180+
return isRootModule;
181+
}
171182
}

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ public void simpleExtension() throws Exception {
314314
public void multipleModules() throws Exception {
315315
scratch.file(
316316
workspaceRoot.getRelative("MODULE.bazel").getPathString(),
317+
"module(name='root',version='1.0')",
317318
"bazel_dep(name='ext',version='1.0')",
318319
"bazel_dep(name='foo',version='1.0')",
319320
"bazel_dep(name='bar',version='2.0')",
@@ -363,10 +364,12 @@ public void multipleModules() throws Exception {
363364
modulesRoot.getRelative("ext.1.0/defs.bzl").getPathString(),
364365
"load('@data_repo//:defs.bzl','data_repo')",
365366
"def _ext_impl(ctx):",
366-
" data_str = 'modules:'",
367+
" data_str = ''",
367368
" for mod in ctx.modules:",
369+
" data_str += mod.name + '@' + mod.version + (' (root): ' if mod.is_root else ': ')",
368370
" for tag in mod.tags.tag:",
369-
" data_str += ' ' + tag.data",
371+
" data_str += tag.data",
372+
" data_str += '\\n'",
370373
" data_repo(name='ext_repo',data=data_str)",
371374
"tag=tag_class(attrs={'data':attr.string()})",
372375
"ext=module_extension(implementation=_ext_impl,tag_classes={'tag':tag})");
@@ -378,7 +381,8 @@ public void multipleModules() throws Exception {
378381
throw result.getError().getException();
379382
}
380383
assertThat(result.get(skyKey).getModule().getGlobal("data"))
381-
384+
.isEqualTo(
385+
382386
}
383387

384388
@Test

0 commit comments

Comments
 (0)