Skip to content

setup: share one go mod cache per OS/arch, and keep it complete - #168

Merged
wagoodman merged 2 commits into
mainfrom
shared-mod-cache
Oct 23, 2026
Merged

wagoodman merged 2 commits into
mainfrom
shared-mod-cache

Conversation

@wagoodman

@wagoodman wagoodman commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

The mod cache is keyed on cache-key-prefix, the same prefix as the build cache, and any job on main can save it. Two problems come out of that, both visible across the org's repos today:

  • thin mod caches get stuck. Keys are immutable, so whichever job saves a key first owns it until go.sum changes. In chronicle, a dependabot burst put four main runs in flight at once; Static analysis saved v1-X64-Linux-go-mod with 421 modules, that entry was gone minutes later (the repo sits at ~9.8/10GB), and Acceptance tests (Linux) re-saved the same key after downloading 4 modules. That 1MB entry has been live for six days, so every Linux job restores it, downloads everything, and fails to save
  • duplicate mod caches per prefix. Any job with its own build cache prefix (snapshot in most repos) also saves its own mod cache holding the same modules, ~0.8-0.9GB per go.sum generation in grype, chronicle and grant

What changes:

  • new mod-cache-key-prefix input, default v1, used for the mod cache instead of cache-key-prefix. Every job on an OS/arch now shares one mod cache whatever its build prefix. Jobs already on v1 keep their exact keys; snapshot jobs start reading the existing v1 entry
  • on a mod cache miss on the default branch, go mod download (root and .make) runs before the job, so whichever job saves the shared key, including after an eviction, leaves a complete cache

What stays the same:

  • writes only on the default branch, restore-only everywhere else. The download step only runs on the save path
  • OS and arch stay in every key, build caches keep their own prefix
  • no caller changes needed, including through syft's and grype's bootstrap wrappers

Tradeoffs:

  • every job that misses on main downloads the full module set once per go.sum change. PRs never pay it
  • an OS slice whose only job needs few modules still stores a full mod cache (grype's macOS acceptance slice would go from 1MB to ~800MB per generation). cache-save: false on that job is the opt-out
  • bumping cache-key-prefix no longer resets the mod cache; bump mod-cache-key-prefix for that

Not covered here, per repo and optional: a concurrency group on validations so a dependabot burst leaves one run writing instead of several, and aligning the Go version between syft's/grype's bootstrap wrappers and their direct setup calls (grype has two Linux build caches per generation from that split). Stuck entries like chronicle's clear on the repo's next go.sum change, or by deleting them.

- new `mod-cache-key-prefix` input, defaulting to `cache-key-prefix`, so a job with its own build cache (snapshot, race builds) can still restore the same mod cache as everything else
- on a mod cache miss on the default branch, `go mod download` runs before the job so the saved cache is complete whichever job saves it first

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
`mod-cache-key-prefix` now defaults to `v1` rather than following `cache-key-prefix`, so every job on an OS/arch shares one mod cache with no caller changes. The download-on-miss step is what keeps that safe: whichever job saves the shared key leaves a complete cache.

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
@wagoodman wagoodman changed the title setup: let jobs share one go mod cache across cache prefixes setup: share one go mod cache per OS/arch, and keep it complete Oct 23, 2026
@wagoodman
wagoodman marked this pull request as ready for review October 23, 2026 16:55
@wagoodman
wagoodman merged commit 87a55e9 into main Oct 23, 2026
21 checks passed
@wagoodman
wagoodman deleted the shared-mod-cache branch October 23, 2026 20:19
@wagoodman wagoodman added the bug Something isn't working label Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants