2.4.4 Diagnosing Cache Misses
Unexpected rebuilds are a different debugging problem from failed builds. When bazel build succeeds but reruns work you expected to be up to date, start with --explain=/tmp/explain.log: Bazel writes, for each build step, why it ran or why it considered the step up to date. This is the first-response tool for cache misses at this level. For failing actions and sandbox problems, use 2.3.4 First-Response Debugging Flags instead. Read the explain log as a human-readable view of 2.4.3 What Makes a Cache Hit.
Start with an explain log
bazel build //app:server --explain=/tmp/explain.log
# Add this when the first log points at configuration drift
bazel build //app:server \
--explain=/tmp/explain.log \
--announce_rc
--announce_rc makes Bazel print which rc files supplied options, which is often the missing clue when local and CI do not build with the same effective flags.1 For a short debugging session, keeping --explain=... in .bazelrc can also be useful, but remove it again when you are done because the logging has a performance cost.1
--explain only covers builds that ran with it. If the surprising rebuild already happened, the server log still records which source files Bazel saw change since the previous command:
grep 'DiffAwareness found' "$(bazel info server_log)"
On Bazel 9.1.0, editing one input and rebuilding logs DiffAwareness found 1 modified source files and directory listings for /path/to/workspace: [/path/to/workspace]/[a.txt]. The line names at most 10 changed entries per check. A detected change is not proof that anything reran, because Bazel may still find every affected action up to date. Use the line to confirm which edit Bazel noticed, then rebuild with --explain to see which actions it caused.2
What the log is actually telling you
The explain file does not invent a new cache model. It surfaces Bazel's existing reuse decision in plain language. If an action reran because one of its inputs changed, that is the expected consequence of the cache-key rules from 2.4.3 What Makes a Cache Hit and of Bazel's general incremental-build guarantee: if the inputs or the build command change, Bazel reruns the affected step.3
When the log says the command changed, compare the two invocations’ execution logs using 5.7.2 Execution Log Analysis. On Bazel 8, you can add --verbose_explanations to the explain command to include command details. Bazel 9 removed that flag.4 Common reasons are different .bazelrc layers, an unexpected --config=..., or build flags drifting between invocations.1,5 Toolchain or compiler changes often show up in the same bucket: Bazel's action identity can depend on command details such as compiler flags, library locations, and other tool-related metadata, not just source files.5 The same flag churn also tends to discard the in-memory analysis cache, which is why iterative workflows get slower even before execution starts.6 This is where 3.2 Project Configuration becomes the right follow-up chapter: the cache miss is often real, but the hidden cause is configuration hygiene rather than a source edit.
Stamping is another common source of "why did this rerun?" confusion. --workspace_status_command writes stable and volatile status files before the build. Stable keys invalidate stamped actions when they change. Volatile keys are treated as expected churn and do not invalidate stamped actions by themselves.1 Specifying --stamp does not automatically force a rebuild when nothing else changed, but it does make those workspace-status files relevant to the action definition.1 If explain output keeps pointing at workspace-status-related actions or stamped binaries, the question is usually not "is caching broken?" but "should this build metadata be stamped in everyday development at all?" That connection is covered in 3.2.3 Stamping & Build Metadata.
Machine-specific misses usually point back to the same leak paths from 2.4.3 What Makes a Cache Hit and 2.3.1 Hermeticity: environment differences such as $PATH change action definitions across machines, while host tools outside the workspace are a warning sign that the environment is not being modeled cleanly.7 The --incompatible_strict_action_env setting exists precisely to shrink this class of problems.8
If the first --explain log is too vague, keep the same target and add one clue at a time instead of changing everything at once:
- A pair of execution logs when Bazel says the command changed
--announce_rcwhen you suspect.bazelrcor--config=...drift- stamping checks when workspace-status-related actions are the noisy input
This keeps the debug session close to the rebuild you are trying to explain instead of jumping immediately to heavier tools.
When explain is no longer enough
--explain is intentionally a first-response tool. It is great at answering "why did Bazel rerun this build step?" but not at exhaustively diffing two invocations. If the log shows that something changed but you still cannot tell which input, environment field, or action detail diverged, the next tool is execution-log comparison in 5.7 Cache & Execution.9 That is the deeper workflow for non-hermetic actions, cross-machine drift, and cache misses that survive the obvious checks.
--explain reports on actions. When the surprise is that one small edit made Bazel reload packages or reanalyze many targets, Bazel 10 adds a server-side view: run bazel dump --skyframe=changed right after the surprising command, before any other build replaces it as the last command. It lists the in-memory graph values that command created or changed, starting from the origins such as the edited file, so you can follow the chain from the edit to the reanalyzed targets and rerun actions.10 It is a developer-facing dump, and Bazel 9.x does not have it. 5.6.7 bazel dump — Internal State Inspection explains how to read it.
Use --explain when the build succeeds but rebuilds unexpectedly. Add --announce_rc for rc-file drift and compare execution logs to identify command differences. Most "mysterious" misses turn out to be configuration, stamping, or environment differences, and the ones that do not are the handoff point to 5.7 Cache & Execution.1,6,7,9
Check your understanding · 3 questions
1.What problem does --explain address, and how does it differ from --verbose_failures?
Select one answer
2.Which flags complement --explain on Bazel 8 when diagnosing why a build step was rebuilt?
Select all that apply
3.True or false: choosing first-response cache-miss diagnostics.
Choose True or False for each sentence
--explain=/path/to/log is the starting point when a build succeeds but reruns work you expected to be cached.--verbose_explanations is useful by itself even when --explain is not enabled.--announce_rc helps when local and CI builds may be using different rc-file or --config settings.Footnotes
-
Commands and Options —
--explain,.bazelrc,--announce_rc,--config, workspace status, and--stamp↩1 ↩2 ↩3 ↩4 ↩5 ↩6 -
What Bazel Thinks Has Changed — searching
$(bazel info server_log)forDiffAwareness foundto list detected changes without prior setup, the 10-entry limit, and that a detected change need not rerun an action. The log line above is from a Bazel 9.1.0 run ↩ -
Build programs with Bazel — correct incremental rebuilds rerun steps when inputs or the build command change ↩
-
Bazel 8.8.1 build options and Bazel 9.0.0 build options — the latter keeps
--explainand drops--verbose_explanations. ↩ -
Bazel Glossary — definitions of action key and configuration, including build flags and tool-related metadata that affect action identity ↩1 ↩2
-
Optimize Iteration Speed — flag changes and startup-option changes discard analysis cache and slow iterative workflows ↩1 ↩2
-
Remote Caching — action definitions include environment variables. Differing
$PATHand untracked host tools break predictable cache sharing ↩1 ↩2 -
.bazelrc flags you should enable —
--incompatible_strict_action_envas a practical guard against environment leakage and large cache misses ↩ -
Bazel and action (non-) determinism —
--explainexample for unexpected rebuilds and execution-log comparison for deeper non-determinism diagnosis ↩1 ↩2 -
Bazel 10.0.0rc1 release notes — new
bazel dump --skyframe=changedlists Skyframe values the last command created or changed with the changed dependencies that caused each change ↩