{"title":"Cache CI Dependencies Without Hiding Lockfile Drift","excerpt":"A cache keyed on an unchanged lockfile can keep CI green while package.json drifts away from it. Cache the package store instead of node_modules, run npm ci or a frozen pnpm install on every hit, limit writes to trusted runs, and schedule a cache-mode: none job to prove the cold build.","canonicalUrl":"https://automationtester.in/blog/automation-tutorials/ci-cache-dependencies-without-hiding-drift","category":{"name":"Automation Tutorials","slug":"automation-tutorials"},"tags":["github-actions","ci-cd","dependency-caching","npm","pnpm","lockfiles"],"author":{"name":"Shashank Rawlani","url":"https://shashank.rawlani.com","linkedin":"https://www.linkedin.com/in/shashankrawlani/"},"datePublished":"2026-11-16T13:30:00.000Z","dateModified":"2026-11-16T13:30:00.000Z","coverImage":{"url":"https://ik.imagekit.io/automationtester/automationtester-in/blog/covers/v3/ci-cache-dependencies-without-hiding-drift.webp","alt":"Two panels on the left, the upper one with a single orange row among green rows, feed green lines into a dashed vertical gate with a ringed node in the centre. A drawer of stacked green pills above also feeds the gate. An orange dashed path leaves the drawer, bypasses the gate to the right and ends at a crossed-out circle just above the result panel, while a green arrow runs from the gate's node to a result panel on the right."},"contentHtml":"<div class=\"callout callout-info\"><strong>Quick answer:</strong> Cache what the package manager downloads, never what it produces. Key the entry on the OS, architecture, runtime version and a hash of the lockfile, and run the frozen install (<code class=\"language-bash\">npm ci</code>, <code class=\"language-bash\">pnpm install --frozen-lockfile</code>) on every run, hit or miss, so the lockfile is checked even when the cache is warm. Let only trusted <code class=\"language-text\">push</code> runs on the default branch write entries. Then schedule one job with <code class=\"language-yaml\">cache-mode: none</code> so a cold build is proven regularly instead of discovered when an entry is evicted.</div>\n\n<p>A pull request bumps a dependency in <code class=\"language-text\">package.json</code> and forgets to regenerate the lockfile. CI is green. It stays green for a week of merges, because the cache key hashes the lockfile, the lockfile did not change, and the workflow skips the install step whenever <code class=\"language-text\">cache-hit</code> is <code class=\"language-text\">true</code>. Then the entry goes unused for seven days, GitHub evicts it, the next run on <code class=\"language-text\">main</code> installs from scratch, and it fails on a change nobody has touched in a week.</p>\n\n<p>The cache did what it was told. The fix is to make the key describe the right inputs and to stop a warm cache from standing in for the check a cold install performs.</p>\n\n<div class=\"callout callout-warning\"><strong>Versions checked on 2026-11-09.</strong> <code class=\"language-text\">actions/cache</code> v6.1.0, <code class=\"language-text\">actions/setup-node</code> v7.1.0, <code class=\"language-text\">actions/setup-python</code> v7.1.0, <code class=\"language-text\">pnpm/setup</code> v3.1.2, npm 12.2.2, pnpm 12.18.0 and Playwright 1.65.0. The local labs below were run with Node.js 26.12.1, npm 11.20.0 and pnpm 10.32.1. The platform is GitHub Actions throughout.</div>\n\n<h2 id=\"the-key-is-a-claim-about-inputs\">The key is a claim about inputs</h2>\n\n<p>Three properties of the GitHub Actions cache decide everything else, and each is easy to get wrong from memory.</p>\n\n<p><strong>Entries are immutable.</strong> The dependency caching reference says it plainly: \"You cannot change the contents of an existing cache. Instead, you can create a new cache with a new key.\" The <code class=\"language-text\">actions/cache</code> README adds the consequence: if the key matches an existing entry, \"a new cache is not created\". Whatever was saved first under a key is what every later run restores, until eviction.</p>\n\n<p><strong>Restore is a search, and it widens.</strong> The action looks for an exact match on <code class=\"language-text\">key</code>, then for prefix matches of <code class=\"language-text\">key</code>, then walks <code class=\"language-text\">restore-keys</code> in order. Among several prefix matches it returns \"the most recently created cache\". If nothing matches on the current branch, it repeats the whole search on the default branch. A run on a feature branch can therefore restore an entry built from a different lockfile on <code class=\"language-text\">main</code>.</p>\n\n<p><strong><code class=\"language-text\">cache-hit</code> means exact match only.</strong> The restore action documents that <code class=\"language-text\">cache-hit</code> \"will be set to <code class=\"language-text\">true</code> only when cache hit occurs for the exact <code class=\"language-text\">key</code> match. For a partial key match via <code class=\"language-text\">restore-keys</code> or a cache miss, it will be set to <code class=\"language-text\">false</code>.\" The <code class=\"language-text\">cache-matched-key</code> output tells you which entry you actually got.</p>\n\n<p>There is also a hidden fourth component. Each entry carries a <em>version</em>, a hash of the cached <code class=\"language-text\">path</code> list and the compression tool. Two jobs using the same key with different paths, or the same key on Linux and Windows, produce separate entries and never match each other.</p>\n\n<p>So a key claims that everything shaping the cached directory is encoded in one string. Any input you leave out can change underneath an exact hit.</p>\n\n<h2 id=\"store-versus-node-modules\">Cache the download, not the install</h2>\n\n<p>The common shape that hides drift caches the result of an install and then trusts it:</p>\n\n<pre><code class=\"language-yaml\"># Hides drift: caches the install output and skips the check on a hit\n- uses: actions/cache@v6\n  id: deps\n  with:\n    path: node_modules\n    key: deps-${{ runner.os }}-${{ hashFiles('package-lock.json') }}\n    restore-keys: deps-${{ runner.os }}-\n- if: steps.deps.outputs.cache-hit != 'true'\n  run: npm install\n</code></pre>\n\n<p>Four separate problems sit in those eight lines. <code class=\"language-text\">node_modules</code> is a product of the lockfile, the Node.js version (native addons compile against it) and the install flags, and only one of those is in the key. The prefix restore key will hand a run a <code class=\"language-text\">node_modules</code> built from some other lockfile. <code class=\"language-bash\">npm install</code> is allowed to rewrite the lockfile, so a miss can quietly \"repair\" the very file under test. And on an exact hit nothing at all verifies that <code class=\"language-text\">package.json</code> and the lockfile agree.</p>\n\n<p>The first-party setup actions avoid most of this by design. <code class=\"language-text\">actions/setup-node</code> states that it \"does not cache <code class=\"language-text\">node_modules</code>\"; it caches the package manager's global store, the directory <code class=\"language-bash\">pnpm store path --silent</code> reports for pnpm. A store is a download cache. A stale or oversized store makes an install slower; it cannot make an install succeed that should fail, because the frozen install still resolves from the lockfile. The safer version keeps the cache and moves the trust:</p>\n\n<pre><code class=\"language-yaml\"># Caches downloads; the frozen install runs on every run\n- uses: actions/checkout@v6\n- uses: actions/setup-node@v7\n  with:\n    node-version-file: .nvmrc\n    cache: npm\n- run: npm ci\n</code></pre>\n\n<p><code class=\"language-bash\">npm ci</code> carries the guarantees that matter here: the project \"must have an existing <code class=\"language-text\">package-lock.json</code>\", it exits with an error if the lock and <code class=\"language-text\">package.json</code> disagree \"instead of updating the package lock\", it removes any existing <code class=\"language-text\">node_modules</code> first, and it \"will never write to <code class=\"language-text\">package.json</code> or <code class=\"language-text\">package-lock.json</code>\". For pnpm, the CI docs say that when pnpm \"detects that it is running in CI, it switches to frozen-lockfile mode automatically\", and that a lockfile written by a newer pnpm major makes the install error out rather than rewriting it. With <code class=\"language-text\">pnpm/setup</code>, which installs pnpm and a runtime in one step, the equivalent is:</p>\n\n<pre><code class=\"language-yaml\">- uses: actions/checkout@v6\n- uses: pnpm/setup@fa36a5ef7dcdefc3a7cb25655c122e00cf6e636e # v3.1.2\n  with:\n    runtime: node@24\n    cache: true\n    save-cache: ${{ github.ref == 'refs/heads/main' }}\n    require-lockfile: true\n</code></pre>\n\n<p><code class=\"language-text\">require-lockfile: true</code> makes the action's install run <code class=\"language-bash\">pnpm install --frozen-lockfile</code>, and restricting <code class=\"language-text\">save-cache</code> to <code class=\"language-text\">main</code> follows the README's pattern for jobs that \"should not write to the cache, such as pull requests\". The pnpm docs are candid about the payoff: \"it is not guaranteed that caching the store will make installation faster.\" Measure it rather than assume it.</p>\n\n<h2 id=\"what-each-setup-action-puts-in-the-key\">What each setup action actually puts in the key</h2>\n\n<p>The built-in caches are convenient, and their keys are not identical. Read from the source at the tags above:</p>\n\n<table>\n<thead><tr><th>Action</th><th>Primary key built from</th><th>Prefix fallback</th><th><code class=\"language-text\">cache-hit</code> on fallback</th></tr></thead>\n<tbody>\n<tr><td><code class=\"language-text\">setup-node</code> v7.1.0</td><td><code class=\"language-text\">node-cache-${RUNNER_OS}-${arch}-${packageManager}-${hash(lockfile)}</code></td><td>None, except Yarn Berry projects whose dependencies are managed locally, which fall back to the hashless prefix</td><td><code class=\"language-text\">true</code> for that Yarn Berry fallback, because the output is set from whether any entry was restored</td></tr>\n<tr><td><code class=\"language-text\">setup-python</code> v7.1.0 (pip)</td><td><code class=\"language-text\">setup-python-${RUNNER_OS}-${arch}-${osVersion}-${osName}-python-${version}-pip-${hash}</code> (the OS segment is Linux only)</td><td>The same string without the hash</td><td><code class=\"language-text\">false</code></td></tr>\n</tbody>\n</table>\n\n<p>The setup-node key omits the Node.js version, which is fine for a download store and wrong for a hand-rolled <code class=\"language-text\">node_modules</code> cache copying its shape. setup-python's pip cache hashes <code class=\"language-text\">requirements.txt</code> by default, which is not a lockfile. Its README warns that with unpinned or range specifiers, <code class=\"language-bash\">pip install -r requirements.txt</code> \"will always try to install the latest available package version\", so a perfect cache hit tells you nothing about which versions were installed. Pin the file, or hash a real lock (<code class=\"language-text\">poetry.lock</code>, <code class=\"language-text\">Pipfile.lock</code>) through <code class=\"language-text\">cache-dependency-path</code>.</p>\n\n<p>For npm projects, setup-node v6 and later enable caching automatically when <code class=\"language-text\">package.json</code> declares npm in <code class=\"language-text\">packageManager</code> or <code class=\"language-text\">devEngines.packageManager</code>, even with no <code class=\"language-text\">cache</code> input. The README recommends <code class=\"language-text\">package-manager-cache: false</code> for \"workflows with elevated privileges or access to sensitive information\". Audit release and deploy workflows for this: a cache you did not ask for is still a cache someone else's run may have written.</p>\n\n<h2 id=\"watch-a-warm-cache-hide-a-broken-lockfile\">Watch a warm cache hide a broken lockfile</h2>\n\n<p>The failure in the opening paragraph reproduces locally in a few seconds, with no registry and no network. Two versions of a package are packed as tarballs and referenced with <code class=\"language-text\">file:</code> specifiers, and a directory keyed on the lockfile's SHA-256 stands in for <code class=\"language-text\">actions/cache</code>:</p>\n\n<pre><code class=\"language-bash\">#!/usr/bin/env bash\n# Local only: file: tarball dependencies, offline npm, no listener.\nset -u\nLAB=$(mktemp -d); cd \"$LAB\"\nexport npm_config_offline=true npm_config_audit=false npm_config_fund=false\nfor v in 1.0.0 2.0.0; do\n  mkdir -p \"src/lib-$v\"\n  printf '{\"name\":\"lib\",\"version\":\"%s\",\"main\":\"index.js\"}\\n' \"$v\" &gt; \"src/lib-$v/package.json\"\n  printf 'module.exports = { version: \"%s\" };\\n' \"$v\" &gt; \"src/lib-$v/index.js\"\n  (cd \"src/lib-$v\" &amp;&amp; npm pack --silent --pack-destination ../.. &gt;/dev/null)\ndone\nmkdir app cache; cd app\nprintf '{\"name\":\"app\",\"version\":\"1.0.0\",\"dependencies\":{\"lib\":\"file:../lib-1.0.0.tgz\"}}\\n' &gt; package.json\nnpm install --silent\nprintf 'console.log(\"lib resolved to\", require(\"lib\").version)\\n' &gt; test.js\n\nci_job() {   # $1 = skip-on-hit | always-install\n  local key=\"deps-$(sha256sum package-lock.json | cut -c1-12)\" hit=false\n  rm -rf node_modules\n  [ -d \"../cache/$key\" ] &amp;&amp; { cp -R \"../cache/$key\" node_modules; hit=true; }\n  echo \"[$1] cache entry $key, cache-hit=$hit\"\n  if [ \"$1\" = always-install ] || [ \"$hit\" != true ]; then\n    npm ci --loglevel=error &gt; ../ci.log 2&gt;&amp;1 \\\n      || { grep -m1 \"in sync\" ../ci.log; echo \"[$1] install FAILED\"; return 1; }\n  else\n    echo \"[$1] install skipped\"\n  fi\n  node test.js\n  [ \"$hit\" = true ] || cp -R node_modules \"../cache/$key\"\n}\n\nci_job skip-on-hit                              # run 1: cold, saves the entry\nsed -i.bak 's/lib-1.0.0/lib-2.0.0/' package.json # bump without relocking\nci_job skip-on-hit                              # run 2: the pattern that hides it\nci_job always-install                           # run 2, frozen install every time\n</code></pre>\n\n<p>The output:</p>\n\n<pre><code class=\"language-text\">[skip-on-hit] cache entry deps-20a3196ae69f, cache-hit=false\nlib resolved to 1.0.0\n[skip-on-hit] cache entry deps-20a3196ae69f, cache-hit=true\n[skip-on-hit] install skipped\nlib resolved to 1.0.0\n[always-install] cache entry deps-20a3196ae69f, cache-hit=true\nnpm error `npm ci` can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with `npm install` before continuing.\n[always-install] install FAILED\n</code></pre>\n\n<p>The key did not change, the hit was exact, and the skip-on-hit job ran the tests against version 1.0.0 while <code class=\"language-text\">package.json</code> asked for 2.0.0. The same restored entry, followed by an unconditional <code class=\"language-bash\">npm ci</code>, fails on the first run after the bad commit. The equivalent pnpm check, run with <code class=\"language-text\">CI=true</code> against the same mismatch, stops with <code class=\"language-text\">ERR_PNPM_OUTDATED_LOCKFILE  Cannot install with \"frozen-lockfile\" because pnpm-lock.yaml is not up to date with &lt;ROOT&gt;/package.json</code>.</p>\n\n<p>Hashing the lockfile is correct. Skipping verification because the hash matched is the bug.</p>\n\n<h2 id=\"restore-keys-and-the-state-you-did-not-choose\">Restore keys and the state you did not choose</h2>\n\n<p>A prefix restore key trades a cold start for a warm directory of unknown provenance. Over a download store that is usually safe: the frozen install fetches whatever is missing and ignores the rest. The cost is growth. pnpm's store documentation notes that pnpm \"does not automatically remove packages\", so each save under a new key carries forward every package any earlier lockfile needed. <code class=\"language-text\">pnpm/setup</code> runs <code class=\"language-bash\">pnpm store prune</code> before deciding whether to save; a hand-rolled <code class=\"language-text\">actions/cache</code> step does not, so add the prune yourself before the post step saves.</p>\n\n<p>Over anything that is executed or trusted as-is (<code class=\"language-text\">node_modules</code>, a virtualenv, a build output directory, a test-result cache) a prefix restore is a correctness risk, because no later step re-derives the contents. Use exact keys only for those, accept the cold start on a lockfile change, and set <code class=\"language-text\">fail-on-cache-miss: true</code> on <code class=\"language-text\">actions/cache/restore</code> where a downstream job must refuse to build without the exact entry a producer job saved. Its documentation is explicit that this only works if you \"leave <code class=\"language-text\">restore-keys</code> empty\".</p>\n\n<p>Playwright browsers fall in between. The Playwright CI guide does not recommend caching them, because restoring takes about as long as downloading, and the Linux system dependencies \"are not cacheable\" anyway. If you cache them regardless, the guide says to key \"against a hash of the Playwright version\", which means the version resolved from the lockfile, not the range in <code class=\"language-text\">package.json</code>, with no prefix fallback:</p>\n\n<pre><code class=\"language-yaml\">- run: npm ci\n- id: pw\n  run: echo \"version=$(node -p \"require('@playwright/test/package.json').version\")\" &gt;&gt; \"$GITHUB_OUTPUT\"\n- uses: actions/cache@v6\n  with:\n    path: ~/.cache/ms-playwright\n    key: playwright-${{ runner.os }}-${{ runner.arch }}-${{ steps.pw.outputs.version }}\n- run: npx playwright install --with-deps chromium\n</code></pre>\n\n<p>The install step stays unconditional, because it is also what installs the system packages the cache cannot carry.</p>\n\n<h2 id=\"who-may-write-the-entry-you-restore\">Who may write the entry you restore</h2>\n\n<p>The reference is direct about integrity: \"Cache contents are not signed or verified, and any workflow run that can read a cache may extract its contents.\" An entry restored into a privileged job is code that job will run.</p>\n\n<p>Branch scope does part of the work: a run restores from its own branch, the default branch and, for pull requests, the base branch, never from siblings or children. The gap is events that resolve to the default branch but can be triggered by people without write access. GitHub limits writes to the default branch's scope to <code class=\"language-text\">push</code>, <code class=\"language-text\">workflow_dispatch</code>, <code class=\"language-text\">repository_dispatch</code>, <code class=\"language-text\">delete</code>, <code class=\"language-text\">registry_package</code>, <code class=\"language-text\">page_build</code> and <code class=\"language-text\">schedule</code>. Runs from <code class=\"language-text\">pull_request_target</code>, <code class=\"language-text\">issue_comment</code> or <code class=\"language-text\">workflow_run</code> get read-only access by default, and a blocked save is a warning, not a failure.</p>\n\n<p>The <code class=\"language-yaml\">cache-mode</code> workflow key makes the intent explicit per job: <code class=\"language-text\">read</code>, <code class=\"language-text\">write</code>, <code class=\"language-text\">write-only</code> or <code class=\"language-text\">none</code>, enforced with scoped cache tokens. Declaring <code class=\"language-text\">write</code> on a low-trust trigger overrides the read-only default, and the docs warn that this \"reintroduces the risk of cache-poisoning\". Treat a <code class=\"language-text\">cache-mode: write</code> next to <code class=\"language-text\">pull_request_target</code> as a review finding: only trusted runs should write entries that privileged runs restore.</p>\n\n<h2 id=\"a-scheduled-cold-build\">Prove the cold path on a schedule</h2>\n\n<p>Every warm run proves less than it appears to. The direct defence is a job that cannot touch the cache at all:</p>\n\n<pre><code class=\"language-yaml\">name: cold-install\non:\n  schedule:\n    - cron: \"17 3 * * 1-5\"\n  workflow_dispatch:\n\ncache-mode: none\n\njobs:\n  cold:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v6\n      - uses: actions/setup-node@v7\n        with:\n          node-version-file: .nvmrc\n      - run: npm ci\n      - run: npm test\n</code></pre>\n\n<p>With <code class=\"language-text\">none</code>, restore is skipped and \"treated as a cache miss\", and save is skipped, so the run neither benefits from nor contaminates the shared entries. It also exercises the path a warm store skips: fetching every package from the registry with the credentials, proxy and mirror settings the job really has. If a cold run fails while warm runs pass, compare the two runs' environments the way <a href=\"/blog/automation-tutorials/debug-ci-only-failures-environment-evidence\">environment evidence for CI-only failures</a> describes; the cache is one more difference between them. Where this job sits relative to pull-request checks is a stage decision, of the kind covered in <a href=\"/blog/automation-tutorials/ci-test-pyramid-feedback-cost\">choosing tests per CI stage by feedback cost</a>.</p>\n\n<h2 id=\"count-exact-partial-and-miss-separately\">Count exact, partial and miss separately</h2>\n\n<p>\"Cache hit rate\" as one number blends two different events. A partial restore is a cold-ish install with extra download, and should be counted on its own. The restore outputs carry enough to classify every run:</p>\n\n<pre><code class=\"language-yaml\">- uses: actions/cache/restore@v6\n  id: store\n  with:\n    path: ~/.npm\n    key: npm-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('package-lock.json') }}\n    restore-keys: npm-${{ runner.os }}-${{ runner.arch }}-\n- name: Record cache outcome\n  env:\n    HIT: ${{ steps.store.outputs.cache-hit }}\n    MATCHED: ${{ steps.store.outputs.cache-matched-key }}\n  run: |\n    if [ \"$HIT\" = \"true\" ]; then outcome=exact\n    elif [ -n \"$MATCHED\" ]; then outcome=partial\n    else outcome=miss; fi\n    echo \"cache_outcome=$outcome matched=${MATCHED:-none}\" &gt;&gt; \"$GITHUB_STEP_SUMMARY\"\n- run: npm ci\n- uses: actions/cache/save@v6\n  if: github.ref == 'refs/heads/main' &amp;&amp; steps.store.outputs.cache-hit != 'true'\n  with:\n    path: ~/.npm\n    key: ${{ steps.store.outputs.cache-primary-key }}\n</code></pre>\n\n<p>Pair the outcome with the install step's duration and you have the number that justifies the cache: minutes saved per exact hit against minutes spent restoring on a miss. If exact hits are rare while the lockfile is stable, check storage pressure. Entries unused for seven days are evicted, and past the repository limit (10 GB by default) the least recently accessed go first. Pull request caches can crowd out the default branch's entries; the cache management docs include a workflow that runs <code class=\"language-bash\">gh cache delete</code> for a branch when its pull request closes.</p>\n\n<h2 id=\"symptoms-that-trace-back-to-the-cache\">Symptoms that trace back to the cache</h2>\n\n<h3 id=\"key-that-never-changes\">A key that never changes</h3>\n\n<p><code class=\"language-text\">hashFiles</code> \"returns an empty string\" when its pattern matches nothing. Copy an npm example into a pnpm repository and <code class=\"language-text\">hashFiles('package-lock.json')</code> collapses the key to a constant like <code class=\"language-text\">npm-Linux-</code>. Because entries are immutable, the first save under that key is restored forever and never updated. Look for a key ending in a separator in the restore log. The setup actions guard against this: setup-node throws when it cannot find a lockfile, and setup-python refuses a primary key ending in <code class=\"language-text\">-</code>. Hand-written keys get no such check.</p>\n\n<h3 id=\"key-listed-but-never-restored\">An entry is listed but never restored</h3>\n\n<p>The key matches by eye and the step still reports a miss. That is the version stamp: a different <code class=\"language-text\">path</code> list between saving and restoring jobs, or a different OS. The README points to the list-caches REST endpoint, which returns each entry's version, so you can compare it against what the restoring job would compute.</p>\n\n<h2 id=\"apply-cache-checks-now\">Apply this now</h2>\n\n<p>Open every workflow and search for <code class=\"language-text\">cache-hit</code>. Any <code class=\"language-text\">if:</code> that skips an install, lockfile check or build step on a hit is the pattern that hid the drift in the lab; remove the condition and keep the cache. Then search for <code class=\"language-text\">path: node_modules</code>, <code class=\"language-text\">.venv</code> or build output directories paired with <code class=\"language-text\">restore-keys</code>, and either cache the package manager's store instead or drop the fallback. Add the scheduled <code class=\"language-yaml\">cache-mode: none</code> job, and add the outcome step to the main CI job.</p>\n\n<p>Capture as evidence: the first cold run's install duration and result, a week of <code class=\"language-text\">cache_outcome</code> lines from the job summaries, and the repository's current cache usage from the Actions caches page.</p>\n\n<h2 id=\"ci-cache-faq\">FAQ</h2>\n\n<h3 id=\"faq-node-modules\">Is caching node_modules ever acceptable?</h3>\n<p>Only with an exact key that includes every input that shapes it (lockfile hash, Node.js version, OS, architecture, install flags), no restore keys, and the frozen install still running afterwards. Even then <code class=\"language-bash\">npm ci</code> deletes an existing <code class=\"language-text\">node_modules</code> before it starts, so the restore buys nothing with npm.</p>\n\n<h3 id=\"faq-pr-caches-main\">Do caches saved by pull requests help later runs on main?</h3>\n<p>No. A <code class=\"language-text\">pull_request</code> run saves under the merge ref, and only re-runs of that pull request can restore it. Pull requests benefit from entries saved on the default branch, so make sure a <code class=\"language-text\">push</code> to the default branch saves them.</p>\n\n<h3 id=\"faq-runtime-in-key\">Should the runtime version be in the key?</h3>\n<p>For anything compiled or linked against the runtime, yes. setup-python already includes the Python version; setup-node does not include the Node.js version, which is acceptable for its download store. In a matrix across runtime versions, hand-rolled keys need <code class=\"language-text\">${{ matrix.node }}</code> or equivalent, or matrix legs will restore each other's entries.</p>\n\n<h3 id=\"faq-force-refresh\">How do you force a fresh entry without changing dependencies?</h3>\n<p>Delete the entry with <code class=\"language-bash\">gh cache delete</code> or from the Actions caches page, or add a manual version segment such as <code class=\"language-text\">v2-</code> to the key prefix. Because entries are immutable, re-running a job will never refresh an existing key.</p>\n\n<h2 id=\"ci-cache-references\">Primary references</h2>\n<ul>\n<li><a href=\"https://docs.github.com/en/actions/reference/workflows-and-actions/dependency-caching\">GitHub Docs: Dependency caching reference</a>. Restore search order including default-branch fallback, immutability of entries, most-recent-created selection among prefix matches, branch and merge-ref scope, the trusted-trigger list for default-branch writes, <code class=\"language-text\">cache-mode</code> values and defaults, unsigned cache contents, and the 7-day and 10 GB eviction rules.</li>\n<li><a href=\"https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-syntax\">GitHub Docs: Workflow syntax, the cache-mode key</a>. Workflow-level and job-level <code class=\"language-text\">cache-mode</code>, enforcement through scoped cache tokens, and the warning about write modes on low-trust triggers.</li>\n<li><a href=\"https://docs.github.com/en/actions/reference/workflows-and-actions/expressions\">GitHub Docs: Expressions, hashFiles</a>. That <code class=\"language-text\">hashFiles</code> combines per-file SHA-256 hashes and returns an empty string when no file matches.</li>\n<li><a href=\"https://docs.github.com/en/actions/how-tos/manage-workflow-runs/manage-caches\">GitHub Docs: Managing caches</a>. Deleting entries from the web UI or <code class=\"language-bash\">gh cache</code>, and the example workflow that clears a branch's caches when its pull request closes.</li>\n<li><a href=\"https://github.com/actions/cache/blob/v6.1.0/README.md\">actions/cache v6.1.0 README</a>. The <code class=\"language-text\">cache-hit</code> output semantics, no new entry on a matching key, cache version from path and compression, and read-only saves reported as warnings.</li>\n<li><a href=\"https://github.com/actions/cache/blob/v6.1.0/restore/README.md\">actions/cache v6.1.0 restore action README</a>. The <code class=\"language-text\">cache-primary-key</code> and <code class=\"language-text\">cache-matched-key</code> outputs, and <code class=\"language-text\">fail-on-cache-miss</code> requiring empty <code class=\"language-text\">restore-keys</code>.</li>\n<li><a href=\"https://github.com/actions/setup-node/blob/v7.1.0/src/cache-restore.ts\">actions/setup-node v7.1.0 cache-restore source</a>. The primary key format, the absence of restore keys outside Yarn Berry, and the error when no lockfile is found; the README at the same tag states that <code class=\"language-text\">node_modules</code> is not cached and documents <code class=\"language-text\">package-manager-cache</code>.</li>\n<li><a href=\"https://github.com/actions/setup-python/blob/v7.1.0/src/cache-distributions/pip-cache.ts\">actions/setup-python v7.1.0 pip cache source</a>. The pip key including OS, architecture, Linux distribution and Python version, and its hashless restore prefix; the README at the same tag warns about unpinned requirements.</li>\n<li><a href=\"https://github.com/pnpm/setup/blob/v3.1.2/README.md\">pnpm/setup v3.1.2 README</a>. The <code class=\"language-text\">cache</code>, <code class=\"language-text\">save-cache</code> and <code class=\"language-text\">require-lockfile</code> inputs, exact-match <code class=\"language-text\">cache-hit</code>, and the store prune before saving.</li>\n<li><a href=\"https://docs.npmjs.com/cli/v12/commands/npm-ci\">npm Docs: npm ci</a>. The lockfile requirement, failure on a mismatch, removal of an existing <code class=\"language-text\">node_modules</code>, and never writing either manifest.</li>\n<li><a href=\"https://pnpm.io/continuous-integration\">pnpm: Continuous Integration</a>. Automatic frozen-lockfile mode in CI, failure on a newer-major lockfile, the caveat that store caching is not guaranteed to be faster, and treating store and cache directories as trusted.</li>\n<li><a href=\"https://pnpm.io/cli/store\">pnpm: pnpm store</a>. That pnpm never removes packages from the store on its own, and what <code class=\"language-bash\">pnpm store prune</code> removes.</li>\n<li><a href=\"https://playwright.dev/docs/ci#caching-browsers\">Playwright: Continuous Integration, caching browsers</a>. Why browser caching is not recommended, that Linux system dependencies are not cacheable, and keying on the Playwright version if you cache anyway.</li>\n</ul>\n"}