Repository navigation
node --help | head -n1 throws an error #51448
Description
Activity
$?after a pipe is the exit code of the last command in the pipe, which in this case ishead -n1. That's why you get0. If you're using Bash,${PIPESTATUS[0]}will give you the exit code ofnode --helpwhich in this case is indeed 1.expected behaviour is what deno and bun do: don't error lol
Could you elaborate a little on why you need this? Is it causing issues for you or just something you noticed out of curiosity?
- changed the title
[-]`node --help | head -n1` throws an error but still exits with code 0[/-][+]`node --help | head -n1` throws an error[/+]on Jan 14, 2024 Ohhhh so that's why it exited with code 0! 🤦♀️
Context of how I noticed this: I was trying to trim the massive
--helpmenus of a bunch of different commands likeclang,deno,bun,cargo,rustc,node, etc. andnode --help | head -n20failed. 🤷♀️This works:
node -e 'console.log("a\n".repeat(2000))' | head -n5
This works:
node -e 'process.stdout.write("a\n".repeat(2000))' | head -n5
This is what the
node --helpprinting code looks like:node/lib/internal/main/print_help.js
Lines 200 to 233 in c25878d
function print(stream) { const { options, aliases } = require('internal/options'); // Use 75 % of the available width, and at least 70 characters. const width = MathMax(70, (stream.columns || 0) * 0.75); const firstColumn = MathFloor(width * 0.4); const secondColumn = MathFloor(width * 0.57); options.set('-', { helpText: 'script read from stdin ' + '(default if no file name is provided, ' + 'interactive mode if a tty)' }); options.set('--', { helpText: 'indicate the end of node options' }); stream.write( 'Usage: node [options] [ script.js ] [arguments]\n' + ' node inspect [options] [ script.js | host:port ] [arguments]\n\n' + 'Options:\n'); stream.write(indent(format({ options, aliases, firstColumn, secondColumn, }), 2)); stream.write('\nEnvironment variables:\n'); stream.write(format({ options: envVars, firstColumn, secondColumn, })); stream.write('\nDocumentation can be found at https://nodejs.org/\n'); } prepareMainThreadExecution(); markBootstrapComplete(); print(process.stdout); Specifically note the use of multiple
.write()calls.This FAILS:
node -e 'for (const i of Array(1000).keys()) process.stdout.write(`${i}\n`)' | head -n5
This works:
node -e 'for (const i of Array(1000).keys()) console.log(i)' | head -n5
What I suggest:
- use
console.log()which appears to already have this swallowing error behaviour - OR if that can't happen: change the
node --helpprinting code to usehelpText += partNand.write(helpText)with atryblock with acatch (error)andif (error?.code !== 'EPIPE') throw errorrethrow
This would:
- Not interfere with the useful and expected feature of throwing on closed pipes
- Stop the weird error from happening with
node --help
- use
- added a commit that references this issue
on May 12, 2024 - added a commit that references this issue
on May 13, 2024 - added a commit that references this issue
on Jun 20, 2024
Version
v21.5.0
Platform
Linux codespaces-6c49bb 6.2.0-1018-azure #18~22.04.1-Ubuntu SMP Tue Nov 21 19:25:02 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
Subsystem
No response
What steps will reproduce the bug?
node --help | head -n1How often does it reproduce? Is there a required condition?
always
What is the expected behavior? Why is that the expected behavior?
expected behaviour is what deno and bun do: don't error lol
What do you see instead?
Additional information
No response