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

bootstrap: try-catch around potentially failing stream.write() for node --help #51463

Merged
merged 1 commit into from
May 12, 2024
Merged
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
12 changes: 7 additions & 5 deletions lib/internal/main/print_help.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ function format(

function print(stream) {
const { options, aliases } = getCLIOptionsInfo();
const console = require('internal/console/global');

// Use 75 % of the available width, and at least 70 characters.
const width = MathMax(70, (stream.columns || 0) * 0.75);
Expand All @@ -212,21 +213,22 @@ function print(stream) {
'(default if no file name is provided, ' +
'interactive mode if a tty)' });
options.set('--', { helpText: 'indicate the end of node options' });
stream.write(
let helpText = (
'Usage: node [options] [ script.js ] [arguments]\n' +
' node inspect [options] [ script.js | host:port ] [arguments]\n\n' +
'Options:\n');
stream.write(indent(format({
helpText += (indent(format({
options, aliases, firstColumn, secondColumn,
}), 2));

stream.write('\nEnvironment variables:\n');
helpText += ('\nEnvironment variables:\n');

stream.write(format({
helpText += (format({
options: envVars, firstColumn, secondColumn,
}));

stream.write('\nDocumentation can be found at https://nodejs.org/\n');
helpText += ('\nDocumentation can be found at https://nodejs.org/');
console.log(helpText);
}

prepareMainThreadExecution();
Expand Down
13 changes: 12 additions & 1 deletion test/parallel/test-cli-node-print-help.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const common = require('../common');
// returns the proper set of cli options when invoked

const assert = require('assert');
const { exec } = require('child_process');
const { exec, spawn } = require('child_process');
const { once } = require('events');
let stdOut;


Expand Down Expand Up @@ -53,3 +54,13 @@ function testForSubstring(options) {
}

startPrintHelpTest();

// Test closed stdout for `node --help`. Like `node --help | head -n5`.
(async () => {
const cp = spawn(process.execPath, ['--help'], {
stdio: ['inherit', 'pipe', 'inherit'],
});
cp.stdout.destroy();
const [exitCode] = await once(cp, 'exit');
assert.strictEqual(exitCode, 0);
})().then(common.mustCall());