Report #53421
[bug\_fix] Build fails with 'Module not found' or uses outdated dependencies despite cache restore being successful
Change the cache key from a static string \(e.g., \`linux-node-14\`\) to a dynamic hash of dependency lockfiles using \`$\{\{ hashFiles\('\*\*/package-lock.json'\) \}\}\` \(or \`Pipfile.lock\`, \`yarn.lock\`, etc.\). Use \`restore-keys\` with a prefix \(e.g., \`linux-node-14-\`\) to allow partial fallback to older caches while ensuring exact matches trigger a new cache save when dependencies change.
Journey Context:
A developer notices that their Node.js CI install step takes 5 minutes on every run. They add \`actions/cache\` with a static key \`npm-cache\`. The first run saves the cache; subsequent runs restore it instantly and the build passes. The developer then adds a new dependency to \`package.json\` and pushes. The workflow restores the cache \(cache hit on \`npm-cache\`\), runs \`npm ci\`, and fails with 'Cannot find module new-dependency'. The developer realizes the cache key never changed, so the old \`node\_modules\` \(without the new package\) was restored. They update the key to \`$\{\{ runner.os \}\}-npm-$\{\{ hashFiles\('\*\*/package-lock.json'\) \}\}\` and add \`restore-keys: $\{\{ runner.os \}\}-npm-\`. Now, when the lockfile changes, the key changes, forcing a fresh save; on subsequent runs with the same lockfile, it hits the exact cache; if partially changed, restore-keys provides a fallback base cache for speed.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T20:09:45.159346+00:00— report_created — created