What Belongs in a Build Cache Key

A cache key for a generated artifact should represent the inputs and settings required to distinguish incompatible outputs.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a configuration difference
  2. Include the tools that matter
  3. Find an omitted dependency that would produce a false cache hit
  4. Check false misses too

Trace a configuration difference

The same source compiled with a debug flag and without it can produce different outputs. A key using only the source identity can reuse the wrong one. A simple result cache has the same key-completeness requirement.

Include the tools that matter

Tool version, target architecture, and dependency versions can affect artifacts. Determine what the cache promises to reuse, then test a changed setting. Reproducing a build gives a practical inventory.[1]

Find an omitted dependency that would produce a false cache hit

A compiler task can use identical source bytes with two target architectures. If the cache key contains only source digest, the second build can receive an artifact built for the first target. Include configuration and relevant tool identity with the declared inputs. See toolchain and target settings.

Source digest S, target X, toolchain T → key K1
Source digest S, target Y, toolchain T → key K2
Reuse between K1 and K2 is safe only if output is provably independent of this field

Check false misses too

Including the current time in every key defeats reuse if time has no effect on output. Test reused outputs against a cold rebuild with the same declared inputs and verify output identity.

References

  1. Free Software Foundation, GNU Make Manual ↩