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

FR: child_process.exec(cmd, { additionalEnv }) #14823

Closed
refack opened this issue Aug 14, 2017 · 15 comments
Closed

FR: child_process.exec(cmd, { additionalEnv }) #14823

refack opened this issue Aug 14, 2017 · 15 comments
Labels
child_process Issues and PRs related to the child_process subsystem. feature request Issues that request new features to be added to Node.js. help wanted Issues that need assistance from volunteers or PRs that need help to proceed.

Comments

@refack
Copy link
Contributor

refack commented Aug 14, 2017

  • Version: *
  • Platform: *
  • Subsystem: child_process

child_process.exec(cmd, { env: Object.assign({}, process.env, {NEW_VAR:1}) }) is a very common pattern. IMHO adding an { additionalEnv } option that implements this pattern, will make the API more complete, and less error prone.

@refack refack added child_process Issues and PRs related to the child_process subsystem. feature request Issues that request new features to be added to Node.js. help wanted Issues that need assistance from volunteers or PRs that need help to proceed. mentor-available labels Aug 14, 2017
@vsemozhetbyt
Copy link
Contributor

FWIW, in Node.js 8.3.0 we can do:

child_process.exec(cmd, { env: { ...process.env, NEW_VAR: 1 } });

@cjihrig
Copy link
Contributor

cjihrig commented Aug 14, 2017

I've thought about this in the past. It seems like an awkward API to me though. I like @vsemozhetbyt's solution moving forward, and Object.assign() (or similar) for older versions.

@refack
Copy link
Contributor Author

refack commented Aug 14, 2017

Ack that object spreads make this more succinct.
IMHO there is still the issue that the current API is too close the the POSIX/WIN32 system call, and is not intuitive at the application level.
I think that the case of adding vars is much more common than the case of replacing the entire env...
Maybe a cleaner option would be to add a { envAdditive: true } flag (with a default of false) that will treat env as additions instead of replacement.

@bnoordhuis
Copy link
Member

I think that the case of adding vars is much more common than the case of replacing the entire env...

C programmers will disagree with you.

This feature request seems like a solution in search of a problem and as to the syntax, I agree with Colin's choice of words: awkward.

@refack
Copy link
Contributor Author

refack commented Aug 14, 2017

I think that the case of adding vars is much more common than the case of replacing the entire env...

C programmers will disagree with you.

This feature request seems like a solution in search of a problem and as to the syntax, I agree with Colin's choice of words: awkward.

That's sort of my point that (C programmes) != (node programmers).
The struggle is real #14822 + #13390, and that's node core code written (or at least reviewed) by node core coders.

I don't have an elegant solution for syntax. Since the API was designed to mimic the system calls, everything would look awkward 🤷‍♂️ The most elegant solution would have been to change the semantics of { env } but that's out of the question.
But I am trying to think what a programmer who is node-as-first-language would think...

@refack
Copy link
Contributor Author

refack commented Aug 14, 2017

🤔 trying to be creative:

const aes = child_process.addativeEnvSymbol;
const env = { [aes]: true, NEW_VAR:1 }
child_process.exec(cmd, { env })

@joaolucasl
Copy link
Contributor

joaolucasl commented Aug 15, 2017

The spread operator is a pretty good solution for this, imho, but I like the idea of an option to ease the work of merging too.

I thought of something like:

const env = { /* ... */ };
child_process.exec(cmd, { env, mergeEnvs: true });

mergeEnvs <boolean> If true, merges the env property with process.env. (Default: false).

The naming might be tricky to get right, since to be descriptive enough it would have to be longer lol

Edit: (Happy to PR if this goes forward 😄)

@evanlucas
Copy link
Contributor

I feel like this is something that could easily be done in an npm package

@refack
Copy link
Contributor Author

refack commented Aug 15, 2017

After reading some POSIX design stories, I tend to agree that this should stay as is for child_process.spawn but now I'm worried about the mismatch with cluster.fork

@gibfahn
Copy link
Member

gibfahn commented Aug 15, 2017

I think a common function that adds something to the existing process.env like:

child_process.exec(cmd, { env: common.envPlus({NEW_VAR:1}) })

would make sense, and would hopefully solve the problem we have in tests, which is that adding something to the existing env isn't exactly intuitive.

Whether this is something that the larger community would need is a different question though, and can probably be dealt with separately.

@cjihrig
Copy link
Contributor

cjihrig commented Aug 15, 2017

Would common.envPlus() just be this?

function envPlus(env) {
  return Object.assign({}, process.env, env);
}

If so, we could probably live without it.

@refack
Copy link
Contributor Author

refack commented Aug 15, 2017

For test/common I was thinking of common.exec as there might be a wider pattern in common use, but it requires an audit.

@sindresorhus
Copy link

I feel like this is something that could easily be done in an npm package

Check out execa which already does this by default. (extendEnv option to turn it off)

@gibfahn
Copy link
Member

gibfahn commented Aug 15, 2017

Raised a PR for discussion purposed: #14845

Not 100% sold on having the function, but it's easier to discuss over code.

gibfahn added a commit to gibfahn/node that referenced this issue Aug 17, 2017
Add a helper function to provide an easy way to modify the
environment passed to child processes.

Fixes: nodejs#14823
@refack
Copy link
Contributor Author

refack commented Aug 26, 2017

Closed for no consensus

@refack refack closed this as completed Aug 26, 2017
BridgeAR pushed a commit that referenced this issue Sep 3, 2017
PR-URL: #14845
Fixes: #14823
Refs: #14822
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
addaleax pushed a commit to addaleax/ayo that referenced this issue Sep 5, 2017
PR-URL: nodejs/node#14845
Fixes: nodejs/node#14823
Refs: nodejs/node#14822
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
MylesBorins pushed a commit that referenced this issue Sep 10, 2017
PR-URL: #14845
Fixes: #14823
Refs: #14822
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
MylesBorins pushed a commit that referenced this issue Sep 11, 2017
PR-URL: #14845
Fixes: #14823
Refs: #14822
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
MylesBorins pushed a commit that referenced this issue Sep 12, 2017
PR-URL: #14845
Fixes: #14823
Refs: #14822
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
gibfahn added a commit to gibfahn/node that referenced this issue Sep 22, 2017
PR-URL: nodejs#14845
Backport-PR-URL: nodejs#15557
Fixes: nodejs#14823
Refs: nodejs#14822
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
MylesBorins pushed a commit that referenced this issue Sep 22, 2017
PR-URL: #14845
Backport-PR-URL: #15557
Fixes: #14823
Refs: #14822
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
MylesBorins pushed a commit that referenced this issue Sep 26, 2017
PR-URL: #14845
Backport-PR-URL: #15557
Fixes: #14823
Refs: #14822
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
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. feature request Issues that request new features to be added to Node.js. help wanted Issues that need assistance from volunteers or PRs that need help to proceed.
Projects
None yet
Development

No branches or pull requests

8 participants