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

[6.2.0]Add support for alias targets to cquery's providers #17786

Merged
merged 3 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -95,4 +96,26 @@ default Label getOriginalLabel() {
default ImmutableMap<Label, ConfigMatchingProvider> getConfigConditions() {
return ImmutableMap.of();
}

default boolean isRuleConfiguredTarget() {
return false;
}

/**
* The base configured target if it has been merged with aspects otherwise the current value.
*
* <p>Unwrapping is recursive if there are multiple layers.
*/
default ConfiguredTarget unwrapIfMerged() {
return this;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original change at https://github.com/bazelbuild/bazel/pull/17753/files doesn't impact lines 99-111. Does that matter?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that was the change I made. I removed this extra code that shouldn't have been included:) Looks like the file here is outdated.


/**
* 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