Skip to content

Commit

Permalink
[6.2.0]Add support for alias targets to cquery's providers (bazelbu…
Browse files Browse the repository at this point in the history
…ild#17786)

* 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`.

Fixes bazelbuild#17749

Closes bazelbuild#17753.

PiperOrigin-RevId: 516707744
Change-Id: I840588d605e3a64b968a019cf4bf43b56d18f4f5

* Remove extra code

---------

Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
Co-authored-by: keertk <keerthanakumar@google.com>
  • Loading branch information
3 people authored Mar 20, 2023
1 parent 3a7236b commit a87b8e0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -95,4 +96,13 @@ default Label getOriginalLabel() {
default ImmutableMap<Label, ConfigMatchingProvider> getConfigConditions() {
return ImmutableMap.of();
}

/**
* This is only intended to be called from the query dialects of Starlark.
*
* @return a map of provider names to their values
*/
default Dict<String, Object> getProvidersDict() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import com.google.devtools.build.lib.skyframe.BuildConfigurationKey;
import java.util.function.Consumer;
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.Starlark;
Expand Down Expand Up @@ -255,12 +254,4 @@ public String getRuleClassString() {
public void repr(Printer printer) {
printer.append("<unknown target " + getLabel() + ">");
}

/**
* 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<String, Object> getProvidersDict() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> ret = ((AbstractConfiguredTarget) target).getProvidersDict();
Dict<String, Object> ret = target.getProvidersDict();
if (ret == null) {
return Starlark.NONE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -196,6 +197,11 @@ public Label getOriginalLabel() {
return label;
}

@Override
public Dict<String, Object> getProvidersDict() {
return actual.getProvidersDict();
}

@Override
public void repr(Printer printer) {
printer.append("<alias target " + label + " of " + actual.getLabel() + ">");
Expand Down
31 changes: 31 additions & 0 deletions src/test/shell/integration/configured_query_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOF
load(":my_rule.bzl", "my_rule")
my_rule(name="myrule")
alias(name="myalias", actual="myrule")
EOF
cat > $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 <<EOF
def format(target):
p = providers(target)
return p["//$pkg:my_rule.bzl%MyRuleInfo"].label
EOF
bazel cquery "//$pkg:myalias" --output=starlark --starlark:file="$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

Expand Down

0 comments on commit a87b8e0

Please sign in to comment.