Skip to content

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
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
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.

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.

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.

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.

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

src/node.cc Outdated
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

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.

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
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.

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.