fix: skip saving missing cache directories

This commit is contained in:
Zoltan Kochan
2026-11-03 11:47:50 +01:00
parent 7cf8c4cab0
commit eedd901dbd
4 changed files with 173 additions and 113 deletions
+111 -111
View File
File diff suppressed because one or more lines are too long
+3 -2
View File
@@ -2,9 +2,10 @@
"private": true, "private": true,
"scripts": { "scripts": {
"build:bundle": "esbuild src/index.ts --bundle --platform=node --target=node24 --format=cjs --minify --outfile=dist/index.js --loader:.json=json", "build:bundle": "esbuild src/index.ts --bundle --platform=node --target=node24 --format=cjs --minify --outfile=dist/index.js --loader:.json=json",
"build": "pnpm run build:bundle", "build": "pnpm test && pnpm run build:bundle",
"start": "pnpm run build && sh ./run.sh", "start": "pnpm run build && sh ./run.sh",
"update-bootstrap": "node scripts/update-bootstrap.mjs" "update-bootstrap": "node scripts/update-bootstrap.mjs",
"test": "node --experimental-test-module-mocks --test test/*.test.mjs"
}, },
"dependencies": { "dependencies": {
"@actions/cache": "^4.1.0", "@actions/cache": "^4.1.0",
+12
View File
@@ -1,5 +1,6 @@
import { saveCache } from '@actions/cache' import { saveCache } from '@actions/cache'
import { getState, info } from '@actions/core' import { getState, info } from '@actions/core'
import { access } from 'fs/promises'
export async function runSaveCache() { export async function runSaveCache() {
const state = getState('cache_restored_key') const state = getState('cache_restored_key')
@@ -11,6 +12,17 @@ export async function runSaveCache() {
return return
} }
try {
await access(cachePath)
} catch (error) {
if (!(error instanceof Error)) throw error
if (!('code' in error)) throw error
if (error.code !== 'ENOENT') throw error
info(`Cache path ${cachePath} does not exist, not saving cache.`)
return
}
const cacheId = await saveCache([cachePath], primaryKey) const cacheId = await saveCache([cachePath], primaryKey)
if (cacheId == -1) return if (cacheId == -1) return
+47
View File
@@ -0,0 +1,47 @@
import assert from 'node:assert/strict'
import { mkdtemp, rm, access } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { after, beforeEach, mock, test } from 'node:test'
const directory = await mkdtemp(join(tmpdir(), 'pnpm-cache-save-'))
after(() => rm(directory, { recursive: true, force: true }))
let state
let checkAccess
const saveCache = mock.fn(async () => 1)
mock.module('@actions/cache', { namedExports: { saveCache } })
mock.module('@actions/core', {
namedExports: { getState: name => state[name], info: () => {} },
})
mock.module('fs/promises', { namedExports: { access: path => checkAccess(path) } })
const { runSaveCache } = await import('../src/cache-save/run.ts')
beforeEach(() => {
state = { cache_restored_key: 'old', cache_primary_key: 'new', cache_path: directory }
checkAccess = access
saveCache.mock.resetCalls()
})
test('skips saving a missing directory', async () => {
state.cache_path = join(directory, 'missing')
await runSaveCache()
assert.equal(saveCache.mock.callCount(), 0)
})
test('saves an existing directory with the primary key', async () => {
await runSaveCache()
assert.deepEqual(saveCache.mock.calls[0].arguments, [[directory], 'new'])
})
test('skips saving a primary-key cache hit', async () => {
state.cache_restored_key = 'new'
await runSaveCache()
assert.equal(saveCache.mock.callCount(), 0)
})
test('propagates unexpected filesystem errors', async () => {
const error = Object.assign(new Error('access denied'), { code: 'EACCES' })
checkAccess = async () => { throw error }
await assert.rejects(runSaveCache(), error)
assert.equal(saveCache.mock.callCount(), 0)
})