Skip to content

Make builds deterministic across output bases and tighten hermeticity - #17

Merged
pawelchcki merged 4 commits into
mainfrom
build-hermetic-caching
Oct 8, 2026
Merged

pawelchcki merged 4 commits into
mainfrom
build-hermetic-caching

Conversation

@pawelchcki

Copy link
Copy Markdown
Owner

Summary

  • Fix the one remaining source of nondeterminism. rav1e's and av-scenechange's build scripts echo CARGO_ENCODED_RUSTFLAGS back, and the build-script runner in the rules_rust that rules_rs ships leaves --remap-path-prefix=<exec_root>=. unredacted. Each run's sandbox path ends up in _bs.env, so neither crate was ever a cache hit on a fresh output base. A local patch (bazel/rules_rs_build_script_env_determinism.patch) fixes it until fix: redact exec root before = in build script env output hermeticbuild/rules_rust#84 ships in a rules_rs release.
  • Drop rules_rs_bootstrap_determinism.patch. The pinned rules_rs already includes the upstream fix (fix: remap bootstrap source paths in rustc hermeticbuild/rules_rust#53). Without our patch, process_wrapper is byte-identical across output bases and embeds no absolute paths.
  • .bazelrc hermeticity:
    • No network inside sandboxed actions.
    • No autodetected host C/C++ toolchain.
    • Persistent workers (Javac, Kotlin, Android) run sandboxed.
  • .bazelrc speed:
    • A disk cache at ~/.cache/bazel-disk-cache, shared by every checkout and worktree and capped at 60G.
    • CI fails on a stale MODULE.bazel.lock.
    • CI downloads only what each step inspects.

Verification

  • Determinism: two clean builds in different checkouts and output bases, compared through --execution_log_json_file. Every spawn of //... and of the CI -c opt APK and WASM build has identical action keys and outputs. The APKs and .wasm are bit-for-bit identical.
  • Offline: the full //... build and the -c opt APK/WASM build pass with sandbox networking disabled.
  • Disk cache: a fresh worktree builds and tests //... in 10s, all disk-cache hits, against 97s from clean. The -c opt APK/WASM build takes 41s, against 168–379s.
  • Pipelined Rust compilation: measured and left off. It was slower on clean 24-core builds: 110s vs 97s for //... and 184s vs 168s for opt.

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=<exec_root>=.`. 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.
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.
@pawelchcki

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-08T09:14:16.401634Z f89767e Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 1f4c167ac5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Since main's #14, CI builds and tests only in -c opt, so there is no
separate fastbuild output tree.
@pawelchcki

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: f89767eded

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@pawelchcki
pawelchcki merged commit 30e697a into main Oct 8, 2026
2 checks passed
@pawelchcki
pawelchcki deleted the build-hermetic-caching branch October 8, 2026 10:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant