Propagate test envs to xml generation action
Previously, we hardcode the envs of the xml generation action, which
caused problem for process-wrapper because it's dynamically linked to
some system libraries and the required PATH or LD_LIBRARY_PATH are not
set.
This change propagate the envs we set for the actual test action to the
xml file generation action to make sure the env vars are correctly set and
can also be controlled by --action_env and --test_env.
Fixes https://github.com/bazelbuild/bazel/issues/4137
Fixes https://github.com/bazelbuild/bazel/issues/12579
Closes #12659.
PiperOrigin-RevId: 347596753
diff --git a/src/main/java/com/google/devtools/build/lib/exec/StandaloneTestStrategy.java b/src/main/java/com/google/devtools/build/lib/exec/StandaloneTestStrategy.java
index fd1214d..cdc4b8d 100644
--- a/src/main/java/com/google/devtools/build/lib/exec/StandaloneTestStrategy.java
+++ b/src/main/java/com/google/devtools/build/lib/exec/StandaloneTestStrategy.java
@@ -391,7 +391,8 @@
* A spawn to generate a test.xml file from the test log. This is only used if the test does not
* generate a test.xml file itself.
*/
- private static Spawn createXmlGeneratingSpawn(TestRunnerAction action, SpawnResult result) {
+ private static Spawn createXmlGeneratingSpawn(
+ TestRunnerAction action, ImmutableMap<String, String> testEnv, SpawnResult result) {
ImmutableList<String> args =
ImmutableList.of(
action.getTestXmlGeneratorScript().getExecPath().getCallablePathString(),
@@ -399,18 +400,21 @@
action.getXmlOutputPath().getPathString(),
Long.toString(result.getWallTime().orElse(Duration.ZERO).getSeconds()),
Integer.toString(result.exitCode()));
-
- String testBinaryName =
- action.getExecutionSettings().getExecutable().getRootRelativePath().getCallablePathString();
+ ImmutableMap.Builder<String, String> envBuilder = ImmutableMap.builder();
+ // "PATH" and "TEST_BINARY" are also required, they should always be set in testEnv.
+ Preconditions.checkArgument(testEnv.containsKey("PATH"));
+ Preconditions.checkArgument(testEnv.containsKey("TEST_BINARY"));
+ envBuilder.putAll(testEnv).put("TEST_NAME", action.getTestName());
+ // testEnv only contains TEST_SHARD_INDEX and TEST_TOTAL_SHARDS if the test action is sharded,
+ // we need to set the default value when the action isn't sharded.
+ if (!action.isSharded()) {
+ envBuilder.put("TEST_SHARD_INDEX", "0");
+ envBuilder.put("TEST_TOTAL_SHARDS", "0");
+ }
return new SimpleSpawn(
action,
args,
- ImmutableMap.of(
- "PATH", "/usr/bin:/bin",
- "TEST_SHARD_INDEX", Integer.toString(action.getShardNum()),
- "TEST_TOTAL_SHARDS", Integer.toString(action.getExecutionSettings().getTotalShards()),
- "TEST_NAME", action.getTestName(),
- "TEST_BINARY", testBinaryName),
+ envBuilder.build(),
// Pass the execution info of the action which is identical to the supported tags set on the
// test target. In particular, this does not set the test timeout on the spawn.
ImmutableMap.copyOf(action.getExecutionInfo()),
@@ -766,7 +770,8 @@
if (executionOptions.splitXmlGeneration
&& fileOutErr.getOutputPath().exists()
&& !xmlOutputPath.exists()) {
- Spawn xmlGeneratingSpawn = createXmlGeneratingSpawn(testAction, spawnResults.get(0));
+ Spawn xmlGeneratingSpawn =
+ createXmlGeneratingSpawn(testAction, spawn.getEnvironment(), spawnResults.get(0));
SpawnStrategyResolver spawnStrategyResolver =
actionExecutionContext.getContext(SpawnStrategyResolver.class);
// We treat all failures to generate the test.xml here as catastrophic, and won't rerun