yarnpkg / yarn

The 1.x line is frozen - features and bugfixes now happen on https://github.com/yarnpkg/berry
https://classic.yarnpkg.com
Other
41.4k stars 2.73k forks source link

npm_config_argv reflects first instead of last command in a chain #8905

Open aomarks opened 1 year ago

aomarks commented 1 year ago

(This affects yarn 1.x, so I realize this is unlikely to be changed at this point, but I just wanted to note the discrepancy so that we have a place to discuss/reference).

Given a package.json:

{
  "scripts": {
    "first:yarn": "yarn run second --extra",
    "first:npm": "npm run second --extra",
    "second": "echo $npm_config_argv"
  },
}

Then running yarn run first:yarn (with yarn@1.22.19) prints:

{"remain":[],"cooked":["run","first:yarn"],"original":["run","first:yarn"]}

Whereas running npm run first:npm (with npm@6.14.17) prints:

{"remain":[],"cooked":["run","second","--extra"],"original":["run","second","--extra"]}

So, the npm behavior is that npm_config_argv reflects the current script regardless of whether that script was invoked as part of a chain of scripts. But the yarn behavior is that npm_config_argv reflects the first script in the chain. In other words, once npm_config_argv has been set by yarn, it never gets updated, even as different scripts are executed.

I believe that yarn should follow the same behavior as npm.

The context for this issue is https://github.com/google/wireit, which needs to understand which arguments have been passed to an npm or yarn script invocation, for enabling its watch mode and understanding the extra arguments to pass to child processes. If one yarn script invokes another, we therefore lose the ability to understand which arguments have been set for the second script. (wireit issue about this behavior: https://github.com/google/wireit/issues/545)

(This issue does not affect yarn 3.x, because yarn 3.x doesn't set the npm_config_argv argument at all. Instead it passes all arguments after yarn run <script> to the argv of the child, which works fine for our purposes.)