From 0e4f0a0a3bf85500745725573be842c657f40c27 Mon Sep 17 00:00:00 2001 From: Tiago Quelhas Date: Thu, 11 Jan 2024 16:32:05 +0100 Subject: [PATCH] Force output checking for incremental run commands without the bytes. When building without the bytes, outputs are only materialized when either (1) an action prefetches them, or (2) they've been explicitly requested. When both --remote_download_minimal and --noexperimental_check_output_files are set, this presents a problem for a build command followed by a run command for the same target: the build command won't download the outputs and the run command won't check for their presence, proceeding without them. A non-incremental run command works, because the outputs are considered top-level even in minimal mode. This is only a problem for a run command because it exists outside of action execution, and doesn't get a chance to prefetch inputs; it would have been better to implement the run command by injecting a specially crafted action into the build graph, but that will have to wait for another day. The present fix might theoretically slow things down if output checking is truly unnecessary, but I doubt that would cause a significant regression in practice. Fixes #20843. --- .../build/lib/buildtool/ExecutionTool.java | 17 ++++++--- .../remote/build_without_the_bytes_test.sh | 38 +++++++++++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java b/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java index 55fcd83b526be6..1e9c84a0cc01f6 100644 --- a/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java +++ b/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java @@ -519,11 +519,18 @@ private ModifiedFileSet startBuildAndDetermineModifiedOutputFiles( startLocalOutputBuild(); } } - if (!request.getPackageOptions().checkOutputFiles - // Do not skip invalidation in case the output tree is empty -- this can happen - // after it's cleaned or corrupted. - && !modifiedOutputFiles.treatEverythingAsDeleted()) { - modifiedOutputFiles = ModifiedFileSet.NOTHING_MODIFIED; + if (!request.getPackageOptions().checkOutputFiles) { + // Do not skip invalidation for a run command, thereby making sure outputs are downloaded even + // if they were previously built with --remote_download_minimal. + // See https://github.com/bazelbuild/bazel/issues/20843. + if (request.getCommandName().equals("run")) { + return ModifiedFileSet.EVERYTHING_MODIFIED; + } + // Do not skip invalidation in case the output tree is empty -- this can happen after it's + // cleaned or corrupted. + if (!modifiedOutputFiles.treatEverythingAsDeleted()) { + return ModifiedFileSet.NOTHING_MODIFIED; + } } return modifiedOutputFiles; } diff --git a/src/test/shell/bazel/remote/build_without_the_bytes_test.sh b/src/test/shell/bazel/remote/build_without_the_bytes_test.sh index 29687aa6d3de92..7f2736e505cef8 100755 --- a/src/test/shell/bazel/remote/build_without_the_bytes_test.sh +++ b/src/test/shell/bazel/remote/build_without_the_bytes_test.sh @@ -2210,4 +2210,42 @@ EOF //:foo >& $TEST_log || fail "Failed to build //:foo" } +function test_incremental_run_command_with_no_check_output_files() { + # Regression test for https://github.com/bazelbuild/bazel/issues/20843. + cat > BUILD <<'EOF' +genrule( + name = "gen", + outs = ["out.txt"], + cmd = "touch $@", +) +sh_binary( + name = "foo", + srcs = ["foo.sh"], + data = ["out.txt"], +) +EOF + cat > foo.sh <<'EOF' +#!/bin/bash +if ! [[ -f "$0.runfiles/_main/out.txt" ]]; then + echo "runfile $0.runfiles/_main/out.txt not found" 1>&2 + exit 1 +fi +EOF + chmod +x foo.sh + + CACHEDIR=$(mktemp -d) + FLAGS=(--disk_cache="$CACHEDIR" --remote_download_minimal --noexperimental_check_output_files) + + # Populate the disk cache. + bazel build "${FLAGS[@]}" //:foo >& $TEST_log || fail "Failed to build //:foo" + + # Clean build. No outputs are considered top-level, so nothing is downloaded. + bazel clean "${FLAGS[@]}" + bazel build "${FLAGS[@]}" //:foo >& $TEST_log || fail "Failed to build //:foo" + + # Incremental run. Even though output checking is disabled, the dirtiness + # check must still occur to force them to be downloaded. + bazel run "${FLAGS[@]}" //:foo >& $TEST_log || fail "Failed to run //:foo" +} + run_suite "Build without the Bytes tests"