← notes

.cache committed to git, and the stale build it can cause

Jan 21, 2020

A Gatsby site, with .cache/ committed to git.

351KB of JavaScript in the repository is not the site. It is Gatsby’s build cache: generated loaders, MDX diskstore JSON keyed by content hash, babel state, and Jest snapshots of Gatsby’s own internals. None of it is source.

This is common enough to be worth writing down. It happens because Gatsby creates .cache/ on the first build, git status shows a wall of unfamiliar files, and adding them is faster than working out what they are. After that every build produces hundreds of meaningless changes, which buries real ones and makes git log useless for the thing it exists for.

The failure with teeth is a stale committed cache being used instead of a fresh build. Entries keyed by content hash are valid for the machine that made them; checked out elsewhere, a build can pick up an entry that no longer matches the source and produce output corresponding to neither. That is unpleasant to debug, because the source is correct and the output is wrong.

The fix is two .gitignore lines and a decision about history.

reserves.wrapped.com commits .sass-cache/ and writings commits LaTeX .aux and .blg files, same mistake, three ecosystems.