Skip to content

Commit 3a5b360

Browse files
coeuvrecopybara-github
authored andcommitted
Remote: Merge target-level exec_properties with --remote_default_exec_properties
Per-target `exec_properties` was introduced by 0dc53a2 but it didn't take into account of `--remote_default_exec_properties` which provides default exec properties for the execution platform if it doesn't already set with `exec_properties`. So the per-target `exec_properties` overrides `--remote_default_exec_properties` instead of merging with them which is wrong. Fixes bazelbuild#10252. Closes bazelbuild#14193. PiperOrigin-RevId: 406337649
1 parent eacb32f commit 3a5b360

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.google.protobuf.TextFormat;
3030
import com.google.protobuf.TextFormat.ParseException;
3131
import java.util.Comparator;
32+
import java.util.HashMap;
3233
import java.util.List;
3334
import java.util.Map;
3435
import java.util.SortedMap;
@@ -78,7 +79,19 @@ public static Platform getPlatformProto(Spawn spawn, @Nullable RemoteOptions rem
7879
Platform.Builder platformBuilder = Platform.newBuilder();
7980

8081
if (!spawn.getCombinedExecProperties().isEmpty()) {
81-
for (Map.Entry<String, String> entry : spawn.getCombinedExecProperties().entrySet()) {
82+
Map<String, String> combinedExecProperties;
83+
// Apply default exec properties if the execution platform does not already set
84+
// exec_properties
85+
if (spawn.getExecutionPlatform() == null
86+
|| spawn.getExecutionPlatform().execProperties().isEmpty()) {
87+
combinedExecProperties = new HashMap<>();
88+
combinedExecProperties.putAll(defaultExecProperties);
89+
combinedExecProperties.putAll(spawn.getCombinedExecProperties());
90+
} else {
91+
combinedExecProperties = spawn.getCombinedExecProperties();
92+
}
93+
94+
for (Map.Entry<String, String> entry : combinedExecProperties.entrySet()) {
8295
platformBuilder.addPropertiesBuilder().setName(entry.getKey()).setValue(entry.getValue());
8396
}
8497
} else if (spawn.getExecutionPlatform() != null

src/test/java/com/google/devtools/build/lib/analysis/platform/PlatformUtilsTest.java

+30-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
import static com.google.common.truth.Truth.assertThat;
1818

1919
import build.bazel.remote.execution.v2.Platform;
20+
import com.google.common.collect.ImmutableList;
2021
import com.google.common.collect.ImmutableMap;
2122
import com.google.devtools.build.lib.actions.ExecutionRequirements;
2223
import com.google.devtools.build.lib.actions.Spawn;
2324
import com.google.devtools.build.lib.exec.util.SpawnBuilder;
2425
import com.google.devtools.build.lib.remote.options.RemoteOptions;
2526
import com.google.devtools.common.options.Options;
27+
import java.util.AbstractMap;
2628
import org.junit.Test;
2729
import org.junit.runner.RunWith;
2830
import org.junit.runners.JUnit4;
@@ -45,8 +47,9 @@ private static String platformOptionsString() {
4547

4648
private static RemoteOptions remoteOptions() {
4749
RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
48-
remoteOptions.remoteDefaultPlatformProperties = platformOptionsString();
49-
50+
remoteOptions.remoteDefaultExecProperties =
51+
ImmutableList.of(
52+
new AbstractMap.SimpleEntry<>("b", "2"), new AbstractMap.SimpleEntry<>("a", "1"));
5053
return remoteOptions;
5154
}
5255

@@ -93,7 +96,7 @@ public void testParsePlatformSortsProperties_execProperties() throws Exception {
9396
.addProperties(Platform.Property.newBuilder().setName("zz").setValue("66"))
9497
.build();
9598
// execProperties are sorted by key
96-
assertThat(PlatformUtils.getPlatformProto(s, remoteOptions())).isEqualTo(expected);
99+
assertThat(PlatformUtils.getPlatformProto(s, null)).isEqualTo(expected);
97100
}
98101

99102
@Test
@@ -115,4 +118,28 @@ public void testGetPlatformProto_differentiateWorkspace() throws Exception {
115118
// execProperties are sorted by key
116119
assertThat(PlatformUtils.getPlatformProto(s, remoteOptions())).isEqualTo(expected);
117120
}
121+
122+
@Test
123+
public void getPlatformProto_mergeTargetExecPropertiesWithPlatform() throws Exception {
124+
Spawn spawn = new SpawnBuilder("dummy").withExecProperties(ImmutableMap.of("c", "3")).build();
125+
Platform expected =
126+
Platform.newBuilder()
127+
.addProperties(Platform.Property.newBuilder().setName("a").setValue("1"))
128+
.addProperties(Platform.Property.newBuilder().setName("b").setValue("2"))
129+
.addProperties(Platform.Property.newBuilder().setName("c").setValue("3"))
130+
.build();
131+
assertThat(PlatformUtils.getPlatformProto(spawn, remoteOptions())).isEqualTo(expected);
132+
}
133+
134+
@Test
135+
public void getPlatformProto_targetExecPropertiesConflictWithPlatform_override()
136+
throws Exception {
137+
Spawn spawn = new SpawnBuilder("dummy").withExecProperties(ImmutableMap.of("b", "3")).build();
138+
Platform expected =
139+
Platform.newBuilder()
140+
.addProperties(Platform.Property.newBuilder().setName("a").setValue("1"))
141+
.addProperties(Platform.Property.newBuilder().setName("b").setValue("3"))
142+
.build();
143+
assertThat(PlatformUtils.getPlatformProto(spawn, remoteOptions())).isEqualTo(expected);
144+
}
118145
}

0 commit comments

Comments
 (0)