diff --git a/README.md b/README.md index f25a870..29b1c2f 100644 --- a/README.md +++ b/README.md @@ -272,6 +272,10 @@ The `cache-key` output is shared by both modes; the operational role never enter `cache-inputs` accepts explicit files, not globs. Include patches, hooks, certificates, local dependency sources, and other inputs that affect installation. Inherited repository manifests/config are also hashed. Additional paths may reference ancestors within the repository; paths outside the repository are rejected. Installation environment, secrets, and policies that are not files must be represented by a caller policy suffix. Populate and restore must use the same inputs, source commit, runner image family, and checkout layout. +Completed archives enumerate only workspace `node_modules` roots, excluding nested matches inside dependencies before tar runs. This preserves pnpm package symlinks without archiving their contents through multiple aliases. Each root is still archived recursively, including nested dependencies and bin files. + +The compatibility marker also records directory-link targets inside the working directory. GNU tar on Windows can recreate a directory symlink as a file symlink when its target appears later in the archive, leaving correct link text but `EPERM` on traversal. After key/ABI validation, the restore codec reinstates the directory type only for recorded links whose existing source/target still match. Missing links, changed targets and paths outside the working directory fail; dependency installation and graph inference remain forbidden. + Completed archives contain workspace `node_modules` trees, their pnpm virtual stores and links, and a compatibility marker. They exclude the pnpm download store. Restores verify the recorded Node major and actual `NODE_MODULE_VERSION` from the selected Node executable; missing or incompatible metadata fails without repair. Different Node majors yield distinct keys; even a same-major ABI mismatch fails. Failed installs, changed lockfiles/inputs, and incompatible trees are not saved. Corepack may prepare/cache the pinned package-manager executable independently of project dependencies. Use portable `packageImportMethod: auto`. Relative pnpm links and allowed native tooling are exercised on Linux, macOS, and Windows. Absolute links, linked packages outside the checkout, platform-specific native artifacts, and different filesystem layouts may require additional policy namespacing or a fresh population. These CI settings do not change local development's shared store. diff --git a/fixtures/cache-archive-probe.mjs b/fixtures/cache-archive-probe.mjs new file mode 100644 index 0000000..9aac02a --- /dev/null +++ b/fixtures/cache-archive-probe.mjs @@ -0,0 +1,36 @@ +import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import path from 'node:path'; + +const contents = 'completed-tree archive probe\n'; + +// A package with its own node_modules is reachable through both the virtual +// store and workspace aliases. actions/cache must not pass all those nested +// aliases to tar as independent archive roots. +export function seedArchiveProbe(root = process.cwd()) { + const target = path.join(root, 'node_modules', '.archive-probe', 'package'); + const bin = path.join(target, 'node_modules', '.bin'); + fs.mkdirSync(bin, { recursive: true }); + const file = path.join(bin, 'probe'); + fs.writeFileSync(file, contents); + const hardlink = path.join(bin, 'probe-copy'); + if (!fs.existsSync(hardlink)) fs.linkSync(file, hardlink); + for (const modules of ['node_modules', 'packages/archive-probe/node_modules']) { + const directory = path.join(root, modules); + fs.mkdirSync(directory, { recursive: true }); + const link = path.join(directory, 'archive-probe'); + if (!fs.existsSync(link)) { + fs.symlinkSync(path.relative(directory, target), link, process.platform === 'win32' ? 'junction' : 'dir'); + } + } +} + +export function checkArchiveProbe(root = process.cwd()) { + for (const modules of ['node_modules', 'packages/archive-probe/node_modules']) { + const link = path.join(root, modules, 'archive-probe'); + assert.ok(fs.lstatSync(link).isSymbolicLink(), 'workspace package aliases must remain links'); + for (const file of ['probe', 'probe-copy']) { + assert.equal(fs.readFileSync(path.join(link, 'node_modules', '.bin', file), 'utf8'), contents); + } + } +} diff --git a/fixtures/pnpm-basic/check.mjs b/fixtures/pnpm-basic/check.mjs index 5e0fd88..e2cfab7 100644 --- a/fixtures/pnpm-basic/check.mjs +++ b/fixtures/pnpm-basic/check.mjs @@ -1,3 +1,4 @@ +import { checkArchiveProbe } from '../cache-archive-probe.mjs'; import pc from 'picocolors'; if (typeof pc.green !== 'function') { @@ -5,3 +6,5 @@ if (typeof pc.green !== 'function') { } console.log(pc.green('pnpm fixture dependency loaded')); + +checkArchiveProbe(); diff --git a/fixtures/pnpm-basic/write-electron-cache-markers.mjs b/fixtures/pnpm-basic/write-electron-cache-markers.mjs index a6ab89d..f2a98ee 100644 --- a/fixtures/pnpm-basic/write-electron-cache-markers.mjs +++ b/fixtures/pnpm-basic/write-electron-cache-markers.mjs @@ -1,6 +1,9 @@ +import { seedArchiveProbe } from '../cache-archive-probe.mjs'; import fs from 'node:fs'; import path from 'node:path'; +seedArchiveProbe(); + if (process.env.LIFECYCLE_LOG) fs.appendFileSync(process.env.LIFECYCLE_LOG, 'postinstall\n'); const cacheRoots = [ diff --git a/fixtures/pnpm12-basic/check.mjs b/fixtures/pnpm12-basic/check.mjs index 9c19e49..157cabc 100644 --- a/fixtures/pnpm12-basic/check.mjs +++ b/fixtures/pnpm12-basic/check.mjs @@ -1,6 +1,27 @@ +import { checkArchiveProbe } from '../cache-archive-probe.mjs'; import assert from 'node:assert/strict'; import pc from 'picocolors'; +import fs from 'node:fs'; +import path from 'node:path'; +import { createRequire } from 'node:module'; import { transformSync } from 'esbuild'; assert.equal(typeof pc.green, 'function'); assert.match(transformSync('const square = (x: number) => x * x', { loader: 'ts' }).code, /const square/); console.log(pc.green('pnpm 12 links and native esbuild binary loaded')); + +checkArchiveProbe(); + +let lockfile; +try { lockfile = (await import('proper-lockfile')).default; } +catch (error) { + const require = createRequire(import.meta.url); + const owner = path.dirname(require.resolve('proper-lockfile/package.json')); + for (const file of [path.resolve(owner, '../retry'), path.resolve('node_modules/.pnpm/retry@0.12.0/node_modules/retry')]) { + const stat = fs.lstatSync(file); + console.log('restored dependency', file, { symlink: stat.isSymbolicLink(), target: stat.isSymbolicLink() ? fs.readlinkSync(file) : null }); + } + throw error; +} +const release = lockfile.lockSync(process.cwd()); +try { assert.equal(lockfile.checkSync(process.cwd()), true); } +finally { release(); } diff --git a/fixtures/pnpm12-basic/lifecycle.mjs b/fixtures/pnpm12-basic/lifecycle.mjs index b864fab..3dd62b5 100644 --- a/fixtures/pnpm12-basic/lifecycle.mjs +++ b/fixtures/pnpm12-basic/lifecycle.mjs @@ -1,4 +1,7 @@ +import { seedArchiveProbe } from '../cache-archive-probe.mjs'; import fs from 'node:fs'; +seedArchiveProbe(); + if (process.env.LIFECYCLE_LOG) fs.appendFileSync(process.env.LIFECYCLE_LOG, 'postinstall\n'); if (process.env.FAIL_INSTALL === 'true') throw new Error('Instrumented install failure'); if (process.env.MUTATE_LOCK === 'true') fs.appendFileSync('pnpm-lock.yaml', '\n# lifecycle mutation\n'); diff --git a/fixtures/pnpm12-basic/package.json b/fixtures/pnpm12-basic/package.json index c9b6fb4..9cc9496 100644 --- a/fixtures/pnpm12-basic/package.json +++ b/fixtures/pnpm12-basic/package.json @@ -10,6 +10,7 @@ }, "dependencies": { "esbuild": "0.25.12", - "picocolors": "1.1.1" + "picocolors": "1.1.1", + "proper-lockfile": "4.1.2" } } diff --git a/fixtures/pnpm12-basic/pnpm-lock.yaml b/fixtures/pnpm12-basic/pnpm-lock.yaml index f2f2345..b06e030 100644 --- a/fixtures/pnpm12-basic/pnpm-lock.yaml +++ b/fixtures/pnpm12-basic/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: picocolors: specifier: 1.1.1 version: 1.1.1 + proper-lockfile: + specifier: 4.1.2 + version: 4.1.2 packages: @@ -178,9 +181,22 @@ packages: engines: {node: '>=18'} hasBin: true + graceful-fs@4.2.11: + resolution: {integrity: sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==} + picocolors@1.1.1: resolution: {integrity: sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==} + proper-lockfile@4.1.2: + resolution: {integrity: sha512-TjNPblN4BwAWMXU8s9AEz4JmQxnD1NNL7bNOY/AKUzyamc379FWASUhc/K1pL2noVb+XmZKLL68cjzLsiOAMaA==} + + retry@0.12.0: + resolution: {integrity: sha512-9LkiTwjUh6rT555DtE9rTX+BKByPfrMzEAtnlEtdEwr3Nkffwiihqe2bWADg+OQRjt9gl6ICdmB/ZFDCGAtSow==} + engines: {node: '>= 4'} + + signal-exit@3.0.7: + resolution: {integrity: sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==} + snapshots: '@esbuild/aix-ppc64@0.25.12': @@ -290,4 +306,16 @@ snapshots: '@esbuild/win32-ia32': 0.25.12 '@esbuild/win32-x64': 0.25.12 + graceful-fs@4.2.11: {} + picocolors@1.1.1: {} + + proper-lockfile@4.1.2: + dependencies: + graceful-fs: 4.2.11 + retry: 0.12.0 + signal-exit: 3.0.7 + + retry@0.12.0: {} + + signal-exit@3.0.7: {} diff --git a/scripts/completed-tree.mjs b/scripts/completed-tree.mjs index 33ec69b..416e613 100644 --- a/scripts/completed-tree.mjs +++ b/scripts/completed-tree.mjs @@ -102,6 +102,71 @@ export function verifyCompletedTree({ cwd, key, nodeMajor, nodeABI }) { throw new Error('Incompatible completed dependency tree key, Node major or ABI; installation/repair is forbidden.'); } if (!fs.existsSync(path.join(cwd, 'node_modules/.pnpm'))) throw new Error('Completed pnpm dependency tree is missing its virtual store.'); + if (process.platform === 'win32') restoreDirectoryLinks({ cwd, links: metadata.directoryLinks }); +} + +export function collectDirectoryLinks(cwd) { + cwd = fs.realpathSync(cwd); + const links = []; + const visit = (directory, dependencies = false) => { + for (const entry of fs.readdirSync(directory, { withFileTypes: true })) { + const file = path.join(directory, entry.name); + if (entry.isSymbolicLink() && dependencies && fs.statSync(file).isDirectory()) { + const target = fs.realpathSync(file); + assertPathWithinDirectory({ boundaryPath: cwd, candidatePath: target, description: 'Completed directory-link target' }); + links.push({ path: path.relative(cwd, file).split(path.sep).join('/'), target: path.relative(cwd, target).split(path.sep).join('/') }); + } else if (entry.isDirectory() && !['.git', '.pnpm-store', '.cache'].includes(entry.name)) { + visit(file, dependencies || entry.name === 'node_modules'); + } + } + }; + visit(cwd); + return links.sort((a, b) => a.path.localeCompare(b.path)); +} + +// GNU tar on Windows can restore a directory alias as a file symlink when its +// target appears later in the archive. Its text is correct, but stat returns +// EPERM. Rehydrate only directory links recorded by the producer, after key/ABI +// validation; never install packages or infer/repair missing dependency edges. +export function restoreDirectoryLinks({ cwd, links, filesystem = fs }) { + cwd = filesystem.realpathSync(cwd); + if (!Array.isArray(links)) throw new Error('Completed tree directory-link metadata is missing.'); + const seen = new Set(); + const operations = links.map((link) => { + if (!link || typeof link.path !== 'string' || typeof link.target !== 'string' || + path.isAbsolute(link.path) || path.isAbsolute(link.target) || + !link.path.split('/').includes('node_modules') || seen.has(link.path)) { + throw new Error('Invalid completed directory-link metadata.'); + } + seen.add(link.path); + const file = path.resolve(cwd, link.path); + const target = path.resolve(cwd, link.target); + const relative = path.relative(cwd, file); + if (!relative || relative === '..' || relative.startsWith(`..${path.sep}`)) throw new Error('Completed directory-link path escapes the workspace.'); + assertPathWithinDirectory({ boundaryPath: cwd, candidatePath: path.dirname(file), description: 'Completed directory-link parent' }); + assertPathWithinDirectory({ boundaryPath: cwd, candidatePath: target, description: 'Completed directory-link target' }); + if (!filesystem.lstatSync(file).isSymbolicLink() || !filesystem.statSync(target).isDirectory()) { + throw new Error('Completed directory-link source or target is incompatible.'); + } + const expected = path.toNamespacedPath(filesystem.realpathSync(target)); + const actual = path.toNamespacedPath(filesystem.realpathSync(path.resolve(path.dirname(file), filesystem.readlinkSync(file)))); + if (actual !== expected) throw new Error('Completed directory-link target changed.'); + try { + if (!filesystem.statSync(file).isDirectory()) throw new Error('Completed directory-link does not reference a directory.'); + return null; + } catch (error) { + if (!['EPERM', 'ENOENT'].includes(error.code)) throw error; + return { file, target }; + } + }); + // Validate every entry before changing any restored link. + for (const operation of operations.filter(Boolean)) { + filesystem.unlinkSync(operation.file); + filesystem.symlinkSync(path.relative(path.dirname(operation.file), operation.target), operation.file, 'junction'); + if (!filesystem.statSync(operation.file).isDirectory() || path.toNamespacedPath(filesystem.realpathSync(operation.file)) !== path.toNamespacedPath(filesystem.realpathSync(operation.target))) { + throw new Error('Completed directory-link restoration failed.'); + } + } } export function completePopulation({ cwd, key, nodeMajor, nodeABI, lockfilePath, lockfileSha, inputsHash, cacheInputs = '', repositoryRoot = cwd }) { @@ -113,6 +178,6 @@ export function completePopulation({ cwd, key, nodeMajor, nodeABI, lockfilePath, const metadataPath = path.join(cwd, METADATA_PATH); assertPathWithinDirectory({ boundaryPath: cwd, candidatePath: metadataPath, description: 'Completed tree metadata' }); fs.mkdirSync(path.dirname(metadataPath), { recursive: true }); - fs.writeFileSync(metadataPath, JSON.stringify({ key, nodeMajor: Number(nodeMajor), nodeABI: String(nodeABI), lockfileSha })); + fs.writeFileSync(metadataPath, JSON.stringify({ key, nodeMajor: Number(nodeMajor), nodeABI: String(nodeABI), lockfileSha, directoryLinks: collectDirectoryLinks(cwd) })); verifyCompletedTree({ cwd, key, nodeMajor, nodeABI }); } diff --git a/scripts/resolve-cache-paths.mjs b/scripts/resolve-cache-paths.mjs index 8300475..5137039 100644 --- a/scripts/resolve-cache-paths.mjs +++ b/scripts/resolve-cache-paths.mjs @@ -229,6 +229,10 @@ export function buildCachePaths( return [ `${base}node_modules`, `${base}**/node_modules`, + // actions/cache follows directory symlinks while expanding these globs. + // Archive each workspace root recursively once; nested matches can name + // pnpm files through aliases and produce unextractable tar hard links. + `!${base}**/node_modules/**/node_modules`, `${base}.cache/configure-nodejs/completed-tree.json`, ...electronCachePaths, ]; diff --git a/test/action-wiring.test.mjs b/test/action-wiring.test.mjs index 2c32a88..6ee7f44 100644 --- a/test/action-wiring.test.mjs +++ b/test/action-wiring.test.mjs @@ -400,7 +400,7 @@ test('executed strict prepare rejects misses and major/ABI mismatch without depe CONFIGURE_NODEJS_CACHE_KEY: 'exact-key', CONFIGURE_NODEJS_NEEDS_COREPACK: 'true', }; - fs.writeFileSync(path.join(cwd, '.cache/configure-nodejs/completed-tree.json'), JSON.stringify({ key: 'exact-key', nodeMajor: 24, nodeABI: scenario === 'abi' ? '999' : '137' })); + fs.writeFileSync(path.join(cwd, '.cache/configure-nodejs/completed-tree.json'), JSON.stringify({ key: 'exact-key', nodeMajor: 24, nodeABI: scenario === 'abi' ? '999' : '137', directoryLinks: [] })); const calls = []; const outputs = {}; const core = { setOutput: (key, value) => { outputs[key] = value; }, exportVariable: () => {}, info: () => {} }; diff --git a/test/completed-directory-links.test.mjs b/test/completed-directory-links.test.mjs new file mode 100644 index 0000000..aacaad1 --- /dev/null +++ b/test/completed-directory-links.test.mjs @@ -0,0 +1,70 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import { collectDirectoryLinks, restoreDirectoryLinks } from '../scripts/completed-tree.mjs'; + +function fixture(t) { + const cwd = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'completed-links-'))); + t.after(() => fs.rmSync(cwd, { recursive: true, force: true })); + const target = path.join(cwd, 'node_modules/.pnpm/retry/node_modules/retry'); + const file = path.join(cwd, 'node_modules/.pnpm/owner/node_modules/retry'); + fs.mkdirSync(target, { recursive: true }); + fs.mkdirSync(path.dirname(file), { recursive: true }); + fs.writeFileSync(path.join(target, 'index.js'), 'module.exports = 42;'); + fs.symlinkSync(path.relative(path.dirname(file), target), file, 'junction'); + return { cwd, target, file, links: collectDirectoryLinks(cwd) }; +} + +test('records directory targets without traversing package aliases', (t) => { + const { cwd, links } = fixture(t); + assert.deepEqual(links, [{ path: 'node_modules/.pnpm/owner/node_modules/retry', target: 'node_modules/.pnpm/retry/node_modules/retry' }]); + restoreDirectoryLinks({ cwd, links }); +}); + +test('restores a recorded directory-link type lost by Windows tar without installing', (t) => { + const { cwd, file, target, links } = fixture(t); + let restored = false; + const filesystem = { + ...fs, + statSync(candidate) { + if (candidate === file && !restored) throw Object.assign(new Error('wrong Windows link type'), { code: 'EPERM' }); + return fs.statSync(candidate); + }, + symlinkSync(...args) { + assert.equal(args[2], 'junction'); + fs.symlinkSync(...args); + restored = true; + }, + }; + restoreDirectoryLinks({ cwd, links, filesystem }); + assert.equal(restored, true); + assert.equal(fs.realpathSync(file), fs.realpathSync(target)); + assert.equal(fs.readFileSync(path.join(file, 'index.js'), 'utf8'), 'module.exports = 42;'); +}); + +test('validates all recorded paths before changing any restored link', (t) => { + const { cwd, links } = fixture(t); + let changes = 0; + const filesystem = { + ...fs, + statSync(candidate) { + if (candidate.endsWith(`${path.sep}owner${path.sep}node_modules${path.sep}retry`)) throw Object.assign(new Error('wrong type'), { code: 'EPERM' }); + return fs.statSync(candidate); + }, + unlinkSync() { changes++; }, + }; + assert.throws(() => restoreDirectoryLinks({ cwd, links: [...links, { path: '../outside/node_modules/link', target: links[0].target }], filesystem }), /escapes/); + assert.equal(changes, 0); +}); + +test('rejects changed, missing, duplicate and out-of-workspace dependency edges', (t) => { + const { cwd, file, links } = fixture(t); + for (const entries of [undefined, [...links, ...links], [{ ...links[0], target: '../outside' }], [{ ...links[0], target: 'node_modules' }], [{ ...links[0], path: 'node_modules/missing' }]]) { + assert.throws(() => restoreDirectoryLinks({ cwd, links: entries })); + } + fs.unlinkSync(file); + fs.mkdirSync(file); + assert.throws(() => restoreDirectoryLinks({ cwd, links }), /incompatible/); +}); diff --git a/test/completed-tree.test.mjs b/test/completed-tree.test.mjs index 60ad4fe..0dc6bb9 100644 --- a/test/completed-tree.test.mjs +++ b/test/completed-tree.test.mjs @@ -6,7 +6,7 @@ import path from 'node:path'; import crypto from 'node:crypto'; import { installationInputsHash, buildCompletedKey, validateCacheMode, completePopulation, verifyCompletedTree, removeMaterializedDependencies, METADATA_PATH } from '../scripts/completed-tree.mjs'; import { shouldInstallDependencies, shouldDiscardRestoredDependencies } from '../scripts/resolve-node-version.mjs'; -import { buildResult } from '../scripts/resolve-cache-paths.mjs'; +import { buildCachePaths, buildResult } from '../scripts/resolve-cache-paths.mjs'; import { hasCacheableDependencyPath } from '../scripts/detect-cache-paths.mjs'; function fixture(t) { @@ -95,6 +95,8 @@ test('default store paths and namespace remain distinct from completed trees', ( assert.equal(defaults.cacheKeyPrefix, 'pnpm-store'); assert.equal(completed.primaryCachePath, 'node_modules'); assert.ok(completed.cachePaths.includes(METADATA_PATH)); + assert.ok(completed.cachePaths.includes('!**/node_modules/**/node_modules')); + assert.ok(buildCachePaths('packages/app', 'pnpm', [], 'node-modules').includes('!packages/app/**/node_modules/**/node_modules')); assert.ok(!completed.cachePaths.includes('.pnpm-store')); });