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 1bedef5c5936a6..27d4db53cc8d3a 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 @@ -535,13 +535,21 @@ private static Spawn createXmlGeneratingSpawn( envBuilder.put("TEST_SHARD_INDEX", "0"); envBuilder.put("TEST_TOTAL_SHARDS", "0"); } + ImmutableMap.Builder executionInfo = ImmutableMap.builder(); + executionInfo.putAll(action.getExecutionInfo()); + if (result.exitCode() != 0) { + // If the test is failed, the spawn shouldn't use remote cache as the test.xml file is renamed immediately after + // the spawn execution if there is another test attempt which will cause upload failures for remote async upload + // because it cannot read the file at original position. + executionInfo.put(ExecutionRequirements.NO_REMOTE_CACHE, ""); + } return new SimpleSpawn( action, args, 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()), + executionInfo.build(), null, ImmutableMap.of(), /*inputs=*/ NestedSetBuilder.create( diff --git a/src/test/shell/bazel/remote/remote_execution_test.sh b/src/test/shell/bazel/remote/remote_execution_test.sh index 3c5735e39e3f0a..3a3218cb79f66f 100755 --- a/src/test/shell/bazel/remote/remote_execution_test.sh +++ b/src/test/shell/bazel/remote/remote_execution_test.sh @@ -3171,4 +3171,37 @@ EOF expect_log "-r-xr-xr-x" } +function test_async_upload_works_for_flaky_tests() { + mkdir -p a + cat > a/BUILD < \$@", +) +EOF + cat > a/test.sh <& $TEST_log || fail "Failed to build" + expect_log "WARNING: Writing to Remote Cache:" + + bazel test \ + --remote_cache=grpc://localhost:${worker_port} \ + --experimental_remote_cache_async \ + --flaky_test_attempts=2 \ + //a:test >& $TEST_log && fail "expected failure" || true + expect_not_log "WARNING: Writing to Remote Cache:" +} + run_suite "Remote execution and remote cache tests"