You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$? after a pipe is the exit code of the last command in the pipe, which in this case is head -n1. That's why you get 0. If you're using Bash, ${PIPESTATUS[0]} will give you the exit code of node --help which 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
Context of how I noticed this: I was trying to trim the massive --help menus of a bunch of different commands like clang, deno, bun, cargo, rustc, node, etc. and node --help | head -n20 failed. 🤷♀️
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 --help printing code to use helpText += partN and .write(helpText) with a try block with a catch (error) and if (error?.code !== 'EPIPE') throw error rethrow
This would:
Not interfere with the useful and expected feature of throwing on closed pipes
Stop the weird error from happening with node --help
Activity
kvakil commentedon Jan 14, 2024
$?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.Could you elaborate a little on why you need this? Is it causing issues for you or just something you noticed out of curiosity?
[-]`node --help | head -n1` throws an error but still exits with code 0[/-][+]`node --help | head -n1` throws an error[/+]jcbhmr commentedon 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. 🤷♀️jcbhmr commentedon Jan 14, 2024
This works:
This works:
This is what the
node --helpprinting code looks like:node/lib/internal/main/print_help.js
Lines 200 to 233 in c25878d
Specifically note the use of multiple
.write()calls.This FAILS:
This works:
What I suggest:
console.log()which appears to already have this swallowing error behaviournode --helpprinting code to usehelpText += partNand.write(helpText)with atryblock with acatch (error)andif (error?.code !== 'EPIPE') throw errorrethrowThis would:
node --helpbootstrap: print `--help` message using `console.log`
bootstrap: print `--help` message using `console.log`
bootstrap: print `--help` message using `console.log`