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

"--" after "-e <script>" means end-of-options #10651

Closed
wants to merge 1 commit into from

Conversation

jBarz
Copy link
Contributor

@jBarz jBarz commented Jan 6, 2017

There is currently no way to pass the first argument to an eval script that starts with a hyphen.

node -e 'console.log(process.argv)' -arg1
[ '/usr/bin/node' ]

node -e 'console.log(process.argv)' -- -arg1
[ '/usr/bin/node' ]

After this fix
node -e 'console.log(process.argv)' -- -arg1
[ '/usr/bin/node', '-arg1' ]

Checklist
  • make -j4 test (UNIX)
  • tests added
  • commit message follows commit guidelines
Affected core subsystem(s)

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lts-watch-v6.x labels Jan 6, 2017
child.exec(nodejs + ' --eval "console.log(process.argv[1])" -- -arg1',
function(status, stdout, stderr) {
assert.strictEqual(stdout, '-arg1\n');
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One suggestion: Could you add tests for more arguments after --, too?

@mscdex mscdex added the process Issues and PRs related to the process subsystem. label Jan 6, 2017
'-arg1 arg2 --arg3',
'--',
].forEach(function(args) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this blank line.

function(status, stdout, stderr) {
assert.strictEqual(stdout, args+'\n');
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this one.

].forEach(function(args) {

child.exec(nodejs +
' --eval "console.log(process.argv.slice(1).join(\' \'))" -- ' +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do this string concatenation in a separate variable, this can be formatted in a much more readable fashion.

' --eval "console.log(process.argv.slice(1).join(\' \'))" -- ' +
args,
function(status, stdout, stderr) {
assert.strictEqual(stdout, args+'\n');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space before and after +. Does make lint complain about this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed it does :-) Will make sure to run that next time.

child.exec(nodejs +
' --eval "console.log(process.argv.slice(1).join(\' \'))" -- ' +
args,
function(status, stdout, stderr) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you wrap the callback in common.mustCall(), and add assertions for status (which should probably be renamed) and stderr.

@Fishrock123
Copy link
Contributor

To be clear, node -e 'console.log(process.argv)' -arg1 isn't being fixed because -arg1 would be conceptually passed to node and not the script, right?

@jBarz
Copy link
Contributor Author

jBarz commented Jan 6, 2017

Thats right

@@ -3690,6 +3690,10 @@ static void ParseArgs(int* argc,
} else if (strcmp(arg, "--expose-internals") == 0 ||
strcmp(arg, "--expose_internals") == 0) {
// consumed in js
} else if (strcmp(arg, "--") == 0 && eval_string != nullptr) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this patch also work with -p?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, The variable "eval_string" is used by both -e and -p

Copy link
Contributor

@Fishrock123 Fishrock123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fishrock123
Copy link
Contributor

Did... the linter error with no output?

+ gmake lint-ci
./node tools/jslint.js -j 2 -f tap -o test-eslint.tap \
	benchmark lib test tools
gmake: *** [Makefile:708: jslint-ci] Error 1

@jBarz could you trying running make -j4 lint?

@jBarz
Copy link
Contributor Author

jBarz commented Jan 6, 2017

Sorry, more linter errors fixed. Passes now

'--',
].forEach(function(args) {
const evalopt = ' --eval "console.log(process.argv.slice(1).join(\' \'))"';
child.exec(nodejs + evalopt + ' -- ' + args,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant by my earlier comment about readability is that you can do this:

const cmd = `${nodejs}${evalopt} -- ${args}`;

child.exec(cmd, common.mustCall(function(err, stdout, stderr) {
  ...
});

It gets rid of that awkward looking indented function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is certainly more readable. Will do

@jasnell
Copy link
Member

jasnell commented Jan 6, 2017

While I see the need the use of the -- just seems... hackish... Something like node -e "console.log(process.argv)" -a "-arg1" seems like a less hackish approach but has it's own range of short-comings. There aren't many great options for resolving this, however, so I won't object to using the --.

@Fishrock123
Copy link
Contributor

Fishrock123 commented Jan 6, 2017

node -e "console.log(process.argv)" -a "-arg1"

I'm not sure how that is less hackish when it uses the same number of characters and -- is already fairly standard for such uses?

Copy link
Contributor

@sam-github sam-github left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment: I would update the description to include how things work with an arg of arg2 in addition to what you currently describe with -arg1, to show how you are making things more consistent.

Also, I'm not sure if a non - prefixed arg case already exists in the unit tests, its a bit hard to see from diffs, but if it doesn't exist, it should.

@sam-github
Copy link
Contributor

sam-github commented Jan 9, 2017

FWIW, the behaviour implemented by this PR is how I would have assumed it worked already.

Is this semver-major, or minor, or patch?

I could call it semver-patch, since I think the existing behaviour doesn't accord with how I would assume node works.

On the other hand, I'm not sure the existing docs cover this behaviour... and @jBarz I just realized this PR doesn't update the docs or the node usage message, shouldn't it?

It would be possible for existing code to rely on the current behaviour, though, making this an incompatible semver-major change... or it could be possible to describe this just as a semver-minor, since now we have the capability of passing - prefixed arguments to -e scripts, which we didn't before.

/cc @gibfahn re: our conversation about what is or is not "API", and thus, an "API change" according to semver.

assert.strictEqual(stderr, '');
assert.strictEqual(err, null);
}));
});
Copy link
Member

@jasnell jasnell Jan 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this is also supposed to work with -p, including that in the test also would be ideal.

Also, given that this is not supposed to work outside of using -p and -e, a test verifying that -- fails when those are not used would be good.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "fails" and "not work"? -- should continue to work as it does currently in absence of -p and -e:

% node args.js -- --help
[ '/home/sam/.nvm/versions/node/v7.2.0/bin/node',
  '/home/sam/w/core/node/args.js',
  '--',
  '--help' ]
% node -- args.js --help   
[ '/home/sam/.nvm/versions/node/v7.2.0/bin/node',
  '/home/sam/w/core/node/args.js',
  '--help' ]
% cat args.js 
console.log(process.argv)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean that node -- aargs.js should fail.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except that this is a bug:

core/node (master $% u=) % node -- --help        
Usage: node [options] [ -e script | script.js ] [arguments] 
       node debug script.js [arguments] 
...

The -- should have stopped the options processing, I would have expected to see something like

core/node (master $% u=) % node -- nexist
module.js:472
    throw err;
    ^

Error: Cannot find module '/home/sam/w/core/node/nexist'

Except with a file name of --help!

Is it out of scope to fix this bug, too, in this PR @jBarz? Should I report this as a bug?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I take that back, you're correct. Using the -- shouldn't fail. I was misreading the PR ;-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still like to see the tests expanded to cover the -p option.

Copy link
Contributor Author

@jBarz jBarz Jan 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this PR:
"--" is treated as an unknown node option. Hence it is always passed on to v8.

After this PR:
"--" is treated as the end-of-options delimiter if and only if "--eval script" has been used prior to this option. Otherwise, treat it the same as before

I can change it to do what @sam-github says it must do:
"--" is treated as the end-of-options delimiter for all situations

Since node also uses a script name to indicate end-of-options, I would like to clarify what should happen in this case?
node --nodeopt1 script.js arg1 -- arg2

Should "--" be passed into the script or ignored?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

passed in, so that the script.js can use it to indicate the end of its options:

node --some-node-option script.js --some-script-option -- --not-a-script-option

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will commit some additional changes and tests as @jasnell mentioned above. Thanks for the feedback!

@bnoordhuis
Copy link
Member

silverwind pushed a commit that referenced this pull request Jan 24, 2017
When the double dash "--" appears after "-e <script>" on the
command line, it indicates the end of options and the beginning
of positional parameters for the script.

PR-URL: #10651
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
@silverwind
Copy link
Contributor

Thanks! Landed in 0a9f360. I'll set a semver-minor as I guess this qualifies as a feature.

@silverwind silverwind added the semver-minor PRs that contain new features and should be released in the next minor version. label Jan 24, 2017
@silverwind
Copy link
Contributor

@bnoordhuis sorry, totally missed your post while landing. I assume it will come out green anyways.

@addaleax
Copy link
Member

I think this can be closed given that it’s landed now

@addaleax addaleax closed this Jan 25, 2017
@silverwind
Copy link
Contributor

Thanks, forgot to close it. CI came out green 👍

@jBarz
Copy link
Contributor Author

jBarz commented Jan 25, 2017

@MylesBorins would it be possible to back-port this to v4.x ?
Edit: nevermind :-) I will open a PR for it myself and make sure it merges cleanly.

@jBarz jBarz deleted the issue9017 branch January 26, 2017 02:20
targos pushed a commit that referenced this pull request Jan 28, 2017
When the double dash "--" appears after "-e <script>" on the
command line, it indicates the end of options and the beginning
of positional parameters for the script.

PR-URL: #10651
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
@italoacasas italoacasas mentioned this pull request Jan 29, 2017
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 30, 2017
When the double dash "--" appears after "-e <script>" on the
command line, it indicates the end of options and the beginning
of positional parameters for the script.

PR-URL: nodejs#10651
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 30, 2017
When the double dash "--" appears after "-e <script>" on the
command line, it indicates the end of options and the beginning
of positional parameters for the script.

PR-URL: nodejs#10651
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
MylesBorins pushed a commit that referenced this pull request May 16, 2017
When the double dash "--" appears after "-e <script>" on the
command line, it indicates the end of options and the beginning
of positional parameters for the script.

PR-URL: #10651
Backport-PR-URL: #11013
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
MylesBorins pushed a commit that referenced this pull request May 18, 2017
When the double dash "--" appears after "-e <script>" on the
command line, it indicates the end of options and the beginning
of positional parameters for the script.

PR-URL: #10651
Backport-PR-URL: #11013
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
@MylesBorins MylesBorins mentioned this pull request May 23, 2017
MylesBorins added a commit that referenced this pull request Jun 6, 2017
This LTS release comes with 126 commits. This includes 40 which
are test related, 32 which are doc related, 12 which are
build / tool related and 4 commits which are updates to
dependencies.

Notable Changes:

* build:
  - support for building mips64el (nanxiongchao)
    #10991
* cluster:
  - disconnect() now returns a reference to the disconnected
    worker. (Sean Villars)
    #10019
* crypto:
  - ability to select cert store at runtime (Adam Majer)
    #8334
  - Use system CAs instead of using bundled ones (Adam Majer)
    #8334
  - The `Decipher` methods `setAuthTag()` and `setAAD` now return
    `this`. (Kirill Fomichev)
    #9398
  - adding support for OPENSSL_CONF again (Sam Roberts)
    #11006
  - make LazyTransform compabile with Streams1 (Matteo Collina)
    #12380
* deps:
  - upgrade libuv to 1.11.0 (cjihrig)
    #11094
  - upgrade libuv to 1.10.2 (cjihrig)
    #10717
  - upgrade libuv to 1.10.1 (cjihrig)
    #9647
  - upgrade libuv to 1.10.0 (cjihrig)
    #9267
* dns:
  - Implemented `{ttl: true}` for `resolve4()` and `resolve6()`
    (Ben Noordhuis)
    #9296
* process:
  - add NODE_NO_WARNINGS environment variable (cjihrig)
    #10842
* readline:
  - add option to stop duplicates in history (Danny Nemer)
    #2982
* src:
  - support "--" after "-e" as end-of-options (John Barboza)
    #10651
* tls:
  - new tls.TLSSocket() supports sec ctx options (Sam Roberts)
    #11005
  - Allow obvious key/passphrase combinations. (Sam Roberts)
    #10294

PR-URL: #13059
MylesBorins added a commit that referenced this pull request Jun 6, 2017
This LTS release comes with 126 commits. This includes 40 which
are test related, 32 which are doc related, 12 which are
build / tool related and 4 commits which are updates to
dependencies.

Notable Changes:

* build:
  - support for building mips64el (nanxiongchao)
    #10991
* cluster:
  - disconnect() now returns a reference to the disconnected
    worker. (Sean Villars)
    #10019
* crypto:
  - ability to select cert store at runtime (Adam Majer)
    #8334
  - Use system CAs instead of using bundled ones (Adam Majer)
    #8334
  - The `Decipher` methods `setAuthTag()` and `setAAD` now return
    `this`. (Kirill Fomichev)
    #9398
  - adding support for OPENSSL_CONF again (Sam Roberts)
    #11006
  - make LazyTransform compabile with Streams1 (Matteo Collina)
    #12380
* deps:
  - upgrade libuv to 1.11.0 (cjihrig)
    #11094
  - upgrade libuv to 1.10.2 (cjihrig)
    #10717
  - upgrade libuv to 1.10.1 (cjihrig)
    #9647
  - upgrade libuv to 1.10.0 (cjihrig)
    #9267
* dns:
  - Implemented `{ttl: true}` for `resolve4()` and `resolve6()`
    (Ben Noordhuis)
    #9296
* process:
  - add NODE_NO_WARNINGS environment variable (cjihrig)
    #10842
* readline:
  - add option to stop duplicates in history (Danny Nemer)
    #2982
* src:
  - support "--" after "-e" as end-of-options (John Barboza)
    #10651
* tls:
  - new tls.TLSSocket() supports sec ctx options (Sam Roberts)
    #11005
  - Allow obvious key/passphrase combinations. (Sam Roberts)
    #10294

PR-URL: #13059
andrew749 pushed a commit to michielbaird/node that referenced this pull request Jul 19, 2017
This LTS release comes with 126 commits. This includes 40 which
are test related, 32 which are doc related, 12 which are
build / tool related and 4 commits which are updates to
dependencies.

Notable Changes:

* build:
  - support for building mips64el (nanxiongchao)
    nodejs/node#10991
* cluster:
  - disconnect() now returns a reference to the disconnected
    worker. (Sean Villars)
    nodejs/node#10019
* crypto:
  - ability to select cert store at runtime (Adam Majer)
    nodejs/node#8334
  - Use system CAs instead of using bundled ones (Adam Majer)
    nodejs/node#8334
  - The `Decipher` methods `setAuthTag()` and `setAAD` now return
    `this`. (Kirill Fomichev)
    nodejs/node#9398
  - adding support for OPENSSL_CONF again (Sam Roberts)
    nodejs/node#11006
  - make LazyTransform compabile with Streams1 (Matteo Collina)
    nodejs/node#12380
* deps:
  - upgrade libuv to 1.11.0 (cjihrig)
    nodejs/node#11094
  - upgrade libuv to 1.10.2 (cjihrig)
    nodejs/node#10717
  - upgrade libuv to 1.10.1 (cjihrig)
    nodejs/node#9647
  - upgrade libuv to 1.10.0 (cjihrig)
    nodejs/node#9267
* dns:
  - Implemented `{ttl: true}` for `resolve4()` and `resolve6()`
    (Ben Noordhuis)
    nodejs/node#9296
* process:
  - add NODE_NO_WARNINGS environment variable (cjihrig)
    nodejs/node#10842
* readline:
  - add option to stop duplicates in history (Danny Nemer)
    nodejs/node#2982
* src:
  - support "--" after "-e" as end-of-options (John Barboza)
    nodejs/node#10651
* tls:
  - new tls.TLSSocket() supports sec ctx options (Sam Roberts)
    nodejs/node#11005
  - Allow obvious key/passphrase combinations. (Sam Roberts)
    nodejs/node#10294

PR-URL: nodejs/node#13059
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. process Issues and PRs related to the process subsystem. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.