Skip to content

Commit f617a73

Browse files
committed
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. PR-URL: #26466 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent d7543a7 commit f617a73

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
@@ -277,29 +277,6 @@ Object.defineProperty(process, 'features', {
277277
hasUncaughtExceptionCaptureCallback;
278278
}
279279

280-
// User-facing NODE_V8_COVERAGE environment variable that writes
281-
// ScriptCoverage to a specified file.
282-
if (process.env.NODE_V8_COVERAGE) {
283-
const originalReallyExit = process.reallyExit;
284-
const cwd = NativeModule.require('internal/process/execution').tryGetCwd();
285-
const { resolve } = NativeModule.require('path');
286-
// Resolve the coverage directory to an absolute path, and
287-
// overwrite process.env so that the original path gets passed
288-
// to child processes even when they switch cwd.
289-
const coverageDirectory = resolve(cwd, process.env.NODE_V8_COVERAGE);
290-
process.env.NODE_V8_COVERAGE = coverageDirectory;
291-
const {
292-
writeCoverage,
293-
setCoverageDirectory
294-
} = NativeModule.require('internal/coverage-gen/with_profiler');
295-
setCoverageDirectory(coverageDirectory);
296-
process.on('exit', writeCoverage);
297-
process.reallyExit = (code) => {
298-
writeCoverage();
299-
originalReallyExit(code);
300-
};
301-
}
302-
303280
function setupProcessObject() {
304281
const EventEmitter = NativeModule.require('events');
305282
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;
@@ -256,6 +284,7 @@ function loadPreloadModules() {
256284
}
257285

258286
module.exports = {
287+
setupCoverageHooks,
259288
setupWarningHandler,
260289
prepareMainThreadExecution,
261290
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)