Skip to content

Commit

Permalink
process: handle process.env.NODE_V8_COVERAGE in pre-execution
Browse files Browse the repository at this point in the history
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: nodejs#26466
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
  • Loading branch information
joyeecheung committed Mar 15, 2019
1 parent 50e4722 commit 95a3844
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
23 changes: 0 additions & 23 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,29 +303,6 @@ Object.defineProperty(process, 'features', {
hasUncaughtExceptionCaptureCallback;
}

// User-facing NODE_V8_COVERAGE environment variable that writes
// ScriptCoverage to a specified file.
if (process.env.NODE_V8_COVERAGE) {
const originalReallyExit = process.reallyExit;
const cwd = NativeModule.require('internal/process/execution').tryGetCwd();
const { resolve } = NativeModule.require('path');
// Resolve the coverage directory to an absolute path, and
// overwrite process.env so that the original path gets passed
// to child processes even when they switch cwd.
const coverageDirectory = resolve(cwd, process.env.NODE_V8_COVERAGE);
process.env.NODE_V8_COVERAGE = coverageDirectory;
const {
writeCoverage,
setCoverageDirectory
} = NativeModule.require('internal/coverage-gen/with_profiler');
setCoverageDirectory(coverageDirectory);
process.on('exit', writeCoverage);
process.reallyExit = (code) => {
writeCoverage();
originalReallyExit(code);
};
}

function setupProcessObject() {
const EventEmitter = NativeModule.require('events');
const origProcProto = Object.getPrototypeOf(process);
Expand Down
29 changes: 29 additions & 0 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ function prepareMainThreadExecution() {

setupWarningHandler();

// Resolve the coverage directory to an absolute path, and
// overwrite process.env so that the original path gets passed
// to child processes even when they switch cwd.
if (process.env.NODE_V8_COVERAGE) {
process.env.NODE_V8_COVERAGE =
setupCoverageHooks(process.env.NODE_V8_COVERAGE);
}

// Only main thread receives signals.
setupSignalHandlers();

Expand Down Expand Up @@ -47,6 +55,26 @@ function setupWarningHandler() {
}
}

// Setup User-facing NODE_V8_COVERAGE environment variable that writes
// ScriptCoverage to a specified file.
function setupCoverageHooks(dir) {
const originalReallyExit = process.reallyExit;
const cwd = require('internal/process/execution').tryGetCwd();
const { resolve } = require('path');
const coverageDirectory = resolve(cwd, dir);
const {
writeCoverage,
setCoverageDirectory
} = require('internal/coverage-gen/with_profiler');
setCoverageDirectory(coverageDirectory);
process.on('exit', writeCoverage);
process.reallyExit = (code) => {
writeCoverage();
originalReallyExit(code);
};
return coverageDirectory;
}

function initializeReport() {
if (!getOptionValue('--experimental-report')) {
return;
Expand Down Expand Up @@ -279,6 +307,7 @@ function loadPreloadModules() {
}

module.exports = {
setupCoverageHooks,
setupWarningHandler,
prepareMainThreadExecution,
initializeDeprecations,
Expand Down
7 changes: 7 additions & 0 deletions lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// message port.

const {
setupCoverageHooks,
setupWarningHandler,
initializeDeprecations,
initializeESMLoader,
Expand Down Expand Up @@ -42,6 +43,12 @@ const debug = require('util').debuglog('worker');

setupWarningHandler();

// Since worker threads cannot switch cwd, we do not need to
// overwrite the process.env.NODE_V8_COVERAGE variable.
if (process.env.NODE_V8_COVERAGE) {
setupCoverageHooks(process.env.NODE_V8_COVERAGE);
}

debug(`[${threadId}] is setting up worker child environment`);

// Set up the message port and start listening
Expand Down

0 comments on commit 95a3844

Please sign in to comment.