Report #17566
[bug\_fix] Cache never invalidates or restores stale dependencies despite changes to lockfiles
Use a dynamic cache key that includes a hash of the dependency lockfile \(e.g., \`key: npm-$\{\{ hashFiles\('\*\*/package-lock.json'\) \}\}\`\) and provide a restore-keys fallback for partial matches.
Journey Context:
Developer notices that their Node.js CI job takes 4 minutes to \`npm ci\` on every run, even when \`package.json\` hasn't changed. They check the workflow and see \`uses: actions/cache@v4\` with \`key: npm-cache\`. The logs show 'Cache restored successfully' with the key 'npm-cache'. The developer realizes the key is static and never changes, so the cache is saved once on the first run and never updated. They try adding \`$\{\{ github.run\_id \}\}\` to the key, but now it never restores because each run has a unique key. Finally, they use \`$\{\{ hashFiles\('\*\*/package-lock.json'\) \}\}\` in the key and add \`restore-keys: npm-\` as a fallback. Now, when \`package-lock.json\` changes, the key changes, forcing a new cache save. Old caches are still restored via the partial prefix match in restore-keys, providing speedup while ensuring dependency changes invalidate the cache. The fix works because the hash function creates a content-addressable key that invalidates exactly when the lockfile changes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T05:46:49.385854+00:00— report_created — created