Zig 0.13.0 content-addressed build cache: what exactly gets verified after restoring a project backup?
0 reputation · 15 Apr 2025, 07:43 UTC
0 reputation · 15 Apr 2025, 07:43 UTC
Zig 0.13.0 replaced the build system's timestamp-based incremental checks with a content-addressed cache that hashes build outputs (documented in the release notes as "Build system: content-addressed cache for reliable incremental builds"). I want to rely on this to verify that a restored project backup is bit-identical to the original build state, rather than trusting preserved file timestamps.
The cache lives in the default cache directory (e.g. ~/.cache/zig), separate from the project tree. If only the project directory is backed up and restored — possibly on a different machine — the hashes used for comparison may not be present, and it is unclear whether the build silently does a full rebuild or reports anything about the verification failing. It is also unclear which artifacts are covered: object files and executables are mentioned, but generated headers, cached dependency fetches, and test binaries are ambiguous.
32275 reputation · 15 Apr 2025, 15:46 UTC
Restoring only the project tree verifies nothing. Zig 0.13.0's content-addressed cache stores its reference hashes in the cache directory (by default the global one, e.g. ~/.cache/zig), not in the project. If that directory is absent, the build has nothing to compare against, so it treats every artifact as a cache miss and rebuilds silently. There is no "hash mismatch" diagnostic because a mismatch requires a stored hash to disagree with — and you didn't restore one.
The 0.13.0 release replaced timestamp-based incremental checks with content addressing: inputs to each build step (source bytes, compiler flags, dependency versions) are hashed, and outputs are stored under those hashes. The practical consequences for your scenario:
Yes — restoring the cache directory alongside the project is the way to get an "up to date" no-op build, and that no-op is the closest thing Zig offers to confirmation of a faithful restore. If any restored source file differs from what the cache recorded, the input hash changes and that step (and its dependents) rebuild. So the sequence is:
zig build --cache-dir .zig-cache --global-cache-dir .zig-global-cache (or set this in your build environment) so both travel together.zig build. A fast no-op means inputs match the recorded hashes. Any rebuild step identifies which inputs diverged.rm -rf .zig-cache zig-out on a scratch copy, then zig build) and compare the resulting binary hashes against your original artifacts with sha256sum. Zig builds are largely reproducible for a fixed compiler version and flags, so identical outputs are good evidence — though this verifies reproducibility, not the cache mechanism itself.Zig 0.13.0 does not emit a user-facing distinction between "cache entry absent" and "cache entry present but stale" — both surface as the step simply running again. If you need visibility, zig build --verbose shows which steps execute; on a warm restored cache you should see almost none. One caveat worth flagging: this behavior is based on the documented 0.13.0 design, and cache internals have evolved in later releases, so verify against your exact zig version before relying on it in a backup policy.
The content-addressed cache is an incremental-build mechanism, not a backup-integrity tool. To use it as one: bundle the cache with the project, treat a no-op rebuild as your verification signal, and use external checksums of final artifacts if you need bit-identical proof independent of the build system.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.