Skip to content

Commit cba6905

Browse files
authored
perf: prefer module.enableCompileCache over v8-compile-cache (#574)
1 parent 72ed05b commit cba6905

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

mkshims.ts

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ async function main() {
2222
fs.writeFileSync(corepackPath, [
2323
`#!/usr/bin/env node`,
2424
`process.env.COREPACK_ENABLE_DOWNLOAD_PROMPT??='0';`,
25+
`require('module').enableCompileCache?.();`,
2526
`require('./lib/corepack.cjs').runMain(process.argv.slice(2));`,
2627
].join(`\n`));
2728
fs.chmodSync(corepackPath, 0o755);
@@ -34,6 +35,7 @@ async function main() {
3435
const entryScript = [
3536
`#!/usr/bin/env node`,
3637
`process.env.COREPACK_ENABLE_DOWNLOAD_PROMPT??='1'`,
38+
`require('module').enableCompileCache?.();`,
3739
`require('./lib/corepack.cjs').runMain(['${binaryName}', ...process.argv.slice(2)]);`,
3840
].join(`\n`);
3941

sources/corepackUtils.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,16 @@ export async function runVersion(locator: Locator, installSpec: InstallSpec & {s
400400
if (!binPath)
401401
throw new Error(`Assertion failed: Unable to locate path for bin '${binName}'`);
402402

403-
// Node.js segfaults when using npm@>=9.7.0 and v8-compile-cache
404-
// $ docker run -it node:20.3.0-slim corepack npm@9.7.1 --version
405-
// [SIGSEGV]
406-
if (locator.name !== `npm` || semverLt(locator.reference, `9.7.0`))
407-
// @ts-expect-error - No types
408-
await import(`v8-compile-cache`);
403+
// @ts-expect-error - Missing types
404+
if (!Module.enableCompileCache) {
405+
// Node.js segfaults when using npm@>=9.7.0 and v8-compile-cache
406+
// $ docker run -it node:20.3.0-slim corepack npm@9.7.1 --version
407+
// [SIGSEGV]
408+
if (locator.name !== `npm` || semverLt(locator.reference, `9.7.0`)) {
409+
// @ts-expect-error - No types
410+
await import(`v8-compile-cache`);
411+
}
412+
}
409413

410414
// We load the binary into the current process,
411415
// while making it think it was spawned.
@@ -429,6 +433,12 @@ export async function runVersion(locator: Locator, installSpec: InstallSpec & {s
429433
// Use nextTick to unwind the stack, and consequently remove Corepack from
430434
// the stack trace of the package manager.
431435
process.nextTick(Module.runMain, binPath);
436+
437+
// @ts-expect-error - No types
438+
if (Module.flushCompileCache) {
439+
// @ts-expect-error - No types
440+
setImmediate(Module.flushCompileCache);
441+
}
432442
}
433443

434444
export function shouldSkipIntegrityCheck() {

0 commit comments

Comments
 (0)