Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is_root struct field to bazel_module #15792

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class StarlarkBazelModule implements StarlarkValue {
private final String name;
private final String version;
private final Tags tags;
private final boolean isRootModule;

@StarlarkBuiltin(
name = "bazel_module_tags",
Expand Down Expand Up @@ -80,10 +81,11 @@ public String getErrorMessageForUnknownField(String field) {
}
}

private StarlarkBazelModule(String name, String version, Tags tags) {
private StarlarkBazelModule(String name, String version, Tags tags, boolean isRootModule) {
this.name = name;
this.version = version;
this.tags = tags;
this.isRootModule = isRootModule;
}

/**
Expand Down Expand Up @@ -128,7 +130,8 @@ public static StarlarkBazelModule create(
return new StarlarkBazelModule(
module.getName(),
module.getVersion().getOriginal(),
new Tags(Maps.transformValues(typeCheckedTags, StarlarkList::immutableCopyOf)));
new Tags(Maps.transformValues(typeCheckedTags, StarlarkList::immutableCopyOf)),
module.getKey().equals(ModuleKey.ROOT));
}

@Override
Expand All @@ -153,4 +156,12 @@ public String getVersion() {
public Tags getTags() {
return tags;
}

@StarlarkMethod(
name = "is_root",
structField = true,
doc = "Whether this module is the root module.")
public boolean isRoot() {
return isRootModule;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ public void simpleExtension() throws Exception {
public void multipleModules() throws Exception {
scratch.file(
workspaceRoot.getRelative("MODULE.bazel").getPathString(),
"module(name='root',version='1.0')",
"bazel_dep(name='ext',version='1.0')",
"bazel_dep(name='foo',version='1.0')",
"bazel_dep(name='bar',version='2.0')",
Expand Down Expand Up @@ -358,10 +359,12 @@ public void multipleModules() throws Exception {
modulesRoot.getRelative("@ext.1.0/defs.bzl").getPathString(),
"load('@data_repo//:defs.bzl','data_repo')",
"def _ext_impl(ctx):",
" data_str = 'modules:'",
" data_str = ''",
" for mod in ctx.modules:",
" data_str += mod.name + '@' + mod.version + (' (root): ' if mod.is_root else ': ')",
" for tag in mod.tags.tag:",
" data_str += ' ' + tag.data",
" data_str += tag.data",
" data_str += '\\n'",
" data_repo(name='ext_repo',data=data_str)",
"tag=tag_class(attrs={'data':attr.string()})",
"ext=module_extension(implementation=_ext_impl,tag_classes={'tag':tag})");
Expand All @@ -373,7 +376,8 @@ public void multipleModules() throws Exception {
throw result.getError().getException();
}
assertThat(result.get(skyKey).getModule().getGlobal("data"))
.isEqualTo("modules: root foo@1.0 bar@2.0 quux@2.0");
.isEqualTo(
"root@1.0 (root): root\nfoo@1.0: foo@1.0\nbar@2.0: bar@2.0\nquux@2.0: quux@2.0\n");
}

@Test
Expand Down