Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for alias targets to cquery's providers #17753

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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<String, Object> getProvidersDict() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,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