2.3.3 Non-Determinism Sources

2.3.2 Sandboxing answers one question: did the action read only the files Bazel staged for it? Non-determinism is the next question: if Bazel reruns the same action with the same declared inputs and action definition, does it produce the same bytes again? When the answer is no, the build can look fine on one cached run and then change after a clean build, cache eviction, or execution on another machine.1,2,3

Same action, different bytes

The simplest example is intentionally boring:

genrule(
    name = "date",
    outs = ["date.txt"],
    cmd = "date >$@",
)

That rule is valid, but its output is a function of wall-clock time. Bazel will happily reuse the first result until the action is forced to run again. Then date.txt changes because the tool itself asked the clock, not because Bazel lost track of a declared dependency.1,3

That is the important distinction from 2.3.1 Hermeticity. Hermeticity is about Bazel knowing the real inputs. Non-determinism is about the tool still producing unstable output after those inputs are fixed.1,2

Common ways entropy leaks into outputs

The pattern is always the same: some value inside the action is less stable than the output contract assumes.

  • Build time and file metadata. Code generators, archive writers, and similar tools often stamp the current time or per-file timestamps into outputs. In C/C++, the preprocessor macros __DATE__ and __TIME__ are a classic source: they embed compilation wall-clock time into every translation unit that uses them.1,4,3
  • Host-specific identities and paths. Some tools embed PID, UID, GID, sandbox paths, output-base paths, or other absolute paths, so the bytes differ across runs or machines even when the logical source tree is the same.1,5,6
  • Unstable ordering. Hash-map iteration, unsorted directory walks, and serializers that do not sort their keys can reshuffle emitted output between runs.1,5,3,7
  • Randomness and live network reads. Random seeds and fetched data make the output depend on changing external state instead of only on declared source inputs.1,4,3

One subtlety: the damage only propagates as far as the changed bytes travel. A later action can still produce stable output from unstable input if it normalizes the data, but any downstream action that hashes the varying bytes will have to re-run when the unstable action re-executes.1 A small archive shows the cost. A genrule that packs a file with plain zip records modification timestamps, so after rm bazel-bin/output.zip the rebuilt archive differs and the genrule that unzips it runs again, although no source changed. Built with @bazel_tools//tools/zip:zipper instead, the archive comes out byte-identical and Bazel skips the consumer. The execution log shows only the archive action running. For archives, reach for a deterministic archiver such as zipper or rules_pkg rather than the host's zip or tar.8

When the tool itself will not cooperate

Fix non-determinism at its source when you can: a deterministic archiver, sorted output, a tool flag that pins the timestamp. A proprietary tool may offer none of these. Then the action has to normalize the tool's output before declaring it, and you need evidence that the normalization is complete.

Tweag's integration of Coverity, a proprietary static analyzer, for a client is a worked case. Each compile step wrote an SQLite database that recorded start and end times, process IDs, source-file timestamps, per-translation-unit command-line IDs, the host name, and absolute source paths whose number of stored segments changed with the depth of the execroot. An environment variable (COV_HOST) pinned the host name. SQL statements reset the other fields, and the path prefix up to the execroot was rewritten to a fixed string. The team iterated on those statements with sqldiff until two runs showed no difference in schema or data. That still did not give identical files, because editing a database in place does not make its binary layout reproducible. Dumping the database to SQL and loading the dump into a fresh file did. Once the outputs were bit-for-bit stable, the remote cache could reuse them, and the analysis moved from an uncached nightly job of over 12 hours into merge-request pipelines.9

The transferable check has two levels. First, compare two runs at the level of meaning, such as rows, entries, or a tool-specific diff, to find which fields vary. Then compare the raw bytes, because Bazel keys reuse on digests, and equal content in a different layout is still a different output.8,9

Why sandboxing still matters, but isn't enough

Sandboxing is still essential because undeclared inputs are usually missing from the action's staged working directory, so ordinary relative reads fail instead of silently poisoning the cache. But it does not rewrite a ZIP header to a fixed timestamp, sort a tool's serialized map output, or stop a generator from writing random bytes. Depending on strategy and platform it may also constrain network access differently, so the safe mental model is: sandboxing reduces hidden inputs. Determinism removes unstable behavior inside the action.1,4,10

If the same commit keeps rebuilding from clean builds or cache reuse never seems to stabilize, treat it as an execution problem, not just a dependency problem. 5.7.2 Execution Log Analysis later covers execution-log and artifact-diff workflows for finding the first action whose outputs diverge between runs, and 4.6 Toolchains & Platform Resolution explains how Bazel makes compiler and SDK selection explicit so machine-by-machine tool differences are no longer mistaken for "the same build."4,3,6

key takeaway

Sandboxing asks whether the action saw only the world Bazel prepared. Determinism asks whether that prepared world is enough to reproduce the same bytes every time. Bazel needs both: one removes hidden inputs, the other keeps outputs stable once those inputs are fixed.1,2,10

Check your understanding · 3 questions

1.Which of the following are common sources of non-determinism in build actions?

Select all that apply

2.What is the key distinction between a hermetic build problem and a non-determinism problem?

Select one answer

3.True or false: how non-determinism propagates and how sandboxing relates to it.

Choose True or False for each sentence

If a non-deterministic action produces different bytes on a re-run, downstream actions that consume those bytes will also be forced to re-run.
Enabling sandboxing is sufficient to prevent non-determinism in build actions.
0 of 3 answered

Footnotes

  1. Bazel and action (non-) determinism — date >$@ example, common entropy sources, propagation, and why sandboxing cannot fully stabilize actions ↩1 ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10

  2. Artifact-Based Build Systems — Bazel's action model assumes outputs are a function of declared inputs, but actions can still be made non-deterministic ↩1 ↩2 ↩3

  3. Debugging and fixing build non-determinism with Bazel — timestamps, random numbers, unstable dictionary ordering, and exec-log based investigation ↩1 ↩2 ↩3 ↩4 ↩5 ↩6

  4. How to keep a Bazel project hermetic? — archives, environment shaping, workspace status, and other practical sources of unstable output ↩1 ↩2 ↩3 ↩4

  5. Deterministic npm dependencies with Bazel — absolute paths and timestamps in generated dependency files causing cross-machine and cross-output-base differences ↩1 ↩2

  6. Reproducible builds with Bazel — reproducibility framing, absolute paths as output leaks, and hermetic toolchains as the long-term answer ↩1 ↩2

  7. rules_applecross repository map — a rules_apple patch sorts a directory listing before concatenating a tool's generated Swift files, because directory order differs between APFS and ext4 ↩

  8. Bazel Knowledge: reproducible outputs — plain zip genrule versus @bazel_tools//tools/zip:zipper, with execution-log output showing the downstream unzip action rerun in one case and skipped in the other ↩1 ↩2

  9. Integrating Coverity static analysis with Bazel — "Difficulties": volatile fields in Coverity's emit-db, COV_HOST, execroot path-prefix rewrite, sqldiff iteration, and dump-and-reload for bit-for-bit reproducible SQLite files; the earlier uncached nightly approach took over 12 hours ↩1 ↩2

  10. Sandboxing — filesystem isolation, sandbox strategy differences, and platform-dependent enforcement limits ↩1 ↩2