Skip to content

Commit 07cddaf

Browse files
comiuscopybara-github
authored andcommitted
Remove support for returning struct providers from aspects
Downstream run: https://buildkite.com/bazel/bazel-at-head-plus-downstream/builds/4596 The only failure in IntelliJ was fixed in bazelbuild/intellij#7606 RELNOTES[INC]: struct providers are not supported in aspects PiperOrigin-RevId: 747836848 Change-Id: Iab2d2eccc455173c53ea160e0a15d90339e6bbb6
1 parent a7da0d8 commit 07cddaf

File tree

3 files changed

+19
-54
lines changed

3 files changed

+19
-54
lines changed

src/main/java/com/google/devtools/build/lib/skyframe/StarlarkAspectFactory.java

+13-47
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.devtools.build.lib.analysis.RequiredConfigFragmentsProvider;
2323
import com.google.devtools.build.lib.analysis.RuleContext;
2424
import com.google.devtools.build.lib.analysis.StarlarkProviderValidationUtil;
25-
import com.google.devtools.build.lib.analysis.starlark.StarlarkRuleConfiguredTargetUtil;
2625
import com.google.devtools.build.lib.analysis.starlark.StarlarkRuleContext;
2726
import com.google.devtools.build.lib.cmdline.Label;
2827
import com.google.devtools.build.lib.cmdline.RepositoryName;
@@ -31,14 +30,10 @@
3130
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
3231
import com.google.devtools.build.lib.packages.StarlarkDefinedAspect;
3332
import com.google.devtools.build.lib.packages.StarlarkInfo;
34-
import com.google.devtools.build.lib.packages.StructImpl;
3533
import com.google.devtools.build.lib.packages.StructProvider;
36-
import java.util.Map;
3734
import javax.annotation.Nullable;
38-
import net.starlark.java.eval.Dict;
3935
import net.starlark.java.eval.EvalException;
4036
import net.starlark.java.eval.Starlark;
41-
import net.starlark.java.eval.StarlarkValue;
4237

4338
/** A factory for aspects that are defined in Starlark. */
4439
public class StarlarkAspectFactory implements ConfiguredAspectFactory {
@@ -87,13 +82,15 @@ public ConfiguredAspect create(
8782

8883
if (ruleContext.hasErrors() && !allowAnalysisFailures) {
8984
return errorConfiguredAspect(ruleContext, requiredConfigFragments);
90-
} else if (!(aspectStarlarkObject instanceof StructImpl)
91-
&& !(aspectStarlarkObject instanceof Iterable)
85+
} else if (aspectStarlarkObject instanceof Info info
86+
&& info.getProvider().getKey().equals(StructProvider.STRUCT.getKey())) {
87+
ruleContext.ruleError(
88+
"Returning a struct from an aspect implementation function is deprecated.");
89+
} else if (!(aspectStarlarkObject instanceof Iterable)
9290
&& !(aspectStarlarkObject instanceof Info)) {
9391
ruleContext.ruleError(
9492
String.format(
95-
"Aspect implementation should return a struct, a list, or a provider "
96-
+ "instance, but got %s",
93+
"Aspect implementation should return a list, or a provider instance, but got %s",
9794
Starlark.type(aspectStarlarkObject)));
9895
return errorConfiguredAspect(ruleContext, requiredConfigFragments);
9996
}
@@ -127,37 +124,17 @@ private static ConfiguredAspect createAspect(
127124
if (requiredConfigFragments != null) {
128125
builder.addProvider(requiredConfigFragments);
129126
}
130-
if (aspectStarlarkObject instanceof Iterable<?> iterable) {
127+
// not instanceof Info, because OutputGroupInfo is both Iterable and Info
128+
if (!(aspectStarlarkObject instanceof Info)
129+
&& aspectStarlarkObject instanceof Iterable<?> iterable) {
131130
addDeclaredProviders(builder, iterable);
132131
} else {
133-
// Either an old-style struct or a single declared provider (not in a list)
132+
// A single declared provider (not in a list)
134133
Info info = (Info) aspectStarlarkObject;
135-
if (info.getProvider().getKey().equals(StructProvider.STRUCT.getKey())) {
136-
// Old-style struct, that may contain declared providers.
137-
StructImpl struct = (StructImpl) aspectStarlarkObject;
138-
for (String field : struct.getFieldNames()) {
139-
if (field.equals("output_groups")) {
140-
addOutputGroups(struct.getValue(field), builder);
141-
} else if (field.equals("providers")) {
142-
Object providers = struct.getValue(field);
143-
// TODO(adonovan): can we be more specific than iterable, and use Sequence.cast?
144-
if (!(providers instanceof Iterable)) {
145-
throw Starlark.errorf(
146-
"The value for \"providers\" should be a list of declared providers, "
147-
+ "got %s instead",
148-
Starlark.type(providers));
149-
}
150-
addDeclaredProviders(builder, (Iterable<?>) providers);
151-
} else {
152-
builder.addStarlarkTransitiveInfo(field, struct.getValue(field));
153-
}
154-
}
155-
} else {
156-
if (info instanceof StarlarkInfo starlarkInfo) {
157-
info = starlarkInfo.unsafeOptimizeMemoryLayout();
158-
}
159-
builder.addStarlarkDeclaredProvider(info);
134+
if (info instanceof StarlarkInfo starlarkInfo) {
135+
info = starlarkInfo.unsafeOptimizeMemoryLayout();
160136
}
137+
builder.addStarlarkDeclaredProvider(info);
161138
}
162139

163140
ConfiguredAspect configuredAspect = builder.build();
@@ -182,15 +159,4 @@ private static void addDeclaredProviders(
182159
i++;
183160
}
184161
}
185-
186-
private static void addOutputGroups(Object outputGroups, ConfiguredAspect.Builder builder)
187-
throws EvalException {
188-
for (Map.Entry<String, StarlarkValue> entry :
189-
Dict.cast(outputGroups, String.class, StarlarkValue.class, "output_groups").entrySet()) {
190-
builder.addOutputGroup(
191-
entry.getKey(),
192-
StarlarkRuleConfiguredTargetUtil.convertToOutputGroupValue(
193-
entry.getKey(), entry.getValue()));
194-
}
195-
}
196162
}

src/test/java/com/google/devtools/build/lib/starlark/StarlarkDefinedAspectsTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -985,8 +985,7 @@ def _impl(target, ctx):
985985
// expect to fail.
986986
}
987987
assertContainsEvent(
988-
"Aspect implementation should return a struct, a list, or a provider "
989-
+ "instance, but got int");
988+
"Aspect implementation should return a list, or a provider instance, but got int");
990989
}
991990

992991
@Test

src/test/shell/bazel/bazel_proto_library_test.sh

+5-5
Original file line numberDiff line numberDiff line change
@@ -413,22 +413,22 @@ EOF
413413
cat > java/proto/my_rule_with_aspect.bzl <<EOF
414414
load("@rules_java//java/common:java_common.bzl", "java_common")
415415
load("@rules_java//java/common:java_info.bzl", "JavaInfo")
416+
417+
MyInfo = provider()
416418
def _my_rule_impl(ctx):
417419
aspect_java_infos = []
418420
for dep in ctx.attr.deps:
419-
aspect_java_infos += dep.my_aspect_providers
421+
aspect_java_infos += dep[MyInfo].my_aspect_providers
420422
merged_java_info = java_common.merge(aspect_java_infos)
421423
for jar in merged_java_info.transitive_runtime_jars.to_list():
422424
print('Transitive runtime jar', jar)
423425
424426
def _my_aspect_impl(target, ctx):
425427
aspect_java_infos = []
426428
for dep in ctx.rule.attr.deps:
427-
aspect_java_infos += dep.my_aspect_providers
429+
aspect_java_infos += dep[MyInfo].my_aspect_providers
428430
aspect_java_infos.append(target[JavaInfo])
429-
return struct(
430-
my_aspect_providers = aspect_java_infos
431-
)
431+
return MyInfo(my_aspect_providers = aspect_java_infos)
432432
433433
my_aspect = aspect(
434434
attr_aspects = ['deps'],

0 commit comments

Comments
 (0)