Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v11.x backport] process: move more dependency of environment variables to pre-execution #26662

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 1 addition & 41 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -222,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
Expand Down Expand Up @@ -320,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
55 changes: 55 additions & 0 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@ let traceEventsAsyncHook;
function prepareMainThreadExecution() {
setupTraceCategoryState();

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);
}

// 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();

Expand Down Expand Up @@ -36,6 +60,35 @@ function prepareMainThreadExecution() {
loadPreloadModules();
}

function setupWarningHandler() {
const {
onWarning
} = require('internal/process/warning');
if (!process.noProcessWarnings && process.env.NODE_NO_WARNINGS !== '1') {
process.on('warning', onWarning);
}
}

// 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 @@ -251,6 +304,8 @@ function loadPreloadModules() {
}

module.exports = {
setupCoverageHooks,
setupWarningHandler,
prepareMainThreadExecution,
initializeDeprecations,
initializeESMLoader,
Expand Down
6 changes: 6 additions & 0 deletions lib/internal/main/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
10 changes: 10 additions & 0 deletions lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// message port.

const {
setupCoverageHooks,
setupWarningHandler,
initializeDeprecations,
initializeESMLoader,
initializeFrozenIntrinsics,
Expand Down Expand Up @@ -39,6 +41,14 @@ const {
const publicWorker = require('worker_threads');
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