From f617a732f86289f39e4f2ce41564b0f35b00a9cf Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 6 Mar 2019 12:10:45 +0100 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/26466 Reviewed-By: Anna Henningsen Reviewed-By: Ruben Bridgewater --- lib/internal/bootstrap/node.js | 23 -------------------- lib/internal/bootstrap/pre_execution.js | 29 +++++++++++++++++++++++++ lib/internal/main/worker_thread.js | 7 ++++++ 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index f04bfe23fbc..9b13d326c21 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -277,29 +277,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); diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index 9dc93a54780..49fc025db01 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -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(); @@ -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; @@ -256,6 +284,7 @@ function loadPreloadModules() { } module.exports = { + setupCoverageHooks, setupWarningHandler, prepareMainThreadExecution, initializeDeprecations, diff --git a/lib/internal/main/worker_thread.js b/lib/internal/main/worker_thread.js index 4ecceff4e6e..0289f97fb1c 100644 --- a/lib/internal/main/worker_thread.js +++ b/lib/internal/main/worker_thread.js @@ -4,6 +4,7 @@ // message port. const { + setupCoverageHooks, setupWarningHandler, initializeDeprecations, initializeESMLoader, @@ -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