Skip to content

Commit 24d0864

Browse files
katrecopybara-github
authored andcommitted
PlatformProviderUtils should ignore targets that don't have the needed
provider. Also update PlatformProviderUtils to work directly with Lists. This prevents a few types of crash that can occur from having null values in a List. Fixes bazelbuild#12879. Closes bazelbuild#12931. PiperOrigin-RevId: 354595201
1 parent 3e6e975 commit 24d0864

File tree

4 files changed

+40
-28
lines changed

4 files changed

+40
-28
lines changed

src/main/java/com/google/devtools/build/lib/analysis/constraints/RuleContextConstraintSemantics.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package com.google.devtools.build.lib.analysis.constraints;
1616

1717
import static com.google.common.collect.ImmutableList.toImmutableList;
18-
import static com.google.common.collect.Streams.stream;
1918

2019
import com.google.auto.value.AutoValue;
2120
import com.google.common.base.Joiner;
@@ -912,9 +911,9 @@ public static ConfiguredTarget incompatibleConfiguredTarget(
912911
if (ruleContext.getRule().getRuleClassObject().useToolchainResolution()
913912
&& ruleContext.attributes().has("target_compatible_with")) {
914913
ImmutableList<ConstraintValueInfo> invalidConstraintValues =
915-
stream(
916-
PlatformProviderUtils.constraintValues(
917-
ruleContext.getPrerequisites("target_compatible_with")))
914+
PlatformProviderUtils.constraintValues(
915+
ruleContext.getPrerequisites("target_compatible_with"))
916+
.stream()
918917
.filter(cv -> !ruleContext.targetPlatformHasConstraint(cv))
919918
.collect(toImmutableList());
920919

src/main/java/com/google/devtools/build/lib/analysis/platform/PlatformProviderUtils.java

+22-9
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414

1515
package com.google.devtools.build.lib.analysis.platform;
1616

17-
import com.google.common.collect.Iterables;
17+
import static com.google.common.base.Predicates.notNull;
18+
import static com.google.common.collect.ImmutableList.toImmutableList;
19+
20+
import com.google.common.collect.ImmutableList;
1821
import com.google.devtools.build.lib.analysis.ProviderCollection;
22+
import java.util.List;
1923
import javax.annotation.Nullable;
2024

2125
/** Utility methods to help locate platform-related providers. */
@@ -31,8 +35,11 @@ public static PlatformInfo platform(@Nullable ProviderCollection target) {
3135
}
3236

3337
/** Retrieves and casts {@link PlatformInfo} providers from the given targets. */
34-
public static Iterable<PlatformInfo> platforms(Iterable<? extends ProviderCollection> targets) {
35-
return Iterables.transform(targets, PlatformProviderUtils::platform);
38+
public static ImmutableList<PlatformInfo> platforms(List<? extends ProviderCollection> targets) {
39+
return targets.stream()
40+
.map(PlatformProviderUtils::platform)
41+
.filter(notNull())
42+
.collect(toImmutableList());
3643
}
3744

3845
/** Retrieves and casts the {@link ConstraintSettingInfo} provider from the given target. */
@@ -45,9 +52,12 @@ public static ConstraintSettingInfo constraintSetting(@Nullable ProviderCollecti
4552
}
4653

4754
/** Retrieves and casts {@link ConstraintSettingInfo} providers from the given targets. */
48-
public static Iterable<ConstraintSettingInfo> constraintSettings(
49-
Iterable<? extends ProviderCollection> targets) {
50-
return Iterables.transform(targets, PlatformProviderUtils::constraintSetting);
55+
public static ImmutableList<ConstraintSettingInfo> constraintSettings(
56+
List<? extends ProviderCollection> targets) {
57+
return targets.stream()
58+
.map(PlatformProviderUtils::constraintSetting)
59+
.filter(notNull())
60+
.collect(toImmutableList());
5161
}
5262

5363
/** Retrieves and casts the {@link ConstraintValueInfo} provider from the given target. */
@@ -65,9 +75,12 @@ public static boolean hasConstraintValue(ProviderCollection target) {
6575
}
6676

6777
/** Retrieves and casts {@link ConstraintValueInfo} providers from the given targets. */
68-
public static Iterable<ConstraintValueInfo> constraintValues(
69-
Iterable<? extends ProviderCollection> targets) {
70-
return Iterables.transform(targets, PlatformProviderUtils::constraintValue);
78+
public static ImmutableList<ConstraintValueInfo> constraintValues(
79+
List<? extends ProviderCollection> targets) {
80+
return targets.stream()
81+
.map(PlatformProviderUtils::constraintValue)
82+
.filter(notNull())
83+
.collect(toImmutableList());
7184
}
7285

7386
/**

src/main/java/com/google/devtools/build/lib/rules/platform/Platform.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
package com.google.devtools.build.lib.rules.platform;
1616

17+
import com.google.common.collect.ImmutableList;
1718
import com.google.common.collect.ImmutableMap;
1819
import com.google.common.collect.Iterables;
19-
import com.google.common.collect.Lists;
2020
import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException;
2121
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
2222
import com.google.devtools.build.lib.analysis.FileProvider;
@@ -35,7 +35,6 @@
3535
import com.google.devtools.build.lib.util.CPU;
3636
import com.google.devtools.build.lib.util.OS;
3737
import com.google.devtools.build.lib.util.Pair;
38-
import java.util.List;
3938
import java.util.Map;
4039

4140
/** Defines a platform for execution contexts. */
@@ -49,10 +48,9 @@ public ConfiguredTarget create(RuleContext ruleContext)
4948

5049
PlatformInfo.Builder platformBuilder = PlatformInfo.builder().setLabel(ruleContext.getLabel());
5150

52-
List<PlatformInfo> parentPlatforms =
53-
Lists.newArrayList(
54-
PlatformProviderUtils.platforms(
55-
ruleContext.getPrerequisites(PlatformRule.PARENTS_PLATFORM_ATTR)));
51+
ImmutableList<PlatformInfo> parentPlatforms =
52+
PlatformProviderUtils.platforms(
53+
ruleContext.getPrerequisites(PlatformRule.PARENTS_PLATFORM_ATTR));
5654

5755
if (parentPlatforms.size() > 1) {
5856
throw ruleContext.throwWithAttributeError(
@@ -130,7 +128,7 @@ private void autodetectConstraints(
130128

131129
// Add the CPU.
132130
CPU cpu = cpuValues.getFirst();
133-
Iterable<ConstraintValueInfo> cpuConstraintValues =
131+
ImmutableList<ConstraintValueInfo> cpuConstraintValues =
134132
PlatformProviderUtils.constraintValues(
135133
ruleContext.getPrerequisites(PlatformRule.CPU_CONSTRAINTS_ATTR));
136134
for (ConstraintValueInfo constraint : cpuConstraintValues) {
@@ -142,7 +140,7 @@ private void autodetectConstraints(
142140

143141
// Add the OS.
144142
OS os = cpuValues.getSecond();
145-
Iterable<ConstraintValueInfo> osConstraintValues =
143+
ImmutableList<ConstraintValueInfo> osConstraintValues =
146144
PlatformProviderUtils.constraintValues(
147145
ruleContext.getPrerequisites(PlatformRule.OS_CONSTRAINTS_ATTR));
148146
for (ConstraintValueInfo constraint : osConstraintValues) {

src/main/java/com/google/devtools/build/lib/rules/platform/Toolchain.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
package com.google.devtools.build.lib.rules.platform;
1616

17-
import com.google.common.collect.Iterables;
17+
import static com.google.common.collect.ImmutableList.toImmutableList;
18+
19+
import com.google.common.collect.ImmutableList;
1820
import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException;
1921
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
2022
import com.google.devtools.build.lib.analysis.FileProvider;
@@ -41,16 +43,16 @@ public ConfiguredTarget create(RuleContext ruleContext)
4143
ToolchainTypeInfo toolchainType =
4244
PlatformProviderUtils.toolchainType(
4345
ruleContext.getPrerequisite(ToolchainRule.TOOLCHAIN_TYPE_ATTR));
44-
Iterable<ConstraintValueInfo> execConstraints =
46+
ImmutableList<ConstraintValueInfo> execConstraints =
4547
PlatformProviderUtils.constraintValues(
4648
ruleContext.getPrerequisites(ToolchainRule.EXEC_COMPATIBLE_WITH_ATTR));
47-
Iterable<ConstraintValueInfo> targetConstraints =
49+
ImmutableList<ConstraintValueInfo> targetConstraints =
4850
PlatformProviderUtils.constraintValues(
4951
ruleContext.getPrerequisites(ToolchainRule.TARGET_COMPATIBLE_WITH_ATTR));
50-
Iterable<ConfigMatchingProvider> targetSettings =
51-
Iterables.transform(
52-
ruleContext.getPrerequisites(ToolchainRule.TARGET_SETTING_ATTR),
53-
target -> target.getProvider(ConfigMatchingProvider.class));
52+
ImmutableList<ConfigMatchingProvider> targetSettings =
53+
ruleContext.getPrerequisites(ToolchainRule.TARGET_SETTING_ATTR).stream()
54+
.map(target -> target.getProvider(ConfigMatchingProvider.class))
55+
.collect(toImmutableList());
5456
Label toolchainLabel =
5557
ruleContext.attributes().get(ToolchainRule.TOOLCHAIN_ATTR, BuildType.NODEP_LABEL);
5658

0 commit comments

Comments
 (0)