Angular-cli: Investigate why `spawnSync` without `shell: true` results in ENOENT for npm/yarn in some windows systems

Created on 8 Nov 2019  路  6Comments  路  Source: angular/angular-cli

All 6 comments

From: https://nodejs.org/dist/latest-v10.x/docs/api/child_process.html#child_process_spawning_bat_and_cmd_files_on_windows

The importance of the distinction between child_process.exec() and child_process.execFile() can vary based on platform. On Unix-type operating systems (Unix, Linux, macOS) child_process.execFile() can be more efficient because it does not spawn a shell by default. On Windows, however, .bat and .cmd files are not executable on their own without a terminal, and therefore cannot be launched using child_process.execFile(). When running on Windows, .bat and .cmd files can be invoked using child_process.spawn() with the shell option set, with child_process.exec(), or by spawning cmd.exe and passing the .bat or .cmd file as an argument (which is what the shell option and child_process.exec() do). In any case, if the script filename contains spaces it needs to be quoted.

In order not to require shell in Windows. We'd need to change the spawn command to the below when running on windows

    const { status, stderr, stdout, error } = child_process_1.spawnSync('cmd.exe', ['/c', packageManager, ...installArgs, ...extraArgs], {
        stdio: 'pipe',
        encoding: 'utf8',
        cwd,
    });

That being said, I don't think we should do anything (ie: leave shell: true) as based on the docs, this is what shell option does.

When running on Windows, .bat and .cmd files can be invoked using child_process.spawn() with the shell option set, with child_process.exec(), or by spawning cmd.exe and passing the .bat or .cmd file as an argument (which is what the shell option and child_process.exec() do).

SGTM, great work getting to the bottom of this!

npm is a batch file on windows?

Looks like it:

PS C:\Users\kamik> get-command npm

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     npm.cmd                                            0.0.0.0    C:\Program Files\nodejs\npm.cmd


PS C:\Users\kamik> get-command yarn

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     yarn.cmd                                           0.0.0.0    C:\Program Files (x86)\Yarn\bin\yarn.cmd

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings