Skip to content

Commit 0b7bf17

Browse files
fix: forward the compile cache only when the CLI enabled it
A cache a preload already enabled reports its version-tagged directory, which a child would tag again. Also exit the test config from the write callback so the report survives an asynchronous pipe.
1 parent 54c7b04 commit 0b7bf17

4 files changed

Lines changed: 24 additions & 5 deletions

File tree

‎packages/webpack-cli/bin/cli.js‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44

55
// `module.enableCompileCache` is only available on Node.js >= 22.8.0
66
// eslint-disable-next-line n/no-unsupported-features/node-builtins
7-
const { enableCompileCache } = require("node:module");
7+
const { constants, enableCompileCache } = require("node:module");
88

99
if (enableCompileCache) {
1010
try {
11-
const { directory } = enableCompileCache();
11+
const { status, directory } = enableCompileCache();
1212

1313
// Minimizer workers and other child processes start without the cache;
14-
// the variable Node reads at startup hands them the same directory.
15-
if (directory && process.env.NODE_COMPILE_CACHE === undefined) {
14+
// the variable Node reads at startup hands them the same directory. Only a
15+
// fresh start returns the base directory, which children tag by version.
16+
if (
17+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
18+
status === constants.compileCacheStatus.ENABLED &&
19+
process.env.NODE_COMPILE_CACHE === undefined
20+
) {
1621
process.env.NODE_COMPILE_CACHE = directory;
1722
}
1823
} catch {

‎test/build/compile-cache/compile-cache.test.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ describe("compile cache", () => {
6060
}
6161
});
6262

63+
it("should hand nothing on when a preload has already enabled the compile cache", async () => {
64+
const { variable, child, worker } = await report({
65+
NODE_OPTIONS: `--require ${path.resolve(__dirname, "preload.js")}`,
66+
});
67+
68+
// The preload's directory is already version-tagged; handing it on would tag it twice.
69+
expect(variable).toBeNull();
70+
expect(child).toBe(worker);
71+
expect(child.match(/v\d+\.\d+\.\d+-/g)).toHaveLength(1);
72+
});
73+
6374
it("should hand nothing on where the compile cache is disabled", async () => {
6475
expect(await report({ NODE_DISABLE_COMPILE_CACHE: "1" })).toEqual({
6576
variable: null,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
require("node:module").enableCompileCache();

‎test/build/compile-cache/webpack.config.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ new Worker(`require("node:worker_threads").parentPort.postMessage(${script})`, {
1919
child: JSON.parse(child),
2020
worker,
2121
})}\n`,
22+
() => process.exit(0),
2223
);
23-
process.exit(0);
2424
});

0 commit comments

Comments
 (0)