From 4be5076f09fd2115620cbf1a3f4d219c5ff5d31e Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 6 Mar 2019 12:02:04 +0100 Subject: [PATCH 1/4] process: call `prepareMainThreadExecution` in `node inspect` Since we should treat the node-inspect as third-party user code. PR-URL: https://github.com/nodejs/node/pull/26466 Reviewed-By: Anna Henningsen Reviewed-By: Ruben Bridgewater --- 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 7ab1a111024aaaaeee1d559f12de2a109d7cbd72 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 6 Mar 2019 12:05:23 +0100 Subject: [PATCH 2/4] process: set up process warning handler in pre-execution Since it depends on environment variables. PR-URL: https://github.com/nodejs/node/pull/26466 Reviewed-By: Anna Henningsen Reviewed-By: Ruben Bridgewater --- 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 0b0fd7c24acad1..81ae3d902bc9a7 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 02acf6b46f6338..eb486e08064c3c 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; @@ -251,6 +262,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 00e7dd2d844952a4342ef149225239c2c85f2edf Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 6 Mar 2019 12:10:45 +0100 Subject: [PATCH 3/4] 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 81ae3d902bc9a7..2d71eec8053a05 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -317,29 +317,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 eb486e08064c3c..74266d0be33fbd 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; @@ -262,6 +290,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 From f442b5435886ed2f181d06bd3f9dcae365d3ed9d Mon Sep 17 00:00:00 2001 From: Refael Ackermann Date: Thu, 14 Mar 2019 11:29:58 -0400 Subject: [PATCH 4/4] process: move deprecation warning setup for --debug* args --- lib/internal/bootstrap/node.js | 14 -------------- lib/internal/bootstrap/pre_execution.js | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index 2d71eec8053a05..aff746f9716032 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -219,20 +219,6 @@ Object.defineProperty(process, 'argv0', { }); process.argv[0] = process.execPath; -// Handle `--debug*` deprecation and invalidation. -if (process._invalidDebug) { - process.emitWarning( - '`node --debug` and `node --debug-brk` are invalid. ' + - 'Please use `node --inspect` or `node --inspect-brk` instead.', - 'DeprecationWarning', 'DEP0062', undefined, true); - process.exit(9); -} else if (process._deprecatedDebugBrk) { - process.emitWarning( - '`node --inspect --debug-brk` is deprecated. ' + - 'Please use `node --inspect-brk` instead.', - 'DeprecationWarning', 'DEP0062', undefined, true); -} - // TODO(jasnell): The following have been globals since around 2012. // That's just silly. The underlying perfctr support has been removed // so these are now deprecated non-ops that can be removed after one diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index 74266d0be33fbd..87fad375053a15 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -18,6 +18,20 @@ function prepareMainThreadExecution() { setupCoverageHooks(process.env.NODE_V8_COVERAGE); } + // Handle `--debug*` deprecation and invalidation. + if (process._invalidDebug) { + process.emitWarning( + '`node --debug` and `node --debug-brk` are invalid. ' + + 'Please use `node --inspect` or `node --inspect-brk` instead.', + 'DeprecationWarning', 'DEP0062', undefined, true); + process.exit(9); + } else if (process._deprecatedDebugBrk) { + process.emitWarning( + '`node --inspect --debug-brk` is deprecated. ' + + 'Please use `node --inspect-brk` instead.', + 'DeprecationWarning', 'DEP0062', undefined, true); + } + // Only main thread receives signals. setupSignalHandlers();