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

child_process spawn does not pass environment key-value pairs on Windows #34667

Closed
beliayeu opened this issue Aug 7, 2020 · 4 comments
Closed
Labels
child_process Issues and PRs related to the child_process subsystem. question Issues that look for answers. windows Issues and PRs related to the Windows platform.

Comments

@beliayeu
Copy link

beliayeu commented Aug 7, 2020

  • Version: 12.18.3
  • Platform: 64-bit Windows

What steps will reproduce the bug?

require('child_process').spawn('node', ['-pe', 'process.env.PATH'], {
  stdio: 'inherit',
  shell: true,
  env: {
    ...process.env,
    PATH: process.env.PATH + require('path').delimiter + __dirname,
  }
});

Actual behavior: PATH environment variable is not updated in the spawned process.

How often does it reproduce? Is there a required condition?

Always on Windows. It is not an issue on OSX.

What is the expected behavior?

PATH environment variable should be updated in the spawned process.

What do you see instead?

add a fake env variable to the env object passed to spawn

require('child_process').spawn('node', ['-pe', 'process.env.PATH'], {
  stdio: 'inherit',
  shell: true,
  env: {
    ...process.env,
    PATH: process.env.PATH + require('path').delimiter + __dirname,
    FAKEENV: 'FAKEVALUE',
  }
});
@richardlau
Copy link
Member

This is probably a duplicate of #20605 and the copied env object contains a variant of PATH with different casing (e.g. Path).

The documentation was updated (#32091) to say:

The command lookup will be performed using options.env.PATH environment variable if passed in options object, otherwise process.env.PATH will be used. To account for the fact that Windows environment variables are case-insensitive Node.js will lexicographically sort all env keys and choose the first one case-insensitively matching PATH to perform command lookup. This may lead to issues on Windows when passing objects to env option that have multiple variants of PATH variable.

@bnoordhuis bnoordhuis added child_process Issues and PRs related to the child_process subsystem. question Issues that look for answers. windows Issues and PRs related to the Windows platform. labels Aug 10, 2020
@bnoordhuis
Copy link
Member

I'll close this as answered. Let me know if it should be reopened.

@beliayeu
Copy link
Author

thanks, @richardlau, for providing the root cause.
what is your recommendation how to change PATH environment variable? should different casing variants be deleted during object copying? or is there a better API how to do it?

  env: {
    ...process.env,
    Path: undefined,
    PATH: process.env.PATH + require('path').delimiter + __dirname,
  }

cc: @bnoordhuis

@richardlau
Copy link
Member

thanks, @richardlau, for providing the root cause.
what is your recommendation how to change PATH environment variable? should different casing variants be deleted during object copying? or is there a better API how to do it?

  env: {
    ...process.env,
    Path: undefined,
    PATH: process.env.PATH + require('path').delimiter + __dirname,
  }

cc: @bnoordhuis

There should only be one variant of PATH in process.env as it has accessors to handle case insensitivity on Windows. So something like this should work on Windows:

  env: {
    ...process.env,
    [Object.keys(process.env).find(x => x.toUpperCase() === 'PATH')]: process.env.PATH + require('path').delimiter + __dirname,
  }

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. question Issues that look for answers. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

No branches or pull requests

3 participants