Skip to content

Commit 4200fc3

Browse files
joyeecheungtargos
authored andcommitted
process: handle process.env.NODE_V8_COVERAGE in pre-execution
Since this depends on environment variable, and the worker threads do not need to persist the variable value because they cannot switch cwd. Backport-PR-URL: #26662 PR-URL: #26466 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent cc606e2 commit 4200fc3

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

lib/internal/bootstrap/node.js

-23
Original file line numberDiff line numberDiff line change
@@ -317,29 +317,6 @@ Object.defineProperty(process, 'features', {
317317
hasUncaughtExceptionCaptureCallback;
318318
}
319319

320-
// User-facing NODE_V8_COVERAGE environment variable that writes
321-
// ScriptCoverage to a specified file.
322-
if (process.env.NODE_V8_COVERAGE) {
323-
const originalReallyExit = process.reallyExit;
324-
const cwd = NativeModule.require('internal/process/execution').tryGetCwd();
325-
const { resolve } = NativeModule.require('path');
326-
// Resolve the coverage directory to an absolute path, and
327-
// overwrite process.env so that the original path gets passed
328-
// to child processes even when they switch cwd.
329-
const coverageDirectory = resolve(cwd, process.env.NODE_V8_COVERAGE);
330-
process.env.NODE_V8_COVERAGE = coverageDirectory;
331-
const {
332-
writeCoverage,
333-
setCoverageDirectory
334-
} = NativeModule.require('internal/coverage-gen/with_profiler');
335-
setCoverageDirectory(coverageDirectory);
336-
process.on('exit', writeCoverage);
337-
process.reallyExit = (code) => {
338-
writeCoverage();
339-
originalReallyExit(code);
340-
};
341-
}
342-
343320
function setupProcessObject() {
344321
const EventEmitter = NativeModule.require('events');
345322
const origProcProto = Object.getPrototypeOf(process);

lib/internal/bootstrap/pre_execution.js

+29
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ function prepareMainThreadExecution() {
1010

1111
setupWarningHandler();
1212

13+
// Resolve the coverage directory to an absolute path, and
14+
// overwrite process.env so that the original path gets passed
15+
// to child processes even when they switch cwd.
16+
if (process.env.NODE_V8_COVERAGE) {
17+
process.env.NODE_V8_COVERAGE =
18+
setupCoverageHooks(process.env.NODE_V8_COVERAGE);
19+
}
20+
1321
// Only main thread receives signals.
1422
setupSignalHandlers();
1523

@@ -47,6 +55,26 @@ function setupWarningHandler() {
4755
}
4856
}
4957

58+
// Setup User-facing NODE_V8_COVERAGE environment variable that writes
59+
// ScriptCoverage to a specified file.
60+
function setupCoverageHooks(dir) {
61+
const originalReallyExit = process.reallyExit;
62+
const cwd = require('internal/process/execution').tryGetCwd();
63+
const { resolve } = require('path');
64+
const coverageDirectory = resolve(cwd, dir);
65+
const {
66+
writeCoverage,
67+
setCoverageDirectory
68+
} = require('internal/coverage-gen/with_profiler');
69+
setCoverageDirectory(coverageDirectory);
70+
process.on('exit', writeCoverage);
71+
process.reallyExit = (code) => {
72+
writeCoverage();
73+
originalReallyExit(code);
74+
};
75+
return coverageDirectory;
76+
}
77+
5078
function initializeReport() {
5179
if (!getOptionValue('--experimental-report')) {
5280
return;
@@ -262,6 +290,7 @@ function loadPreloadModules() {
262290
}
263291

264292
module.exports = {
293+
setupCoverageHooks,
265294
setupWarningHandler,
266295
prepareMainThreadExecution,
267296
initializeDeprecations,

lib/internal/main/worker_thread.js

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// message port.
55

66
const {
7+
setupCoverageHooks,
78
setupWarningHandler,
89
initializeDeprecations,
910
initializeESMLoader,
@@ -42,6 +43,12 @@ const debug = require('util').debuglog('worker');
4243

4344
setupWarningHandler();
4445

46+
// Since worker threads cannot switch cwd, we do not need to
47+
// overwrite the process.env.NODE_V8_COVERAGE variable.
48+
if (process.env.NODE_V8_COVERAGE) {
49+
setupCoverageHooks(process.env.NODE_V8_COVERAGE);
50+
}
51+
4552
debug(`[${threadId}] is setting up worker child environment`);
4653

4754
// Set up the message port and start listening

0 commit comments

Comments
 (0)