From 32229c27192b05bd18e20d411ab32521fe451b39 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Mon, 13 Mar 2023 15:09:15 +0100 Subject: [PATCH] Add support for alias targets to cquery's `providers` When applied to an `alias` target, the `providers` function of `cquery`'s `--output=starlark` mode now returns the providers of the aliased target rather than `None`. This is achieved by moving `getProvidersDict` from `AbstractConfiguredTarget` up to `ConfiguredTarget`. --- .../build/lib/analysis/ConfiguredTarget.java | 10 ++++++ .../AbstractConfiguredTarget.java | 8 ----- .../StarlarkOutputFormatterCallback.java | 6 +--- .../lib/rules/AliasConfiguredTarget.java | 6 ++++ .../integration/configured_query_test.sh | 31 +++++++++++++++++++ 5 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredTarget.java b/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredTarget.java index 8fded0ca226299..a53fea14973223 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredTarget.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredTarget.java @@ -20,6 +20,7 @@ import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.skyframe.BuildConfigurationKey; import javax.annotation.Nullable; +import net.starlark.java.eval.Dict; import net.starlark.java.eval.Structure; /** @@ -110,4 +111,13 @@ default boolean isRuleConfiguredTarget() { default ConfiguredTarget unwrapIfMerged() { return this; } + + /** + * This is only intended to be called from the query dialects of Starlark. + * + * @return a map of provider names to their values + */ + default Dict getProvidersDict() { + return null; + } } diff --git a/src/main/java/com/google/devtools/build/lib/analysis/configuredtargets/AbstractConfiguredTarget.java b/src/main/java/com/google/devtools/build/lib/analysis/configuredtargets/AbstractConfiguredTarget.java index e5156c1cf3eee5..187d9758051527 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/configuredtargets/AbstractConfiguredTarget.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/configuredtargets/AbstractConfiguredTarget.java @@ -255,12 +255,4 @@ public String getRuleClassString() { public void repr(Printer printer) { printer.append(""); } - - /** - * Returns a map of provider names to their values. This is only intended to be called from the - * query dialects of Starlark. Implement in subclasses which can have providers. - */ - public Dict getProvidersDict() { - return null; - } } diff --git a/src/main/java/com/google/devtools/build/lib/query2/cquery/StarlarkOutputFormatterCallback.java b/src/main/java/com/google/devtools/build/lib/query2/cquery/StarlarkOutputFormatterCallback.java index 267ec00df96c8b..d107529352ec4a 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/cquery/StarlarkOutputFormatterCallback.java +++ b/src/main/java/com/google/devtools/build/lib/query2/cquery/StarlarkOutputFormatterCallback.java @@ -21,7 +21,6 @@ import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue; import com.google.devtools.build.lib.analysis.config.BuildOptions; import com.google.devtools.build.lib.analysis.config.FragmentOptions; -import com.google.devtools.build.lib.analysis.configuredtargets.AbstractConfiguredTarget; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.events.ExtendedEventHandler; @@ -121,10 +120,7 @@ public Object buildOptions(ConfiguredTarget target) { @Param(name = "target"), }) public Object providers(ConfiguredTarget target) { - if (!(target instanceof AbstractConfiguredTarget)) { - return Starlark.NONE; - } - Dict ret = ((AbstractConfiguredTarget) target).getProvidersDict(); + Dict ret = target.getProvidersDict(); if (ret == null) { return Starlark.NONE; } diff --git a/src/main/java/com/google/devtools/build/lib/rules/AliasConfiguredTarget.java b/src/main/java/com/google/devtools/build/lib/rules/AliasConfiguredTarget.java index 27148faf0b927a..14711711471c24 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/AliasConfiguredTarget.java +++ b/src/main/java/com/google/devtools/build/lib/rules/AliasConfiguredTarget.java @@ -38,6 +38,7 @@ import com.google.devtools.build.lib.packages.Provider; import com.google.devtools.build.lib.skyframe.BuildConfigurationKey; import javax.annotation.Nullable; +import net.starlark.java.eval.Dict; import net.starlark.java.eval.EvalException; import net.starlark.java.eval.Printer; import net.starlark.java.eval.StarlarkSemantics; @@ -196,6 +197,11 @@ public Label getOriginalLabel() { return label; } + @Override + public Dict getProvidersDict() { + return actual.getProvidersDict(); + } + @Override public void repr(Printer printer) { printer.append(""); diff --git a/src/test/shell/integration/configured_query_test.sh b/src/test/shell/integration/configured_query_test.sh index 5e293407ef0b68..8cf12718bb32ef 100755 --- a/src/test/shell/integration/configured_query_test.sh +++ b/src/test/shell/integration/configured_query_test.sh @@ -1268,6 +1268,37 @@ EOF assert_contains "some_value" output } +function test_starlark_output_providers_starlark_provider_for_alias() { + local -r pkg=$FUNCNAME + mkdir -p $pkg + cat > $pkg/BUILD < $pkg/my_rule.bzl <<'EOF' +# A no-op rule that manifests a provider +MyRuleInfo = provider(fields={"label": "a_rule_label"}) + +def _my_rule_impl(ctx): + return [MyRuleInfo(label="some_value")] + +my_rule = rule( + implementation = _my_rule_impl, + attrs = {}, +) +EOF + cat > $pkg/outfunc.bzl <output \ + 2>"$TEST_log" || fail "Expected success" + + assert_contains "some_value" output +} + function test_bazelignore_error_cquery_nocrash() { local -r pkg=$FUNCNAME