From 446d29f8b6da9923a77660dc89f698489dbe619a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chojnacki?= Date: Thu, 8 Oct 2026 02:08:21 +0200 Subject: [PATCH 1/3] Make builds deterministic across output bases and tighten hermeticity Two clean builds of this commit in different checkouts and output bases now produce identical action keys and identical outputs for every spawn of `//...` and of the CI `-c opt` APK and WASM build, and the APKs and WASM are bit-for-bit identical. Before this, rav1e and av-scenechange were never cache hits on a fresh output base: their build scripts echo CARGO_ENCODED_RUSTFLAGS back as `cargo:rustc-env`, and that value carries `--remap-path-prefix==.`. The rules_rust build-script runner shipped by rules_rs only redacts the exec root when a `/` or `\` follows it, so the per-action sandbox path leaked into `_bs.env`. A local patch also redacts it before `=`. The response-file bootstrap patch is dropped: the pinned rules_rs already includes the upstream fix (hermeticbuild/rules_rust#53), and without the patch process_wrapper is byte-identical across output bases and embeds no absolute paths. .bazelrc: - Disable sandbox networking. Every toolchain is downloaded and checksummed; the full `//...` and `-c opt` APK/WASM builds pass offline. - Never autodetect a host C/C++ toolchain; @llvm is the only one. - Sandbox persistent workers (Javac, Kotlin, Android tools), which otherwise run in the unsandboxed execroot. - Share a disk cache across checkouts and worktrees, garbage collected at 60G. Action keys are path-independent, so a fresh worktree reuses every other worktree's outputs. CI keeps using its output base and BuildBuddy. - CI: fail on a stale MODULE.bazel.lock and download only what each step inspects. Pipelined Rust compilation was measured and left off: on clean 24-core builds it was slower for both `//...` (110s vs 97s) and the `-c opt` APK/WASM build (184s vs 168s), since the builds are CPU-bound rather than critical-path-bound. --- .bazelrc | 23 +++++++ MODULE.bazel | 11 +++- bazel/BUILD.bazel | 2 +- bazel/rules_rs_bootstrap_determinism.patch | 64 ------------------- ...ules_rs_build_script_env_determinism.patch | 14 ++++ 5 files changed, 46 insertions(+), 68 deletions(-) delete mode 100644 bazel/rules_rs_bootstrap_determinism.patch create mode 100644 bazel/rules_rs_build_script_env_determinism.patch diff --git a/.bazelrc b/.bazelrc index 9b10eee..122fb73 100644 --- a/.bazelrc +++ b/.bazelrc @@ -10,6 +10,21 @@ common --tool_java_runtime_version=remotejdk_25 # Match Gradle's manifest merger: plugin library permissions must reach the APK. common --merge_android_manifest_permissions +# Hermeticity. Every toolchain (LLVM, Rust, JDK, Android SDK/NDK, Dart/Flutter, +# Go) is downloaded and checksummed, so no action needs the network, and no +# host C/C++ compiler may be autodetected as a fallback toolchain. +common --sandbox_default_allow_network=false +common --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 +# Persistent workers (Javac, Kotlin, Android tools) otherwise run unsandboxed +# and can read undeclared inputs from the execroot. +common --worker_sandboxing + +# A disk cache shared by every checkout and worktree on this machine: action +# keys are independent of the workspace path, so a fresh worktree reuses the +# outputs of all others instead of rebuilding from scratch. +common --disk_cache=~/.cache/bazel-disk-cache +common --experimental_disk_cache_gc_max_size=60G + # BuildBuddy is opt-in so a fresh clone works without credentials. See # user.bazelrc.example; --config=remote also enables this cache/BES config. build:buildbuddy --bes_backend=grpcs://remote.buildbuddy.io @@ -32,6 +47,14 @@ build:buildbuddy --build_metadata=REPO_URL=https://github.com/pawelchcki/razemif # the workflow runner because this organization has no remote-executor pool. build:ci --config=buildbuddy build:ci --build_metadata=ROLE=CI +# The runner's output base and the BuildBuddy cache already cover what the +# local disk cache would, without spending runner disk on a third copy. +common:ci --disk_cache= +# Fail rather than silently re-resolve when MODULE.bazel.lock is stale. +common:ci --lockfile_mode=error +# Only download what a step inspects; steps that need artifacts on disk pass +# --remote_download_toplevel explicitly. +build:ci --remote_download_minimal # //:clippy is a gate, so warnings must fail it -- the equivalent of # `cargo clippy -- -D warnings`. diff --git a/MODULE.bazel b/MODULE.bazel index fabce0e..f01484c 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -91,11 +91,16 @@ register_toolchains( # rules_rs is implemented on a patched compatibility repository. Keep that # implementation detail behind a rules_rs-owned apparent name; Razemify's Rust -# build rules are loaded exclusively from @rules_rs. The local patch fixes -# response-file substitutions while rules_rs bootstraps its process wrapper. +# build rules are loaded exclusively from @rules_rs. The local patch redacts the +# exec root from build-script env output when it precedes `=`, as in the +# `--remap-path-prefix==.` that rav1e and av-scenechange echo back +# from CARGO_ENCODED_RUSTFLAGS. Unpatched, those outputs embed the per-action +# sandbox path, so neither crate is ever a cache hit on a fresh output base. +# Drop once https://github.com/hermeticbuild/rules_rust/pull/84 reaches a +# rules_rs release. rules_rs_internal = use_extension("@rules_rs//rs:rules_rust.bzl", "rules_rust") rules_rs_internal.patch( - patches = ["//bazel:rules_rs_bootstrap_determinism.patch"], + patches = ["//bazel:rules_rs_build_script_env_determinism.patch"], strip = 1, ) use_repo(rules_rs_internal, rules_rs_internal = "rules_rust") diff --git a/bazel/BUILD.bazel b/bazel/BUILD.bazel index c499c57..cc7d0dd 100644 --- a/bazel/BUILD.bazel +++ b/bazel/BUILD.bazel @@ -16,5 +16,5 @@ exports_files([ "defs.bzl", "rules_android_manifest_log_determinism.patch", "rustix_determinism.patch", - "rules_rs_bootstrap_determinism.patch", + "rules_rs_build_script_env_determinism.patch", ]) diff --git a/bazel/rules_rs_bootstrap_determinism.patch b/bazel/rules_rs_bootstrap_determinism.patch deleted file mode 100644 index 948634e..0000000 --- a/bazel/rules_rs_bootstrap_determinism.patch +++ /dev/null @@ -1,64 +0,0 @@ -diff --git a/util/process_wrapper/private/bootstrap_process_wrapper.cc b/util/process_wrapper/private/bootstrap_process_wrapper.cc ---- a/util/process_wrapper/private/bootstrap_process_wrapper.cc -+++ b/util/process_wrapper/private/bootstrap_process_wrapper.cc -@@ -1,6 +1,7 @@ - #include - #include - #include - #include -+#include - #include - #include -@@ -138 +139,40 @@ bool replace_environment_placeholders(const char* name, -+bool expand_response_file(std::string* arg, -+ const std::string& pwd, -+ const std::string& output_base, -+ const std::string& exec_root) { -+ if (arg->empty() || arg->front() != '@') { -+ return true; -+ } -+ -+ const std::string input_path = arg->substr(1); -+ const std::string output_path = input_path + ".expanded"; -+ std::ifstream input(input_path); -+ if (!input) { -+ std::fprintf(stderr, -+ "bootstrap_process_wrapper: cannot read response file %s\n", -+ input_path.c_str()); -+ return false; -+ } -+ std::ofstream output(output_path, std::ios::trunc); -+ if (!output) { -+ std::fprintf(stderr, -+ "bootstrap_process_wrapper: cannot write response file %s\n", -+ output_path.c_str()); -+ return false; -+ } -+ -+ std::string line; -+ while (std::getline(input, line)) { -+ output << replace_placeholders(line, pwd, output_base, exec_root) << '\n'; -+ } -+ if (!input.eof() || !output) { -+ std::fprintf(stderr, -+ "bootstrap_process_wrapper: failed expanding response file %s\n", -+ input_path.c_str()); -+ return false; -+ } -+ *arg = "@" + output_path; -+ return true; -+} -+ - } // namespace -@@ -132,6 +172,9 @@ int main(int argc, char** argv) { - std::vector command_args; - command_args.reserve(static_cast(argc - first_arg_index)); - for (int i = first_arg_index; i < argc; ++i) { -- command_args.push_back( -- replace_placeholders(argv[i], pwd, output_base, exec_root)); -+ std::string arg = replace_placeholders(argv[i], pwd, output_base, exec_root); -+ if (!expand_response_file(&arg, pwd, output_base, exec_root)) { -+ return 1; -+ } -+ command_args.push_back(arg); - } diff --git a/bazel/rules_rs_build_script_env_determinism.patch b/bazel/rules_rs_build_script_env_determinism.patch new file mode 100644 index 0000000..d4f4ded --- /dev/null +++ b/bazel/rules_rs_build_script_env_determinism.patch @@ -0,0 +1,14 @@ +--- a/cargo/private/cargo_build_script_runner/lib.rs ++++ b/cargo/private/cargo_build_script_runner/lib.rs +@@ -340,6 +340,11 @@ + value + .replace(&format!("{exec_root}/"), "${pwd}/") + .replace(&format!("{exec_root}\\"), "${pwd}\\") ++ // Flags such as `--remap-path-prefix==.` (which build ++ // scripts see in CARGO_ENCODED_RUSTFLAGS and may echo back as ++ // `cargo:rustc-env`) follow the exec root with `=`. Leaving it ++ // unredacted bakes the per-action sandbox path into the output. ++ .replace(&format!("{exec_root}="), "${pwd}=") + } + + /// Redact for env vars: uses the generic `${out_dir}` token, resolved From 1f4c167ac53765b7909d6cd30f48753a172abc53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chojnacki?= Date: Thu, 8 Oct 2026 10:37:16 +0200 Subject: [PATCH 2/3] ci: request a larger BuildBuddy workflow VM The default Linux workflow VM (3 CPU, 8GB RAM, 20GB disk) ran out of disk in the -c opt APK step: the Android SDK/NDK, LLVM, Flutter and JDK repositories plus the fastbuild and opt output trees on a recycled runner exceed 20GB. More CPUs also shorten every remote-cache miss. Drop --remote_download_toplevel from the buildbuddy config: it is Bazel's default, and with --config=ci adding --remote_download_minimal it only produced a conflicting-expansion warning. --- .bazelrc | 1 - buildbuddy.yaml | 8 ++++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/.bazelrc b/.bazelrc index 122fb73..68fa4c4 100644 --- a/.bazelrc +++ b/.bazelrc @@ -35,7 +35,6 @@ build:buildbuddy --experimental_remote_downloader_local_fallback build:buildbuddy --remote_timeout=10m build:buildbuddy --remote_cache_compression build:buildbuddy --remote_cache_async -build:buildbuddy --remote_download_toplevel build:buildbuddy --nolegacy_important_outputs build:buildbuddy --bes_upload_mode=fully_async diff --git a/buildbuddy.yaml b/buildbuddy.yaml index 140cd85..f403e1a 100644 --- a/buildbuddy.yaml +++ b/buildbuddy.yaml @@ -9,6 +9,14 @@ actions: - "*" container_image: "ubuntu-24.04" timeout: "30m" + # The default Linux workflow VM (3 CPU, 8GB RAM, 20GB disk) runs out of + # disk once the Android SDK/NDK, LLVM, Flutter and JDK repositories, the + # fastbuild and opt output trees, and the recycled runner's state add up, + # and 3 CPUs make every remote-cache miss slow. + resource_requests: + cpu: 16 + memory: 32GB + disk: 60GB steps: - run: bazel test --config=ci //... From f89767eded28d58c0e572bbc13e59f5084c9bd1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chojnacki?= Date: Thu, 8 Oct 2026 11:02:23 +0200 Subject: [PATCH 3/3] ci: describe the VM sizing for the single opt build step Since main's #14, CI builds and tests only in -c opt, so there is no separate fastbuild output tree. --- buildbuddy.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/buildbuddy.yaml b/buildbuddy.yaml index 35ade6e..4f55fda 100644 --- a/buildbuddy.yaml +++ b/buildbuddy.yaml @@ -11,8 +11,8 @@ actions: timeout: "30m" # The default Linux workflow VM (3 CPU, 8GB RAM, 20GB disk) runs out of # disk once the Android SDK/NDK, LLVM, Flutter and JDK repositories, the - # fastbuild and opt output trees, and the recycled runner's state add up, - # and 3 CPUs make every remote-cache miss slow. + # output tree, and the recycled runner's state add up, and 3 CPUs make + # every remote-cache miss slow. resource_requests: cpu: 16 memory: 32GB