Modify AliasConfiguredTargets to store related config conditions.
This allows for properly resolved selects for the proto output format of cquery (b/112853400). AliasConfiguredTargets don't extend RuleConfiguredTargets, just stores an 'actual' configured target value. Since they don't extend RuleConfiguredTarget, they don't have access to the config conditions for the alias target. They *can* currently access the config condition for the 'actual' target, but those aren't the correct conditions for the alias target.
ex of pattern that this CL addresses:
//test:BUILD
alias(
name = 'fake_target',
actual = select({
':config1': ':target1',
':config2': ':target2',
})
)
Currently, running "blaze cquery 'deps(//test:fake-target)' --output=proto --correct_flag_to_trigger_config1" returns an error since the output formatter attempts to use the config conditions of target1 (the resolved value of the 'actual' attribute) to evaluate the select in the 'actual' attribute of the alias target.
PiperOrigin-RevId: 210002631
diff --git a/src/main/java/com/google/devtools/build/lib/query2/ProtoOutputFormatterCallback.java b/src/main/java/com/google/devtools/build/lib/query2/ProtoOutputFormatterCallback.java
index 1741eb9..011d483 100644
--- a/src/main/java/com/google/devtools/build/lib/query2/ProtoOutputFormatterCallback.java
+++ b/src/main/java/com/google/devtools/build/lib/query2/ProtoOutputFormatterCallback.java
@@ -118,12 +118,18 @@
private class ConfiguredProtoOutputFormatter extends ProtoOutputFormatter {
@Override
protected void addAttributes(Build.Rule.Builder rulePb, Rule rule) throws InterruptedException {
- ConfiguredTarget ctForConfigConditions = currentTarget;
- while (ctForConfigConditions instanceof AliasConfiguredTarget) {
- ctForConfigConditions = ((AliasConfiguredTarget) ctForConfigConditions).getActual();
+ // We know <code>currentTarget</code> will be one of these two types of configured targets
+ // because this method is only triggered in ProtoOutputFormatter.toTargetProtoBuffer when
+ // the target in currentTarget is an instanceof Rule.
+ ImmutableMap<Label, ConfigMatchingProvider> configConditions;
+ if (currentTarget instanceof AliasConfiguredTarget) {
+ configConditions = ((AliasConfiguredTarget) currentTarget).getConfigConditions();
+ } else if (currentTarget instanceof RuleConfiguredTarget) {
+ configConditions = ((RuleConfiguredTarget) currentTarget).getConfigConditions();
+ } else {
+ // Other subclasses of ConfiguredTarget don't have attribute information.
+ return;
}
- ImmutableMap<Label, ConfigMatchingProvider> configConditions =
- ((RuleConfiguredTarget) ctForConfigConditions).getConfigConditions();
ConfiguredAttributeMapper attributeMapper =
ConfiguredAttributeMapper.of(rule, configConditions);
Map<Attribute, Build.Attribute> serializedAttributes = Maps.newHashMap();
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 a21a5b9..a8a0e10 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
@@ -21,6 +21,7 @@
import com.google.devtools.build.lib.analysis.FileProvider;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.TransitiveInfoProvider;
+import com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.events.Location;
@@ -49,6 +50,7 @@
private final ConfiguredTarget actual;
private final ImmutableMap<Class<? extends TransitiveInfoProvider>, TransitiveInfoProvider>
overrides;
+ private final ImmutableMap<Label, ConfigMatchingProvider> configConditions;
public AliasConfiguredTarget(
RuleContext ruleContext,
@@ -58,7 +60,8 @@
ruleContext.getLabel(),
Preconditions.checkNotNull(ruleContext.getConfigurationKey()),
Preconditions.checkNotNull(actual),
- Preconditions.checkNotNull(overrides));
+ Preconditions.checkNotNull(overrides),
+ ruleContext.getConfigConditions());
}
@AutoCodec.Instantiator
@@ -67,11 +70,17 @@
Label label,
BuildConfigurationValue.Key configurationKey,
ConfiguredTarget actual,
- ImmutableMap<Class<? extends TransitiveInfoProvider>, TransitiveInfoProvider> overrides) {
+ ImmutableMap<Class<? extends TransitiveInfoProvider>, TransitiveInfoProvider> overrides,
+ ImmutableMap<Label, ConfigMatchingProvider> configConditions) {
this.label = label;
this.configurationKey = configurationKey;
this.actual = actual;
this.overrides = overrides;
+ this.configConditions = configConditions;
+ }
+
+ public ImmutableMap<Label, ConfigMatchingProvider> getConfigConditions() {
+ return configConditions;
}
@Override