From 22bd3624804a222d2dd5823ebdd9b33d2a4c6e24 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Sat, 2 Jul 2022 18:44:11 +0200 Subject: [PATCH] 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. --- .../lib/bazel/bzlmod/StarlarkBazelModule.java | 15 +++++++++++++-- .../bzlmod/ModuleExtensionResolutionTest.java | 10 +++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/StarlarkBazelModule.java b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/StarlarkBazelModule.java index da0f70def85bed..7491a9ea449211 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/StarlarkBazelModule.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/StarlarkBazelModule.java @@ -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", @@ -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; } /** @@ -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 @@ -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; + } } diff --git a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionResolutionTest.java b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionResolutionTest.java index 2ec6741447d3eb..f9c816986ae089 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionResolutionTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionResolutionTest.java @@ -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')", @@ -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})"); @@ -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