diff --git a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleConfiguredTargetUtil.java b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleConfiguredTargetUtil.java index 54eb63f7ddd156..b652357f0f520a 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleConfiguredTargetUtil.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleConfiguredTargetUtil.java @@ -318,6 +318,16 @@ private static void addProviders( // Use the creation location of this struct as a better reference in error messages loc = info.getCreationLoc(); if (info.getProvider().getKey().equals(StructProvider.STRUCT.getKey())) { + + if (context.getSkylarkSemantics().incompatibleDisallowStructProviderSyntax()) { + throw new EvalException( + loc, + "Returning a struct from a rule implementation function is deprecated and will " + + "be removed soon. It may be temporarily re-enabled by setting " + + "--incompatible_disallow_struct_provider_syntax=false . See " + + "https://github.com/bazelbuild/bazel/issues/7347 for details."); + } + // Old-style struct, but it may contain declared providers StructImpl struct = (StructImpl) target; oldStyleProviders = struct; diff --git a/src/main/java/com/google/devtools/build/lib/packages/SkylarkSemanticsOptions.java b/src/main/java/com/google/devtools/build/lib/packages/SkylarkSemanticsOptions.java index 695a2e7f673b66..30ea540e3b6a7d 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/SkylarkSemanticsOptions.java +++ b/src/main/java/com/google/devtools/build/lib/packages/SkylarkSemanticsOptions.java @@ -325,6 +325,20 @@ public class SkylarkSemanticsOptions extends OptionsBase implements Serializable ) public boolean incompatibleDisallowLoadLabelsToCrossPackageBoundaries; + @Option( + name = "incompatible_disallow_struct_provider_syntax", + defaultValue = "false", + documentationCategory = OptionDocumentationCategory.SKYLARK_SEMANTICS, + effectTags = {OptionEffectTag.BUILD_FILE_SEMANTICS}, + metadataTags = { + OptionMetadataTag.INCOMPATIBLE_CHANGE, + OptionMetadataTag.TRIGGERED_BY_ALL_INCOMPATIBLE_CHANGES + }, + help = + "If set to true, rule implementation functions may not return a struct. They must " + + "instead return a list of provider instances.") + public boolean incompatibleDisallowStructProviderSyntax; + @Option( name = "incompatible_generate_javacommon_source_jar", defaultValue = "true", @@ -555,6 +569,7 @@ public SkylarkSemantics toSkylarkSemantics() { .incompatibleDisallowLoadLabelsToCrossPackageBoundaries( incompatibleDisallowLoadLabelsToCrossPackageBoundaries) .incompatibleDisallowOldStyleArgsAdd(incompatibleDisallowOldStyleArgsAdd) + .incompatibleDisallowStructProviderSyntax(incompatibleDisallowStructProviderSyntax) .incompatibleExpandDirectories(incompatibleExpandDirectories) .incompatibleGenerateJavaCommonSourceJar(incompatibleGenerateJavaCommonSourceJar) .incompatibleNewActionsApi(incompatibleNewActionsApi) diff --git a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkSemantics.java b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkSemantics.java index 039600715ecca4..3a7882a8db74b7 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkSemantics.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkSemantics.java @@ -158,6 +158,8 @@ public boolean flagValue(FlagIdentifier flagIdentifier) { public abstract boolean incompatibleDisallowOldStyleArgsAdd(); + public abstract boolean incompatibleDisallowStructProviderSyntax(); + public abstract boolean incompatibleExpandDirectories(); public abstract boolean incompatibleGenerateJavaCommonSourceJar(); @@ -225,6 +227,7 @@ public static Builder builderWithDefaults() { .incompatibleDisallowLegacyJavaInfo(false) .incompatibleDisallowLoadLabelsToCrossPackageBoundaries(false) .incompatibleDisallowOldStyleArgsAdd(false) + .incompatibleDisallowStructProviderSyntax(false) .incompatibleExpandDirectories(true) .incompatibleGenerateJavaCommonSourceJar(true) .incompatibleNewActionsApi(false) @@ -290,6 +293,8 @@ public abstract static class Builder { public abstract Builder incompatibleDisallowOldStyleArgsAdd(boolean value); + public abstract Builder incompatibleDisallowStructProviderSyntax(boolean value); + public abstract Builder incompatibleExpandDirectories(boolean value); public abstract Builder incompatibleGenerateJavaCommonSourceJar(boolean value); diff --git a/src/test/java/com/google/devtools/build/lib/packages/SkylarkSemanticsConsistencyTest.java b/src/test/java/com/google/devtools/build/lib/packages/SkylarkSemanticsConsistencyTest.java index 55065d5cbc8d2a..d2333d14dd1793 100644 --- a/src/test/java/com/google/devtools/build/lib/packages/SkylarkSemanticsConsistencyTest.java +++ b/src/test/java/com/google/devtools/build/lib/packages/SkylarkSemanticsConsistencyTest.java @@ -145,6 +145,7 @@ private static SkylarkSemanticsOptions buildRandomOptions(Random rand) throws Ex "--incompatible_disallow_legacy_java_provider=" + rand.nextBoolean(), "--incompatible_disallow_load_labels_to_cross_package_boundaries=" + rand.nextBoolean(), "--incompatible_disallow_old_style_args_add=" + rand.nextBoolean(), + "--incompatible_disallow_struct_provider_syntax=" + rand.nextBoolean(), "--incompatible_expand_directories=" + rand.nextBoolean(), "--incompatible_generate_javacommon_source_jar=" + rand.nextBoolean(), "--incompatible_new_actions_api=" + rand.nextBoolean(), @@ -192,6 +193,7 @@ private static SkylarkSemantics buildRandomSemantics(Random rand) { .incompatibleDisallowLegacyJavaProvider(rand.nextBoolean()) .incompatibleDisallowLoadLabelsToCrossPackageBoundaries(rand.nextBoolean()) .incompatibleDisallowOldStyleArgsAdd(rand.nextBoolean()) + .incompatibleDisallowStructProviderSyntax(rand.nextBoolean()) .incompatibleExpandDirectories(rand.nextBoolean()) .incompatibleGenerateJavaCommonSourceJar(rand.nextBoolean()) .incompatibleNewActionsApi(rand.nextBoolean()) diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkIntegrationTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkIntegrationTest.java index 6078e349ee0cc0..3b342a7f4dfc8c 100644 --- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkIntegrationTest.java +++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkIntegrationTest.java @@ -2749,6 +2749,29 @@ public void testLicenseType() throws Exception { assertContainsEvent(""); } + @Test + public void testDisallowStructProviderSyntax() throws Exception { + setSkylarkSemanticsOptions("--incompatible_disallow_struct_provider_syntax=true"); + scratch.file( + "test/skylark/extension.bzl", + "def custom_rule_impl(ctx):", + " return struct()", + "", + "custom_rule = rule(implementation = custom_rule_impl)"); + scratch.file( + "test/skylark/BUILD", + "load('//test/skylark:extension.bzl', 'custom_rule')", + "", + "custom_rule(name = 'cr')"); + + reporter.removeHandler(failFastHandler); + getConfiguredTarget("//test/skylark:cr"); + assertContainsEvent( + "Returning a struct from a rule implementation function is deprecated and will be " + + "removed soon. It may be temporarily re-enabled by setting " + + "--incompatible_disallow_struct_provider_syntax=false"); + } + /** * Skylark integration test that forces inlining. */