From 5bc9620f51d254f771ff3167616be7e168f2b3c2 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 | 14 ++++--- .../remote/build_without_the_bytes_test.sh | 38 +++++++++++++++++++ 2 files changed, 47 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..52606047863782 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,15 @@ 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 output invalidation in the following cases: + // 1. If the output tree is empty: this can happen after it's cleaned or corrupted. + // 2. For a run command: so that outputs are downloaded even if they were previously built + // with --remote_download_minimal. See https://github.com/bazelbuild/bazel/issues/20843. + if (!modifiedOutputFiles.treatEverythingAsDeleted() && !request.getCommandName() + .equals("run")) { + 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..71cf0742e9b2a8 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, invalidation must + # 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"