From bdbcd0f6143fd8d208debcad7fc5474186c8cbb2 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 6 Mar 2019 12:02:04 +0100 Subject: [PATCH 1/3] process: call `prepareMainThreadExecution` in `node inspect` Since we should treat the node-inspect as third-party user code. --- lib/internal/main/inspect.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/internal/main/inspect.js b/lib/internal/main/inspect.js index 7376f8984b13e1..f6777fe852bcfc 100644 --- a/lib/internal/main/inspect.js +++ b/lib/internal/main/inspect.js @@ -2,6 +2,12 @@ // `node inspect ...` or `node debug ...` +const { + prepareMainThreadExecution +} = require('internal/bootstrap/pre_execution'); + +prepareMainThreadExecution(); + if (process.argv[1] === 'debug') { process.emitWarning( '`node debug` is deprecated. Please use `node inspect` instead.', From 0e97fa381d89ae3fe58d4da4b09b1e07ee3335bc Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 6 Mar 2019 12:05:23 +0100 Subject: [PATCH 2/3] process: set up process warning handler in pre-execution Since it depends on environment variables. --- lib/internal/bootstrap/node.js | 5 +---- lib/internal/bootstrap/pre_execution.js | 12 ++++++++++++ lib/internal/main/worker_thread.js | 3 +++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index eb1f3e582045ae..ef19d4799c5d9d 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -115,12 +115,9 @@ if (isMainThread) { } const { - onWarning, emitWarning } = NativeModule.require('internal/process/warning'); -if (!process.noProcessWarnings && process.env.NODE_NO_WARNINGS !== '1') { - process.on('warning', onWarning); -} + process.emitWarning = emitWarning; const { diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index 992c0c1983e722..55ce3b29e793b5 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -8,6 +8,8 @@ let traceEventsAsyncHook; function prepareMainThreadExecution() { setupTraceCategoryState(); + setupWarningHandler(); + // Only main thread receives signals. setupSignalHandlers(); @@ -36,6 +38,15 @@ function prepareMainThreadExecution() { loadPreloadModules(); } +function setupWarningHandler() { + const { + onWarning + } = require('internal/process/warning'); + if (!process.noProcessWarnings && process.env.NODE_NO_WARNINGS !== '1') { + process.on('warning', onWarning); + } +} + function initializeReport() { if (!getOptionValue('--experimental-report')) { return; @@ -231,6 +242,7 @@ function loadPreloadModules() { } module.exports = { + setupWarningHandler, prepareMainThreadExecution, initializeDeprecations, initializeESMLoader, diff --git a/lib/internal/main/worker_thread.js b/lib/internal/main/worker_thread.js index 0dc1b61ef94f45..4ecceff4e6ec41 100644 --- a/lib/internal/main/worker_thread.js +++ b/lib/internal/main/worker_thread.js @@ -4,6 +4,7 @@ // message port. const { + setupWarningHandler, initializeDeprecations, initializeESMLoader, initializeFrozenIntrinsics, @@ -39,6 +40,8 @@ const { const publicWorker = require('worker_threads'); const debug = require('util').debuglog('worker'); +setupWarningHandler(); + debug(`[${threadId}] is setting up worker child environment`); // Set up the message port and start listening From 410c5881644b26c7e21a61599dbe01dc8d8b9537 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 6 Mar 2019 12:10:45 +0100 Subject: [PATCH 3/3] 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. --- 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 ef19d4799c5d9d..bceb8f4512894d 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -278,29 +278,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 55ce3b29e793b5..9474912c534f95 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; @@ -242,6 +270,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 4ecceff4e6ec41..0289f97fb1c110 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