Skip to content

Commit 6c61110

Browse files
Wyveraldfmeum
andauthored
Add module_ctx.extension_metadata (bazelbuild#18174)
Fixes bazelbuild#17908 Closes bazelbuild#17970. PiperOrigin-RevId: 525134299 Change-Id: I9088e3f4561c0c27135cfdd1e5be8390ea8da7eb Co-authored-by: Fabian Meumertzheim <[email protected]>
1 parent 755cf95 commit 6c61110

14 files changed

+1085
-11
lines changed

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BUILD

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ java_library(
142142
"Discovery.java",
143143
"GsonTypeAdapterUtil.java",
144144
"ModuleExtensionContext.java",
145+
"ModuleExtensionMetadata.java",
145146
"ModuleFileFunction.java",
146147
"ModuleFileGlobals.java",
147148
"Selection.java",
@@ -168,7 +169,6 @@ java_library(
168169
"//src/main/java/com/google/devtools/build/lib/cmdline",
169170
"//src/main/java/com/google/devtools/build/lib/events",
170171
"//src/main/java/com/google/devtools/build/lib/packages",
171-
"//src/main/java/com/google/devtools/build/lib/packages/semantics",
172172
"//src/main/java/com/google/devtools/build/lib/rules:repository/repository_directory_value",
173173
"//src/main/java/com/google/devtools/build/lib/rules:repository/repository_function",
174174
"//src/main/java/com/google/devtools/build/lib/skyframe:bzl_load_value",

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

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.common.collect.ImmutableBiMap;
1919
import com.google.common.collect.ImmutableList;
2020
import com.google.common.collect.ImmutableMap;
21+
import com.google.common.collect.ImmutableSet;
2122
import com.google.gson.Gson;
2223
import com.google.gson.TypeAdapter;
2324
import com.google.gson.TypeAdapterFactory;
@@ -29,8 +30,10 @@
2930
import java.lang.reflect.Type;
3031
import java.util.ArrayList;
3132
import java.util.LinkedHashMap;
33+
import java.util.LinkedHashSet;
3234
import java.util.List;
3335
import java.util.Map;
36+
import java.util.Set;
3437
import java.util.function.Function;
3538
import javax.annotation.Nullable;
3639
import net.starlark.java.eval.Dict;
@@ -92,6 +95,14 @@ private DelegateTypeAdapterFactory(
9295
raw -> new ArrayList<>((List<?>) raw),
9396
delegate -> ImmutableList.copyOf((List<?>) delegate));
9497

98+
public static final TypeAdapterFactory IMMUTABLE_SET =
99+
new DelegateTypeAdapterFactory<>(
100+
ImmutableSet.class,
101+
Set.class,
102+
LinkedHashSet.class,
103+
raw -> new LinkedHashSet<>((Set<?>) raw),
104+
delegate -> ImmutableSet.copyOf((Set<?>) delegate));
105+
95106
@SuppressWarnings("unchecked")
96107
@Override
97108
@Nullable

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

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static com.google.devtools.build.lib.bazel.bzlmod.DelegateTypeAdapterFactory.IMMUTABLE_BIMAP;
1919
import static com.google.devtools.build.lib.bazel.bzlmod.DelegateTypeAdapterFactory.IMMUTABLE_LIST;
2020
import static com.google.devtools.build.lib.bazel.bzlmod.DelegateTypeAdapterFactory.IMMUTABLE_MAP;
21+
import static com.google.devtools.build.lib.bazel.bzlmod.DelegateTypeAdapterFactory.IMMUTABLE_SET;
2122

2223
import com.google.common.base.Splitter;
2324
import com.google.devtools.build.lib.bazel.bzlmod.Version.ParseException;
@@ -95,6 +96,7 @@ public ModuleKey read(JsonReader jsonReader) throws IOException {
9596
.registerTypeAdapterFactory(IMMUTABLE_MAP)
9697
.registerTypeAdapterFactory(IMMUTABLE_LIST)
9798
.registerTypeAdapterFactory(IMMUTABLE_BIMAP)
99+
.registerTypeAdapterFactory(IMMUTABLE_SET)
98100
.registerTypeAdapter(Version.class, VERSION_TYPE_ADAPTER)
99101
.registerTypeAdapter(ModuleKey.class, MODULE_KEY_TYPE_ADAPTER)
100102
.registerTypeAdapter(AttributeValues.class, new AttributeValuesAdapter())

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

+2
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ public Builder addExtensionUsage(ModuleExtensionUsage value) {
261261
return this;
262262
}
263263

264+
abstract ModuleKey getKey();
265+
264266
abstract String getName();
265267

266268
abstract Optional<String> getRepoName();

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

+66
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import net.starlark.java.annot.StarlarkBuiltin;
3030
import net.starlark.java.annot.StarlarkMethod;
3131
import net.starlark.java.eval.EvalException;
32+
import net.starlark.java.eval.NoneType;
33+
import net.starlark.java.eval.Sequence;
3234
import net.starlark.java.eval.StarlarkList;
3335
import net.starlark.java.eval.StarlarkSemantics;
3436

@@ -117,4 +119,68 @@ public StarlarkList<StarlarkBazelModule> getModules() {
117119
public boolean isDevDependency(TypeCheckedTag tag) {
118120
return tag.isDevDependency();
119121
}
122+
123+
@StarlarkMethod(
124+
name = "extension_metadata",
125+
doc =
126+
"Constructs an opaque object that can be returned from the module extension's"
127+
+ " implementation function to provide metadata about the repositories generated by"
128+
+ " the extension to Bazel.",
129+
parameters = {
130+
@Param(
131+
name = "root_module_direct_deps",
132+
doc =
133+
"The names of the repositories that the extension considers to be direct"
134+
+ " dependencies of the root module. If the root module imports additional"
135+
+ " repositories or does not import all of these repositories via <a"
136+
+ " href=\"../globals/module.html#use_repo\"><code>use_repo</code></a>, Bazel"
137+
+ " will print a warning and a fixup command when the extension is"
138+
+ " evaluated.<p>If one of <code>root_module_direct_deps</code> and"
139+
+ " <code>root_module_direct_dev_deps</code> is specified, the other has to be"
140+
+ " as well. The lists specified by these two parameters must be"
141+
+ " disjoint.<p>Exactly one of <code>root_module_direct_deps</code> and"
142+
+ " <code>root_module_direct_dev_deps</code> can be set to the special value"
143+
+ " <code>\"all\"</code>, which is treated as if a list with the names of"
144+
+ " allrepositories generated by the extension was specified as the value.",
145+
positional = false,
146+
named = true,
147+
defaultValue = "None",
148+
allowedTypes = {
149+
@ParamType(type = Sequence.class, generic1 = String.class),
150+
@ParamType(type = String.class),
151+
@ParamType(type = NoneType.class)
152+
}),
153+
@Param(
154+
name = "root_module_direct_dev_deps",
155+
doc =
156+
"The names of the repositories that the extension considers to be direct dev"
157+
+ " dependencies of the root module. If the root module imports additional"
158+
+ " repositories or does not import all of these repositories via <a"
159+
+ " href=\"../globals/module.html#use_repo\"><code>use_repo</code></a> on an"
160+
+ " extension proxy created with <code><a"
161+
+ " href=\"../globals/module.html#use_extension>use_extension</a>(...,"
162+
+ " dev_dependency = True)</code>, Bazel will print a warning and a fixup"
163+
+ " command when the extension is evaluated.<p>If one of"
164+
+ " <code>root_module_direct_deps</code> and"
165+
+ " <code>root_module_direct_dev_deps</code> is specified, the other has to be"
166+
+ " as well. The lists specified by these two parameters must be"
167+
+ " disjoint.<p>Exactly one of <code>root_module_direct_deps</code> and"
168+
+ " <code>root_module_direct_dev_deps</code> can be set to the special value"
169+
+ " <code>\"all\"</code>, which is treated as if a list with the names of"
170+
+ " allrepositories generated by the extension was specified as the value.",
171+
positional = false,
172+
named = true,
173+
defaultValue = "None",
174+
allowedTypes = {
175+
@ParamType(type = Sequence.class, generic1 = String.class),
176+
@ParamType(type = String.class),
177+
@ParamType(type = NoneType.class)
178+
}),
179+
})
180+
public ModuleExtensionMetadata extensionMetadata(
181+
Object rootModuleDirectDepsUnchecked, Object rootModuleDirectDevDepsUnchecked)
182+
throws EvalException {
183+
return ModuleExtensionMetadata.create(
184+
rootModuleDirectDepsUnchecked, rootModuleDirectDevDepsUnchecked);
185+
}
120186
}

0 commit comments

Comments
 (0)