From 8396203b3315b21c3aed6b265e6f6ce7307342b2 Mon Sep 17 00:00:00 2001 From: Waqar Ali Date: Mon, 28 Sep 2026 19:21:35 +0200 Subject: [PATCH] fix(setup-node-with-cache): compute the dependency cache hash once, before restore The debug and log steps recomputed hashFiles('**/package.json', ...) after the cache restore, when the glob also matches every package.json inside the restored node_modules. On frontend-packages each call took ~4s (two per step, since both the yarn and pnpm lines are evaluated), ~16s per job, and printed a different key than the one the cache was restored with. Hash once in a dep-hash step before the restore and reuse it for the cache keys and both log lines. Keys are unchanged. Co-Authored-By: Claude Opus 5.5 --- .../setup-node-with-cache/action.yml | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/shared-actions/setup-node-with-cache/action.yml b/shared-actions/setup-node-with-cache/action.yml index 408a32c..a8fd8e1 100644 --- a/shared-actions/setup-node-with-cache/action.yml +++ b/shared-actions/setup-node-with-cache/action.yml @@ -181,13 +181,26 @@ runs: echo "key-suffix=-$CACHE_MODE" >> $GITHUB_OUTPUT + # Hash once, before the cache restore. After node_modules is restored, '**/package.json' + # also matches every package.json inside it: re-hashing then takes seconds per call and + # yields a different key than the one the cache was restored with. + - name: Compute dependency cache hash + id: dep-hash + shell: bash + env: + YARN_HASH: ${{ hashFiles('**/yarn.lock', '**/package.json') }} + PNPM_HASH: ${{ hashFiles('**/pnpm-lock.yaml', '**/package.json') }} + run: | + echo "yarn=$YARN_HASH" >> $GITHUB_OUTPUT + echo "pnpm=$PNPM_HASH" >> $GITHUB_OUTPUT + - name: Get yarn cache if: ${{ !env.ACT && inputs.package-manager != 'pnpm' }} uses: actions/cache@v6 id: yarn-cache with: path: ${{ steps.cache-paths.outputs.paths }} - key: ${{ runner.os }}-${{ runner.arch }}-yarn-${{ hashFiles('**/yarn.lock', '**/package.json') }}${{ steps.cache-paths.outputs.key-suffix }} + key: ${{ runner.os }}-${{ runner.arch }}-yarn-${{ steps.dep-hash.outputs.yarn }}${{ steps.cache-paths.outputs.key-suffix }} restore-keys: ${{ inputs.disable-restore-keys != 'true' && format('{0}-{1}-yarn-', runner.os, runner.arch) || '' }} - name: Get pnpm cache @@ -196,7 +209,7 @@ runs: id: pnpm-cache with: path: ${{ steps.cache-paths-pnpm.outputs.paths }} - key: ${{ runner.os }}-${{ runner.arch }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml', '**/package.json') }}${{ steps.cache-paths-pnpm.outputs.key-suffix }} + key: ${{ runner.os }}-${{ runner.arch }}-pnpm-${{ steps.dep-hash.outputs.pnpm }}${{ steps.cache-paths-pnpm.outputs.key-suffix }} restore-keys: ${{ inputs.disable-restore-keys != 'true' && format('{0}-{1}-pnpm-', runner.os, runner.arch) || '' }} - name: Debug cache contents @@ -210,9 +223,9 @@ runs: echo "=== 🔍 Cache Debug Info ===" if [ "$PM" == "pnpm" ]; then - echo "Cache key: ${{ runner.os }}-${{ runner.arch }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml', '**/package.json') }}" + echo "Cache key: ${{ runner.os }}-${{ runner.arch }}-pnpm-${{ steps.dep-hash.outputs.pnpm }}" else - echo "Cache key: ${{ runner.os }}-${{ runner.arch }}-yarn-${{ hashFiles('**/yarn.lock', '**/package.json') }}" + echo "Cache key: ${{ runner.os }}-${{ runner.arch }}-yarn-${{ steps.dep-hash.outputs.yarn }}" fi echo "" @@ -414,11 +427,11 @@ runs: if [ "$PM" == "pnpm" ]; then CACHE_HIT="${{ steps.pnpm-cache.outputs.cache-hit }}" - CACHE_KEY="${{ runner.os }}-${{ runner.arch }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml', '**/package.json') }}" + CACHE_KEY="${{ runner.os }}-${{ runner.arch }}-pnpm-${{ steps.dep-hash.outputs.pnpm }}" INSTALL_CMD="pnpm install" else CACHE_HIT="${{ steps.yarn-cache.outputs.cache-hit }}" - CACHE_KEY="${{ runner.os }}-${{ runner.arch }}-yarn-${{ hashFiles('**/yarn.lock', '**/package.json') }}" + CACHE_KEY="${{ runner.os }}-${{ runner.arch }}-yarn-${{ steps.dep-hash.outputs.yarn }}" INSTALL_CMD="yarn install" fi