Skip to content

fix(daemon): restore depfile on cache hit (closes #643) - #644

Merged
zackees merged 3 commits into
mainfrom
fix/depfile-restore-on-cache-hit
Jun 4, 2026
Merged

zackees merged 3 commits into
mainfrom
fix/depfile-restore-on-cache-hit

Conversation

@zackees

@zackees zackees commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

Summary

On a cache hit zccache currently restores only the .obj — the user's .d depfile (-MD -MF <path>) is silently absent. Downstream build systems with deps = gcc then read an absent depfile, record zero dependencies for the object, and stop recompiling when included headers change. After a git pull that touches headers, this surfaces as stale incrementals and undefined symbol link errors.

This PR makes the miss path capture the user's depfile bytes as a second artifact output, and the hit path write them back to the current build's -MF destination alongside the .obj. Cache-file mtime preservation is unchanged.

Downstream impact

Reported in the FastLED organization by way of #643's reproducer (12-line shell script). Without this fix the symptom is a quietly stale .o whose missing header changes only surface at link time — the kind of bug that wastes hours bisecting "a working build went bad."

Root cause

cached_hit::materialize_cached_compile_hit built its targets vector purely from payloads.len() × output_names. It never wrote the depfile that the original miss-time depfile parse had seen, because:

  • the miss path only stored output_data (the .obj) into ArtifactData::outputs[0], and
  • the hit path had no notion of "the user asked for an .d here, please put one back."

Fix

  • New depgraph::depfile::user_depfile_destination(dep_flags, output) — single source of truth for "where does the current build want its depfile?" (-MF <path> when present, else <output>.d when -MD is set, else None). Applies only to the UserSpecified / UserDefault strategies; the Injected strategy (zccache added the flags purely for its own depgraph use) is intentionally excluded.
  • pipeline.rs: on the C/C++ miss path, after parse_depfile_path succeeds for UserSpecified / UserDefault, also std::fs::read(path) and thread the bytes into the new MissArtifactStoreRequest::user_depfile field.
  • miss_store::store_single_output: when user_depfile is Some, push a second ArtifactOutput for the depfile (basename used as a stable identifier; the on-disk destination on hit always comes from the current request).
  • cached_hit::materialize_cached_compile_hit: new current_depfile_dest: Option<NormalizedPath> field. When the cached artifact has 2 outputs AND the field is Some, routes payloads[1] to that destination. Legacy single-output cache entries from before this PR are untouched (the 2-output check is what gates the behavior, not the field alone).
  • hit_branches::{try_fast_hit, try_depgraph_cached_hit}: derive current_depfile_dest from the passed-in dep_flags, and stash it into the RequestCacheEntry::depfile_path so subsequent ultra-fast try_request_cache_hit calls (which run before arg parsing) also restore the depfile.
  • RequestCacheEntry / request_cache_entry: depfile destination is stored as a CachedRequestPath so cross-worktree shareable hits rebase it to the current key root just like source_path / output_path. If the user supplied an absolute path outside the key root, the entry falls back to worktree-bound semantics.

Coordinated with FastLED/FastLED — downstream stale-build investigation.

Test plan

  • Failing test landed first as a separate commit (7bf6ae6) — cached_hit_restores_user_depfile_alongside_object. Run against unmodified main: the assertion assert!(depfile_dest.as_path().exists(), ...) panics with the issue-depfile not restored on cache hit -> incremental build silently produces stale objects #643 stale-incremental-build message. Run against the impl commit (b1d6c37): passes.
  • Legacy guard: cached_hit_object_only_artifact_ignores_depfile_dest — passing a current_depfile_dest with a 1-output cached artifact must NOT manufacture a depfile.
  • Helper unit tests in depgraph::depfile cover the three user_depfile_destination branches (mf_path wins / -MD derives default / no flags → None).
  • soldr cargo fmt --all clean.
  • soldr cargo clippy --workspace --all-targets -- -D warnings clean.
  • ./test (-p zccache --lib) all 1690 unit tests green.
  • ./test --integration — not run locally (low disk; pre-existing flake noted in daemon_rustc_restore_test is on the rustc path and unaffected by this fix, which only touches C/C++ depfile flow).

Out of scope (mentioned, not fixed)

  • The rustc cache-hit dep-info restore path uses a separate persist mechanism (store_rustc_outputs) and is not modified here. Its dep-info file is already part of the snapshotted rustc outputs.
  • MSVC /showIncludes has no on-disk depfile to capture; the ShowIncludes strategy is correctly excluded by the helper.

🤖 Generated with Claude Code

zackees and others added 3 commits June 3, 2026 20:53
Pins the bug: when zccache wraps `clang++ -MD -MF <depfile>` (the shape
emitted by build systems with `deps = gcc`), a cache hit currently
restores only the `.obj` — the user's `.d` is silently absent. The
downstream build tool then records zero dependencies for the object and
stops recompiling when included headers change, producing stale objects
and `undefined symbol` link errors after `git pull`.

Wires `current_depfile_dest: Option<NormalizedPath>` into
`CachedHitMaterializeRequest` so the caller can supply *this build's*
`-MF` destination on a hit (independent of where the depfile lived when
the miss recorded it). The handler body intentionally does not yet
consume the field — the next commit implements the restore and flips
this test from RED to GREEN.

Refs #643.
When zccache wraps `clang++ -MD -MF <depfile>` (the shape build systems
emit for `deps = gcc`), the miss path now captures the user's depfile
bytes as a second artifact output, and the hit path writes them back
alongside the `.obj` to the *current* build's `-MF` destination.

Before this fix, only the `.obj` was restored on a hit. The build tool
then read an absent depfile, recorded zero dependencies for the object,
and stopped recompiling when included headers changed — silently
producing stale objects and `undefined symbol` link errors after
`git pull`.

The depfile destination on hit is derived from the current request's
dep_flags (not the cached basename), so renamed-output / cross-worktree
hits restore the depfile to the correct place. Legacy single-output
artifacts cached before this fix keep working unchanged: the hit-path
materializer only consults `current_depfile_dest` when the artifact
actually has a second payload. Cache-file mtime preservation is
unchanged — the depfile write goes through the same
`write_payloads_par_with_mtime_floor` path the `.obj` already used.

Touched layers:
- `depgraph::depfile::user_depfile_destination` — single source of truth
  for "where does the current build want its depfile?"
- `pipeline::handle_compile_request` — on miss, after parse_depfile_path
  succeeds in `UserSpecified` / `UserDefault`, also reads the depfile
  bytes and threads them into `MissArtifactStoreRequest::user_depfile`.
- `miss_store::store_single_output` — extends the artifact with a second
  output when `user_depfile` is `Some`.
- `cached_hit::materialize_cached_compile_hit` — extra
  `current_depfile_dest` field; routes payloads[1] to it when both the
  artifact has two outputs and the field is `Some`.
- `hit_branches::{try_fast_hit, try_depgraph_cached_hit}` — derive
  `current_depfile_dest` from passed-in `dep_flags`, also stamp it into
  the new `RequestCacheEntry::depfile_path` so the ultra-fast request
  cache path restores the depfile on subsequent hits.
- `request_cache::RequestCacheEntry` + `keys::request_cache_entry` —
  store the depfile destination as a `CachedRequestPath` so
  cross-worktree fast-path hits rebase it to the new root.

Coordinated with FastLED/FastLED — downstream stale-build investigation.

Closes #643.
The depfile-fix work accidentally overwrote the in-progress Static
Library (.a) Caching planning content with its own scratch notes.
This commit restores tasks/todo.md to main's content so the PR is
scoped only to the depfile-on-cache-hit fix.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@zackees
zackees merged commit 5fb9f54 into main Jun 4, 2026
26 checks passed
@zackees
zackees deleted the fix/depfile-restore-on-cache-hit branch June 4, 2026 04:25
zackees added a commit that referenced this pull request Jun 4, 2026
Patch release for #643/#644 — restore depfile on cache hit.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
zackees added a commit to FastLED/FastLED that referenced this pull request Jun 4, 2026
)

1.11.11 ships forge-ci/zccache#644 — cache hits now restore the user's
`-MF <path>` depfile alongside the .obj.

Without it, build systems reading depfiles silently record zero
dependencies for cached objects, and from then on header changes
never trigger rebuilds. The symptom for FastLED was `undefined
symbol: ...` link errors hours after a `git pull` that modified a
transitively included header, with no obvious reason for the build
to be using stale objects. A clean rebuild worked around it but
threw away the whole incremental-build benefit.

Closes the local-side of forge-ci/zccache#643. The fix landed in
forge-ci/zccache@5fb9f54 and shipped to PyPI as zccache 1.11.11.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
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