Skip to content

Add {shell: 'auto'} to child_process.spawn - #6784

Closed
timdp wants to merge 1 commit into
nodejs:masterfrom
timdp:child-process-shell-auto
Closed

timdp wants to merge 1 commit into
nodejs:masterfrom
timdp:child-process-shell-auto

Conversation

@timdp

@timdp timdp commented May 16, 2016

Copy link
Copy Markdown
Contributor
Checklist
  • tests and code linting passes (see below)
  • a test and/or benchmark is included
  • documentation is changed or added
  • the commit message follows commit guidelines
Affected core subsystem(s)

child_process

Description of change

Discussed in #6671.

Synopsis: On Windows, Node/uv doesn't take PATHEXT into account, making spawning fail unexpectedly if you don't pass {shell: true}. However, that option makes spawning roughly twice as slow. Hence, I'm adding {shell: 'auto'} to guess the need to spawn a shell from the extension on Windows.

This implies that you can no longer pass 'auto' as the name of a shell, which breaks backward compatibility. Alternatives include:

  • Allowing {shell: 1} and checking if the type is 'number'.
  • Introducing a new option such as autoShell.
  • Defaulting to 'auto' if shell is undefined.

Sidenote: I'm adding a test but:

  • vcbuild test makes my Windows machine freeze. Running the test directly worked though.
  • I didn't test on *NIX yet.

@nodejs-github-bot nodejs-github-bot added the child_process Issues and PRs related to the child_process subsystem. label May 16, 2016
@addaleax addaleax added the windows Issues and PRs related to the Windows platform. label May 16, 2016
Comment thread lib/child_process.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This logic could be moved down to https://github.com/timdp/node/blob/0bd9febb52756a8e2069d279703601bc017d5ab0/lib/child_process.js#L335, where the presence of the shell options and the windows check are already done for you.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I considered that at first, but since the statement can make options.shell become false, that would mean that you'd have to break out of the if afterwards.

@cjihrig

cjihrig commented May 16, 2016

Copy link
Copy Markdown
Contributor

I'm personally -1 for adding this to core, but as I don't use Windows, I'll defer to @nodejs/platform-windows.

@timdp

timdp commented May 16, 2016

Copy link
Copy Markdown
Contributor Author

@cjihrig I'm not a huge fan of it myself, but the performance difference is striking. I wish I could think of a more elegant solution.

@timdp
timdp force-pushed the child-process-shell-auto branch from 0bd9feb to a75cb44 Compare May 16, 2016 12:49
On Windows, Node/uv doesn't take PATHEXT into account, making
spawning fail unexpectedly if you don't pass {shell: true}.
However, that option makes spawning roughly twice as slow.
Hence, I'm adding {shell: 'auto'} to guess the need to spawn a
shell from the extension on Windows.

This implies that you can no longer pass 'auto' as the name of a
shell, which breaks backward compatibility. Alternatives include:

- Allowing {shell: 1} and checking if the type is 'number'.
- Introducing a new option such as autoShell.
- Defaulting to 'auto' if shell is undefined.

Sidenote: I'm adding a test but:

- vcbuild test makes my Windows machine freeze. Running the test
  directly worked though.
- I didn't test on *NIX yet.
@timdp
timdp force-pushed the child-process-shell-auto branch from a75cb44 to 3249965 Compare May 16, 2016 13:03
@seishun

seishun commented May 16, 2016

Copy link
Copy Markdown
Contributor

-1 Easily done from userland.

@timdp

timdp commented May 16, 2016

Copy link
Copy Markdown
Contributor Author

@seishun The shell option also could've been done from userland. That's how we ended up with win-spawn, cross-spawn, execa, etc. It's about striking a good balance.

@seishun

seishun commented May 16, 2016

Copy link
Copy Markdown
Contributor

The shell option also could've been done from userland.

And it should have been, since what Node.js has now is unportable.

That's how we ended up with win-spawn, cross-spawn, execa, etc.

So you agree that it was a bad idea?

@timdp

timdp commented May 16, 2016

Copy link
Copy Markdown
Contributor Author

And it should have been, since what Node.js has now is unportable.

I agree, so I'm trying to fix it.

So you agree that it was a bad idea?

I mainly think it's insufficient as a solution. Either you make it a hassle-free API that takes care of all the platform-specific nonsense internally, or you expose a bare-bones API and hope that someone comes up with a wrapper. The former makes Node bigger, the latter is cumbersome because in the end, everyone's going to want that wrapper, raising the question why it isn't in the core API to begin with. Hence this PR.

@seishun

seishun commented May 16, 2016

Copy link
Copy Markdown
Contributor

Either you make it a hassle-free API that takes care of all the platform-specific nonsense internally

You can't really translate bash to cmd, so it would only pretend to take care of it (which it kinda does now by using a platform-specific shell).

I'm trying to fix it.

How does this help the portability problem?

@timdp

timdp commented May 16, 2016

Copy link
Copy Markdown
Contributor Author

You can't really translate bash to cmd, so it would only pretend to take care of it (which it kinda does now by using a platform-specific shell).

The core APIs are platform-agnostic, right? I don't see any reason to mirror the process API after bash, cmd, or any other shell, or OS in general.

How does this help the portability problem?

It frees developers from having to write additional code to handle different executable types, thereby exposing a single API that will always pick the best execution strategy for the given command. Like I mentioned originally, I'd even consider treating undefined as 'auto' so that you don't need to pass it. Ultimately, that would bring the exposed behavior on Windows closer to that on *NIX: just run the darned program and don't bother me with how you do it; if that's not portable, I don't know what is. I feel that that's how a third-party developer should approach the API, and right now, with or without specifying shell, it's not really working out.

@Fishrock123

Copy link
Copy Markdown
Contributor

As noted above, this seems easily done in userland. This is a windows-only thing too, right?

I mean generally if you are passing stuff to spawn you should probably know what you are doing with it anyways, it's not exactly safe to spawn arbitrary child processes as I'm sure you are aware.

@timdp

timdp commented May 16, 2016

Copy link
Copy Markdown
Contributor Author

@Fishrock123 Then the shell option shouldn't exist at all, right? I don't think the "it can be done in userland" argument holds up. Basically anything that's JS can be done in userland, so there's a lot more we should throw out if that's the criterion. APIs are there to facilitate common operations, and on Windows, this is unfortunately a commonly encountered issue.

@cjihrig

cjihrig commented May 16, 2016

Copy link
Copy Markdown
Contributor

Just a tiny bit of history. The shell option wasn't created specifically for spawn(). It was already being done for exec(). It was weird that the same functionality didn't exist for spawn(), so the logic was just moved around, and the option added.

@jasnell

jasnell commented May 16, 2016

Copy link
Copy Markdown
Member

@timdp ... the criterion as I've come to understand is, "anything that's not currently being done that can be done in userland..."

Personally I'm +0 on this, don't really have any major objections but also not seeing it as being critical.

FWIW, another alternative for the value to avoid the theoretical auto name collision would be to use a symbol... e.g:

Inside child_process: exports.kAuto = Symbol('auto');

Then in the app: {shell: cp.kAuto}

@Fishrock123

Copy link
Copy Markdown
Contributor

I'm just not fond of having a magic spawning option that is intended for generated input to be added first-class. (If I understand it's usefulness correctly.)

@jasnell

jasnell commented May 17, 2016

Copy link
Copy Markdown
Member

@bnoordhuis ... given that you were the one engaging on this in the original thread (#6671) I'd love to get your thoughts.

@timdp

timdp commented May 17, 2016

Copy link
Copy Markdown
Contributor Author

@jasnell If "anything that's not currently being done that can be done in userland" is the unwritten rule, then I'm all for moving it to an npm module. I was going to make the comparison to having to depend on request vs. got vs. node-fetch vs. whatever, but I guess choice is good then.

Which, for me, raises the question whether future versions of Node should lose more core stuff in favor of npm. I'm a big fan of micromodules if the core APIs that they get built upon are lean and consistent. But I digress.

@bnoordhuis

Copy link
Copy Markdown
Member

@jasnell Personally, I'm -1: it's not strictly necessary and it's a platform-specific hack. Its redeeming feature is that it has a positive impact on spawn times (but only on Windows.)

@sam-github

Copy link
Copy Markdown
Contributor

I'm -1 on this, too, if you are about to type [shell: auto} into your code, you are already thinking about portability, and in that case, you could pass {shell: process.platform == "win32"}, couldn't you?

@jasnell

jasnell commented May 18, 2016

Copy link
Copy Markdown
Member

@timdp ... I think that answers it then. Given the -1's it's not likely that this would make it in. I really do appreciate you taking the time to put the PR together. I know it's can be a bit of a hassle, especially when it doesn't end up landing but the contribution is very welcome and I hope it doesn't discourage you from making future contributions!

I'm going to go ahead and close the PR.

@jasnell jasnell closed this May 18, 2016
@timdp

timdp commented May 19, 2016

Copy link
Copy Markdown
Contributor Author

@jasnell No problem. I might bug @IndigoUnited to add this to cross-spawn instead though. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

child_process Issues and PRs related to the child_process subsystem. windows Issues and PRs related to the Windows platform.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants