Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
246 changes: 123 additions & 123 deletions dist/index.js

Large diffs are not rendered by default.

9 changes: 1 addition & 8 deletions src/cache-restore/keys.test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'node:assert/strict'
import test from 'node:test'

import { getCacheKeyPrefix, getRestoreKeys, getSaveCacheKey, isLockfileExactHit } from './keys.ts'
import { getCacheKeyPrefix, getSaveCacheKey, isLockfileExactHit } from './keys.ts'

test('moving runtime selectors produce distinct key prefixes', () => {
const previous = getCacheKeyPrefix('Linux', 'x64', [{ name: 'node', version: 'lts' }])
Expand Down Expand Up @@ -100,13 +100,6 @@ test('a version change in any runtime changes the save key', () => {
assert.notEqual(before, after)
})

test('restore keys try the exact lockfile match before falling back to any store for the runtime', () => {
const keyPrefix = getCacheKeyPrefix('Linux', 'x64', [{ name: 'node', version: '24' }])
const lockfileKeyPrefix = `${keyPrefix}lockfile-hash-`

assert.deepEqual(getRestoreKeys(lockfileKeyPrefix, keyPrefix), [lockfileKeyPrefix, keyPrefix])
})

test('cache-hit is true only when the restored key matches the current lockfile exactly', () => {
const keyPrefix = getCacheKeyPrefix('Linux', 'x64', [{ name: 'node', version: '24' }])
const lockfileKeyPrefix = `${keyPrefix}lockfile-hash-`
Expand Down
4 changes: 0 additions & 4 deletions src/cache-restore/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ export function getSaveCacheKeyPrefix (
return `${lockfileKeyPrefix}${runtimeVersionKey}`
}

export function getRestoreKeys (lockfileKeyPrefix: string, keyPrefix: string): string[] {
return [lockfileKeyPrefix, keyPrefix]
}

export function isLockfileExactHit (restoredKey: string | undefined, lockfileKeyPrefix: string): boolean {
return restoredKey?.startsWith(lockfileKeyPrefix) ?? false
}
Expand Down
18 changes: 14 additions & 4 deletions src/cache-restore/run.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,30 @@ test('verification caching remains enabled when store caching and installation a
assert.equal(cache.restoreCache.mock.callCount(), 0)
})

test('restore asks for the current lockfile before the broader runtime fallback', async () => {
// Each lookup searches every scope, so the lockfile prefix gets a lookup of its
// own: a fallback entry in the pull request's scope must not beat an entry for
// the current lockfile on the default branch.
test('restore looks up the current lockfile on its own before the broader runtime fallback', async () => {
await runRestoreCache(inputs, pnpmBin, runtimes)
assert.deepEqual(cache.restoreCache.mock.calls[0].arguments, [
['/pnpm-store'], lockfileKeyPrefix, [lockfileKeyPrefix, keyPrefix],
assert.deepEqual(cache.restoreCache.mock.calls.map(call => call.arguments), [
[['/pnpm-store'], lockfileKeyPrefix, [lockfileKeyPrefix]],
[['/pnpm-store'], keyPrefix, [keyPrefix]],
])
})

test('an entry for the current lockfile skips the fallback lookup', async () => {
cache.restoreCache.mock.mockImplementation(async () => `${lockfileKeyPrefix}previous-invocation`)
await runRestoreCache(inputs, pnpmBin, runtimes)
assert.equal(cache.restoreCache.mock.callCount(), 1)
})

for (const [label, restoredKey, expectedHit] of [
['same lockfile', `${lockfileKeyPrefix}previous-invocation`, true],
['different lockfile', `${keyPrefix}other-lockfile-previous-invocation`, false],
['cache miss', undefined, false],
]) {
test(`${label}: reports cache-hit and publishes a fresh store`, async () => {
cache.restoreCache.mock.mockImplementation(async () => restoredKey)
cache.restoreCache.mock.mockImplementation(async (_paths, _key, [prefix]) => restoredKey?.startsWith(prefix) ? restoredKey : undefined)
const restored = await runRestoreCache(inputs, pnpmBin, runtimes)
finalizeCache(restored, runtimes)
assert.equal(core.outputs.get('cache-hit'), expectedHit)
Expand Down
10 changes: 7 additions & 3 deletions src/cache-restore/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { restoreVerificationCache } from '../lockfile-verification-cache/index.j
import { lockfileDir } from '../pnpm-install/lockfile.js'
import { fingerprintStore, statStore } from '../store-fingerprint/index.js'
import { removeWindowsExtendedPathPrefix } from '../windows-path/index.js'
import { getCacheKeyPrefix, getRestoreKeys, getSaveCacheKey, getSaveCacheKeyPrefix, isLockfileExactHit } from './keys.js'
import { getCacheKeyPrefix, getSaveCacheKey, getSaveCacheKeyPrefix, isLockfileExactHit } from './keys.js'

export interface RestoredCache {
readonly lockfileKeyPrefix: string
Expand Down Expand Up @@ -67,9 +67,13 @@ async function runRestoreStoreCache (

const keyPrefix = getCacheKeyPrefix(process.env.RUNNER_OS, os.arch(), runtimes)
const lockfileKeyPrefix = `${keyPrefix}${fileHash}-`
const restoreKeys = getRestoreKeys(lockfileKeyPrefix, keyPrefix)

const restoredKey = await restoreCache([cachePath], lockfileKeyPrefix, restoreKeys)
// Two lookups, not one with both prefixes: GitHub tries every key in the
// current scope before the default branch's, so a single lookup would
// prefer any older store in a pull request's scope over an entry for the
// current lockfile on the default branch.
const restoredKey = await restoreCache([cachePath], lockfileKeyPrefix, [lockfileKeyPrefix]) ??
await restoreCache([cachePath], keyPrefix, [keyPrefix])

if (!restoredKey) {
info('Cache is not found')
Expand Down
Loading