Skip to content

Commit 79d4090

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 79d4090

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,12 @@ public void multipleModules() throws Exception {
358358
modulesRoot.getRelative("@ext.1.0/defs.bzl").getPathString(),
359359
"load('@data_repo//:defs.bzl','data_repo')",
360360
"def _ext_impl(ctx):",
361-
" data_str = 'modules:'",
361+
" data_str = ''",
362362
" for mod in ctx.modules:",
363+
" data_str += mod.name + '@' + mod.version + (' (root): ' if mod.is_root else ': ')",
363364
" for tag in mod.tags.tag:",
364-
" data_str += ' ' + tag.data",
365+
" data_str += tag.data",
366+
" data_str += '\\n'",
365367
" data_repo(name='ext_repo',data=data_str)",
366368
"tag=tag_class(attrs={'data':attr.string()})",
367369
"ext=module_extension(implementation=_ext_impl,tag_classes={'tag':tag})");
@@ -373,7 +375,7 @@ public void multipleModules() throws Exception {
373375
throw result.getError().getException();
374376
}
375377
assertThat(result.get(skyKey).getModule().getGlobal("data"))
376-
378+
377379
}
378380

379381
@Test

0 commit comments

Comments
 (0)