镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

@beliayeu
  • 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',
  }
});

Activity

  1. richardlau commented on Aug 7, 2020

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

  2. added
    child_processIssues and PRs related to the child_process subsystem.
    questionIssues asking questions about Node.js.
    windowsIssues and PRs related to the Windows platform.
    on Aug 10, 2020
  3. bnoordhuis commented on Aug 10, 2020

    @bnoordhuis
    Member

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

  4. beliayeu commented on Aug 12, 2020

    @beliayeu
    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

  5. richardlau commented on Aug 12, 2020

    @richardlau
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    child_processIssues and PRs related to the child_process subsystem.questionIssues asking questions about Node.js.windowsIssues and PRs related to the Windows platform.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions