npm / npm/cli

[BUG] <package.json scripts do not create expected sub-shells>

Open
#5,038 0 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Needs Triage Release 8.x
Dominant language
JavaScript
Stars
10.1k
Forks
4.7k
Avg merge
2d 2h
Merged PRs (30d)
19

Description

Is there an existing issue for this?
  • I have searched the existing issues
This issue exists in the latest npm version
  • I am using the latest npm
Current Behavior

When using subshells in scripts to prevent working directory changes, it behaves as though no subshell exists and persists the shell current working directory changes. This appears like it might be a windows specific issue, as it works for my coworker on other platforms.

Expected Behavior

I would expect that when using sub-shells in package.json scripts, they work on windows.

Steps To Reproduce

Using any project with at least one subdirectory with files in it, add this script to the package.json and run it on a windows environment (I'm using a mingw64 git bash terminal).

script:
"example": "(cd <path to directory> && ls) && ls

$ npm run example

> example@0.1.0 example
> (cd subdir && ls) && ls

samplefile.txt
samplefile.txt

Nathan Pettorini@wf01-npettorini MINGW64 ~/npm-subshell-issue-example/example (main)
$ (cd subdir && ls) && ls
samplefile.txt
package.json  subdir/

Nathan Pettorini@wf01-npettorini MINGW64 ~/npm-subshell-issue-example/example (main)

You can see in this example output, when running directly through the terminal that command correctly discards the changes in working directory done in the subshell, so when the second ls is executed we're back at the root level, but the same code when run via package script does not behave this way, and both ls commands print out the contents of the subdirectory.

To be clear, the expected output is

samplefile.txt
package.json  subdir/

The sample above was made with this simple repo: https://github.com/NathanPepperoni/npm-subshell-issue-example

Environment
  • npm: 8.11.0 (I know this technically isn't the latest, but its only 1 off and its a pain in the butt to update this machine without nvm, of which no current node package uses anything beyond 8.11.0)
  • Node.js: 16.15.1
  • OS Name: Windows 10
  • System Model Name: Lenovo X1
  • npm config:
; "user" config from C:\Users\Nathan Pettorini\.npmrc

//registry.npmjs.org/:_authToken = (protected)
cache = "C:\\Users\\Nathan~1\\AppData\\Roaming\\npm-cache"
msvs_version = "2017"
node-options = "--max_old_space_size=4096"

; "project" config from C:\Users\Nathan Pettorini\playr\workspace\.npmrc

@WahooFitness:registry = "https://npm.pkg.github.com"
//npm.pkg.github.com/:_authToken = (protected)

; node bin location = C:\Program Files\nodejs\node.exe
; cwd = C:\Users\Nathan Pettorini\playr\workspace
; HOME = C:\Users\Nathan Pettorini
; Run `npm config ls -l` to show all defaults.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue from the linked example using the package.json script and npm run example in a Windows 10 Git Bash environment. Compare the script output with direct terminal execution; done means the subshell leaves the working directory unchanged and the second ls lists package.json and subdir/ from the project root.

Written by the indexing model from the issue text.

Assessment

Tech stack
bash, javascript, nodejs
Domain
cli, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.