Skip to content

Commit 22bd362

Browse files
committed
Add is_root struct field to bazel_module
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.
1 parent 0152338 commit 22bd362

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class StarlarkBazelModule implements StarlarkValue {
4242
private final String name;
4343
private final String version;
4444
private final Tags tags;
45+
private final boolean isRootModule;
4546

4647
@StarlarkBuiltin(
4748
name = "bazel_module_tags",
@@ -80,10 +81,11 @@ public String getErrorMessageForUnknownField(String field) {
8081
}
8182
}
8283

83-
private StarlarkBazelModule(String name, String version, Tags tags) {
84+
private StarlarkBazelModule(String name, String version, Tags tags, boolean isRootModule) {
8485
this.name = name;
8586
this.version = version;
8687
this.tags = tags;
88+
this.isRootModule = isRootModule;
8789
}
8890

8991
/**
@@ -128,7 +130,8 @@ public static StarlarkBazelModule create(
128130
return new StarlarkBazelModule(
129131
module.getName(),
130132
module.getVersion().getOriginal(),
131-
new Tags(Maps.transformValues(typeCheckedTags, StarlarkList::immutableCopyOf)));
133+
new Tags(Maps.transformValues(typeCheckedTags, StarlarkList::immutableCopyOf)),
134+
module.getKey().equals(ModuleKey.ROOT));
132135
}
133136

134137
@Override
@@ -153,4 +156,12 @@ public String getVersion() {
153156
public Tags getTags() {
154157
return tags;
155158
}
159+
160+
@StarlarkMethod(
161+
name = "is_root",
162+
structField = true,
163+
doc = "Whether this module is the root module.")
164+
public boolean isRoot() {
165+
return isRootModule;
166+
}
156167
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ public void simpleExtension() throws Exception {
309309
public void multipleModules() throws Exception {
310310
scratch.file(
311311
workspaceRoot.getRelative("MODULE.bazel").getPathString(),
312+
"module(name='root',version='1.0')",
312313
"bazel_dep(name='ext',version='1.0')",
313314
"bazel_dep(name='foo',version='1.0')",
314315
"bazel_dep(name='bar',version='2.0')",
@@ -358,10 +359,12 @@ public void multipleModules() throws Exception {
358359
modulesRoot.getRelative("@ext.1.0/defs.bzl").getPathString(),
359360
"load('@data_repo//:defs.bzl','data_repo')",
360361
"def _ext_impl(ctx):",
361-
" data_str = 'modules:'",
362+
" data_str = ''",
362363
" for mod in ctx.modules:",
364+
" data_str += mod.name + '@' + mod.version + (' (root): ' if mod.is_root else ': ')",
363365
" for tag in mod.tags.tag:",
364-
" data_str += ' ' + tag.data",
366+
" data_str += tag.data",
367+
" data_str += '\\n'",
365368
" data_repo(name='ext_repo',data=data_str)",
366369
"tag=tag_class(attrs={'data':attr.string()})",
367370
"ext=module_extension(implementation=_ext_impl,tag_classes={'tag':tag})");
@@ -373,7 +376,8 @@ public void multipleModules() throws Exception {
373376
throw result.getError().getException();
374377
}
375378
assertThat(result.get(skyKey).getModule().getGlobal("data"))
376-
379+
.isEqualTo(
380+
377381
}
378382

379383
@Test

0 commit comments

Comments
 (0)