Skip to content

Commit 22f518c

Browse files
jmmvcopybara-github
authored andcommitted
Add the remote_require_cached flag
When set to true, this flag causes Bazel to abort the build whenever it encounters an action that is not cached. This is very useful when trying to troubleshoot action caching issues across machines because it allows running a build on one and having it fail on another as soon as there is a problem without tainting what already exists in the cache. My workflow is to essentially do: 1. Machine 1: bazel clean 2. Machine 1: bazel build ... 3. Machine 2: bazel clean 4. Machine 2: bazel build --remote_require_cached ... which makes step 4 fail on the first action that wasn't cached as expected. Then I can address that problem and re-run step 4 to encounter the next issue. Closes #18942. PiperOrigin-RevId: 549242966 Change-Id: Ib46a2eb8cce6f4444968882e99c21284fc6bc4f8
1 parent 1d980d9 commit 22f518c

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/main/java/com/google/devtools/build/lib/remote/RemoteSpawnRunner.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,22 @@ public SpawnResult exec(Spawn spawn, SpawnExecutionContext context)
236236
return execLocallyAndUploadOrFail(action, spawn, context, uploadLocalResults, e);
237237
}
238238

239+
if (remoteOptions.remoteRequireCached) {
240+
return new SpawnResult.Builder()
241+
.setStatus(SpawnResult.Status.EXECUTION_DENIED)
242+
.setExitCode(1)
243+
.setFailureMessage(
244+
"Action must be cached due to --experimental_remote_require_cached but it is not")
245+
.setFailureDetail(
246+
FailureDetail.newBuilder()
247+
.setSpawn(
248+
FailureDetails.Spawn.newBuilder()
249+
.setCode(FailureDetails.Spawn.Code.EXECUTION_DENIED))
250+
.build())
251+
.setRunnerName("remote")
252+
.build();
253+
}
254+
239255
AtomicBoolean useCachedResult = new AtomicBoolean(acceptCachedResult);
240256
AtomicBoolean forceUploadInput = new AtomicBoolean(false);
241257
try {

src/main/java/com/google/devtools/build/lib/remote/options/RemoteOptions.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,18 @@ public final class RemoteOptions extends CommonRemoteOptions {
229229
help = "Whether to accept remotely cached action results.")
230230
public boolean remoteAcceptCached;
231231

232+
@Option(
233+
name = "experimental_remote_require_cached",
234+
defaultValue = "false",
235+
documentationCategory = OptionDocumentationCategory.REMOTE,
236+
effectTags = {OptionEffectTag.UNKNOWN},
237+
help =
238+
"If set to true, enforce that all actions that can run remotely are cached, or else "
239+
+ "fail the build. This is useful to troubleshoot non-determinism issues as it "
240+
+ "allows checking whether actions that should be cached are actually cached "
241+
+ "without spuriously injecting new results into the cache.")
242+
public boolean remoteRequireCached;
243+
232244
@Option(
233245
name = "remote_local_fallback",
234246
defaultValue = "false",

src/test/shell/bazel/remote/remote_execution_test.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,36 @@ EOF
12631263
expect_not_log "1 local"
12641264
}
12651265

1266+
function test_require_cached() {
1267+
mkdir -p a
1268+
cat > a/BUILD <<'EOF'
1269+
genrule(
1270+
name = "foo",
1271+
srcs = ["foo.in"],
1272+
outs = ["foo.out"],
1273+
cmd = "cp \"$<\" \"$@\"",
1274+
)
1275+
EOF
1276+
1277+
echo "input 1" >a/foo.in
1278+
bazel build \
1279+
--remote_executor=grpc://localhost:${worker_port} \
1280+
//a:foo >& $TEST_log || fail "Failed to build //a:foo"
1281+
1282+
expect_log "1 remote"
1283+
1284+
echo "input 2" >a/foo.in
1285+
if bazel build \
1286+
--remote_executor=grpc://localhost:${worker_port} \
1287+
--experimental_remote_require_cached \
1288+
//a:foo >& $TEST_log; then
1289+
fail "Build of //a:foo succeeded but it should have failed"
1290+
fi
1291+
1292+
expect_log "Action must be cached due to --experimental_remote_require_cached but it is not"
1293+
expect_not_log "remote cache hit"
1294+
}
1295+
12661296
function test_nobuild_runfile_links() {
12671297
mkdir data && echo "hello" > data/hello && echo "world" > data/world
12681298
cat > WORKSPACE <<EOF

0 commit comments

Comments
 (0)