-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
cmd: support dash as stdin alias #13012
Conversation
setTimeout(function() { | ||
child.stdin.end('console.log(process.pid)'); | ||
}, 1); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: We like an \n
on the last line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done for both
let found = ''; | ||
for (const args of [[], ['-']]) { | ||
const child = spawn(process.execPath, args, { | ||
env: Object.assign(process.env, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you want the arguments to assign
the other way around?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just copied… is this an existing bug? :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just copied from the old version, I guess better to not touch it now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 this is a super weird construct. IMHO If it's to suss out a bug it should be near https://github.com/nodejs/node/blob/master/test/parallel/test-child-process-spawn-shell.js#L51
Personaly I'd rather see this fixed here, but I won't block for it.
@@ -123,7 +123,7 @@ | |||
const internalModule = NativeModule.require('internal/module'); | |||
internalModule.addBuiltinLibsToObject(global); | |||
evalScript('[eval]'); | |||
} else if (process.argv[1]) { | |||
} else if (process.argv[1] && process.argv[1] !== '-') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO --
is much more common alias for "the rest will be piped through stdin
". If it's not too much work I'd be happy if you adjust.
So you could do echo "console.log(process.argv[2])" | node -h --
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got you -h
is an arg to the script. Not to node.
Retracting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already use --
for separating arguments, and that’s what it’s commonly used for (on Unixes, at least). -
is the most common choice for “stdin”, by far.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last comment is a real nit.
src/node.cc
Outdated
while (index < nargs && argv[index][0] == '-' && !short_circuit) { | ||
while (index < nargs && | ||
// starts with dash but is not only a dash which is special | ||
argv[index][0] == '-' && strlen(argv[index]) != 1 && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could replace the strlen
check with argv[index][1] == '\0'
let found = ''; | ||
for (const args of [[], ['-']]) { | ||
const child = spawn(process.execPath, args, { | ||
env: Object.assign(process.env, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just copied… is this an existing bug? :/
I think I've fixed these |
Thank you :) Is linter giving a true found error? I couldn't find anything useful from its console info. |
setTimeout(function() { | ||
child.stdin.end('console.log(process.pid)'); | ||
}, 1); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here just one blank line, i.e. file should end }\n
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super weird, I really didn't want to put two... I guess done now
eefb4e3
to
1497e2b
Compare
Great, it seems CI don't dislike this anymore |
Yeah I run a linter CI: https://ci.nodejs.org/job/node-test-linter/9043/ |
No meaningful output on |
I've retriggered linux-fips: https://ci.nodejs.org/job/node-test-commit-linux-fips/8382/ I'm assuming this is your first contribution so I've put down a little explanation. If it's not ignore the rest 😉 Our "landing" policy is to wait 2 days (3-4 if it's weekend) so that stakeholders can chime in. Since I've marked this as Good luck, and thank you very much for the contribution 🥇 |
np, I just wanted to know if my part is complete :) |
src/node.cc
Outdated
@@ -3780,7 +3780,10 @@ static void ParseArgs(int* argc, | |||
|
|||
unsigned int index = 1; | |||
bool short_circuit = false; | |||
while (index < nargs && argv[index][0] == '-' && !short_circuit) { | |||
while (index < nargs && | |||
// starts with dash but is not only a dash which is special |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’d drop this comment, or change it to something like // skip "-"
, because currently it described how it does stuff, not what it does, so it’s directly redundant to the code.
child.stdin.end('console.log(process.argv[2])'); | ||
|
||
let actual = ''; | ||
child.stdout.on('data', (chunk) => actual += chunk); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should call child.stdout.setEncoding('utf8');
before using .on('data')
with strings
Done |
Going to land this at the end of today, then. |
877fbfe
to
0283fc0
Compare
@@ -3598,6 +3598,8 @@ static void PrintHelp() { | |||
" does not appear to be a terminal\n" | |||
" -r, --require module to preload (option can be " | |||
"repeated)\n" | |||
" - script read from stdin (default; " | |||
"interactive mode if a tty)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just added this line from the python -h
, please review it before the merge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean ahh "from"node -h
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant I've copied this literally from the similar place on python -h
:)
@@ -10,7 +10,7 @@ To view this documentation as a manual page in a terminal, run `man node`. | |||
|
|||
## Synopsis | |||
|
|||
`node [options] [v8 options] [script.js | -e "script"] [--] [arguments]` | |||
`node [options] [v8 options] [script.js | -e "script" | -] [--] [arguments]` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed space after the dash as its section start also is not space padded.
child.on('close', common.mustCall(function(c) { | ||
assert.strictEqual(c, 0); | ||
assert.strictEqual(found, wanted); | ||
console.log('ok'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the console
output necessary to the test? If not, please remove :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
3a63d34
to
55e51d6
Compare
@Fishrock123 are you still planning on landing this? |
Landed in 594b5d7, thanks for the PR! |
PR-URL: #13012 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]>
@refack ok not really, go ahead and land it please |
huh, guess github didn't update... Thanks @addaleax 😬 😓 |
PR-URL: #13012 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]>
PR-URL: #13012 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]>
Release team were +1 on backporting to v6.x if someone would be willing to raise a backport PR, see the guide for more info. |
If this gets backported, #18440 should probably also be applied. |
make -j4 test
(UNIX), orvcbuild test
(Windows) passes(some of the tests are failing regardless of my change AFAICS so I assume this as passed)
(an available test is modified to cover this, and a more specific one also added)
Affected core subsystem(s)
cli
Dash as stdin alias is a usual convention between unix programs https://unix.stackexchange.com/a/16364 and I believe it should be available on nodejs also.
Here I've made a change that make node ignore dash as an option so it will effectively be treated like /dev/stdin
Simply, with my change this will be made possible with node:
echo "console.log(process.argv[2])" | node - --option-for-stdin-script
The reason I like to have this is because of my one-file webserver http://pad.js.org which in order to be able to pass extra option to its one-linear execution command on the fly (without having to download or install the script first, which it supports that also through npm and docker). BTW, have a look at that project also, I guess you will find it interesting, specially the way its source is distributed and several features it offers :)
Interestingly, I saw during my tests if I pass "/std/stdin" instead "-" on the above command, even without my change, it will be ran on macOS just fine but for some reasons it seems it doesn't work on Linux equally which perhaps should be filed as a bug separately (Edited: proposed another patch for that). However with this change, "-" will provide a cross platform solution for achieving this goal.
For my specific case, this patch will make this functionality possible:
curl pad.js.org | node - -h
which as mentioned before if you have access to macOS, this is already possible with the following command, but not on Linux and obviously ever Windows:
curl pad.js.org | node /dev/stdin -h
Thanks.