Files
node/test/parallel/test-compile-cache-api-portable.js
Shelley Vohr adad4d650d module: do not split a portable compile cache by uid
A portable compile cache is meant to be reused wherever the same module
layout is found, which includes being generated once (at build time,
say) and shipped read-only with an application for whoever runs it. On
platforms with uids the cache subdirectory is suffixed with the uid of
the user who created it, so such a cache is only ever found by that one
user. Omit the suffix in portable mode: a user who cannot write to the
directory still reads it, and a failed write is only a cache miss.

Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/65293
Reviewed-By: James M Snell <jasnell@gmail.com>
2026-08-21 21:53:19 +00:00

109 lines
2.9 KiB
JavaScript

'use strict';
// This tests module.enableCompileCache({ directory, portable: true }) works
// and supports portable paths across directory relocations.
require('../common');
const { spawnSyncAndAssert } = require('../common/child_process');
const assert = require('assert');
const fs = require('fs');
const tmpdir = require('../common/tmpdir');
const path = require('path');
const fixtures = require('../common/fixtures');
tmpdir.refresh();
const workDir = path.join(tmpdir.path, 'work');
const cacheRel = '.compile_cache_dir';
fs.mkdirSync(workDir, { recursive: true });
const wrapper = fixtures.path('compile-cache-wrapper-options.js');
const target = path.join(workDir, 'target.js');
fs.writeFileSync(target, '');
const NODE_TEST_COMPILE_CACHE_OPTIONS = JSON.stringify({ directory: cacheRel, portable: true });
// First run
{
spawnSyncAndAssert(
process.execPath,
['-r', wrapper, target],
{
env: {
...process.env,
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
NODE_TEST_COMPILE_CACHE_OPTIONS,
},
cwd: workDir,
},
{
stdout(output) {
console.log(output);
assert.match(output, /dir before enableCompileCache: undefined/);
assert.match(
output,
/dir after enableCompileCache: .+\.compile_cache_dir/
);
return true;
},
stderr(output) {
assert.match(
output,
/target\.js was not initialized, initializing the in-memory entry/
);
assert.match(output, /writing cache for .*target\.js.*success/);
return true;
},
}
);
}
// The cache subdirectory of a portable cache is not suffixed with the uid,
// so a cache generated by one user is found by another.
{
const [tag] = fs.readdirSync(path.join(workDir, cacheRel));
// $NODE_VERSION-$ARCH-$CACHE_DATA_VERSION_TAG, with no -$UID.
assert.match(tag, new RegExp(`^${process.version}-${process.arch}-[0-9a-f]+$`));
}
// Second run — moved directory, but same relative cache path
{
const movedWorkDir = `${workDir}_moved`;
fs.renameSync(workDir, movedWorkDir);
spawnSyncAndAssert(
process.execPath,
[
'-r',
wrapper,
path.join(movedWorkDir, 'target.js'),
],
{
env: {
...process.env,
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
NODE_TEST_COMPILE_CACHE_OPTIONS,
},
cwd: movedWorkDir,
},
{
stdout(output) {
console.log(output);
assert.match(output, /dir before enableCompileCache: undefined/);
assert.match(
output,
/dir after enableCompileCache: .+\.compile_cache_dir/
);
return true;
},
stderr(output) {
assert.match(
output,
/cache for .*target\.js was accepted, keeping the in-memory entry/
);
assert.match(output, /.*skip .*target\.js because cache was the same/);
return true;
},
}
);
}