Skip to content
Merged
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
36 changes: 36 additions & 0 deletions fixtures/cache-archive-probe.mjs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
3 changes: 3 additions & 0 deletions fixtures/pnpm-basic/check.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { checkArchiveProbe } from '../cache-archive-probe.mjs';
import pc from 'picocolors';

if (typeof pc.green !== 'function') {
throw new Error('Expected picocolors.green to be available after pnpm install.');
}

console.log(pc.green('pnpm fixture dependency loaded'));

checkArchiveProbe();
3 changes: 3 additions & 0 deletions fixtures/pnpm-basic/write-electron-cache-markers.mjs
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
21 changes: 21 additions & 0 deletions fixtures/pnpm12-basic/check.mjs
Original file line number Diff line number Diff line change
@@ -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(); }
3 changes: 3 additions & 0 deletions fixtures/pnpm12-basic/lifecycle.mjs
Original file line number Diff line number Diff line change
@@ -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');
3 changes: 2 additions & 1 deletion fixtures/pnpm12-basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"esbuild": "0.25.12",
"picocolors": "1.1.1"
"picocolors": "1.1.1",
"proper-lockfile": "4.1.2"
}
}
28 changes: 28 additions & 0 deletions fixtures/pnpm12-basic/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 66 additions & 1 deletion scripts/completed-tree.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand All @@ -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 });
}
4 changes: 4 additions & 0 deletions scripts/resolve-cache-paths.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
Expand Down
2 changes: 1 addition & 1 deletion test/action-wiring.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => {} };
Expand Down
70 changes: 70 additions & 0 deletions test/completed-directory-links.test.mjs
Original file line number Diff line number Diff line change
@@ -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/);
});
4 changes: 3 additions & 1 deletion test/completed-tree.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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'));
});

Expand Down
Loading