cli icon indicating copy to clipboard operation
cli copied to clipboard

[BUG] exit codes not being set properly in node v20

Open ericcornelissen opened this issue 3 years ago • 18 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

This issue exists in the latest npm version

  • [X] I am using the latest npm

Current Behavior

Running npm audit (seemingly) always exists with a 0 exit code, even if a vulnerability is found (with a severity higher than configured by the audit-level).

In contrast, [email protected] ([email protected]) exits with a 1 if a vulnerability is found (with a severity higher than configured by the audit-level).

Expected Behavior

Per the Exit Code section:

[...]

If vulnerabilities were found the exit code will depend on the audit-level config.

Steps To Reproduce

  1. Clone https://github.com/ericcornelissen/shescape/tree/dbaa0fd36af4fd0439af87548ce710468f25cb18
  2. Run npm audit
  3. Observe a warning for (at least) the high severity vulnerability GHSA-9c47-m6qq-7p4h
  4. Run echo $?
  5. Observe a 0 being printed

Environment

  • npm: 9.6.5
  • Node.js: v20.0.0
  • OS Name: Ubuntu 22.04.2 LTS
  • System Model Name: custom
  • npm config:
; "user" config from ~/.npmrc

update-notifier = false 

; "project" config from ~/workspace/shescape/.npmrc

lockfile-version = "3" 
save-exact = true 
save-prefix = "" 

; node bin location = ~/.nvm/versions/node/v20.0.0/bin/node
; node version = v20.0.0
; npm local prefix = ~/Documents/workspace/shescape
; npm version = 9.6.5
; cwd = ~/Documents/workspace/shescape
; HOME = ~
; Run `npm config ls -l` to show all defaults.

ericcornelissen avatar Apr 25 '23 20:04 ericcornelissen

The same happens when running workspace scripts: npm run build -w test If the script fails, npm still returns with exit code 0.

I have made a reproduction repository: https://github.com/darthmaim/node-exit-code-test I also added a workflow to test multiple node/npm versions: https://github.com/darthmaim/node-exit-code-test/actions/runs/4837883808

It only fails on node 20

This probably breaks all CI jobs when using worskpaces and node 20, so maybe this should be higher priority?

darthmaim avatar Apr 29 '23 08:04 darthmaim

Hi - thanks for looking into this.

We're using workspaces, npm 9.6.6 and node 20.1.0 and this is currently breaking our CI jobs

ghost avatar May 10 '23 06:05 ghost

This should be related to this changes on node side https://github.com/nodejs/node/issues/48027

process.exit will be called with undefined here: https://github.com/npm/cli/blob/829503b804f31b63a405ece48ea265b641b43392/lib/utils/exit-handler.js#L220-L223 and with node 20, this will override the process.exitCode set earlier

https://github.com/npm/cli/blob/09b58e4cabc2202aab878c89fc6930e4a085fd3a/lib/commands/run-script.js#L200-L222

srapilly avatar May 16 '23 07:05 srapilly

@MichaelBitard the previous comment shows the root cause here. v20 subtly changed how exitCode setting works.

The process.exit in the exit handler needs to be smarter about either not passing anything and setting process.exitCode if it needs to, or casting that to 0 if it's not already set.

wraithgar avatar May 17 '23 14:05 wraithgar

Ok thanks, from what I see err is always undefined in our cases (running a script that fails in a workspace), but process.exitCode is set to a non 0 value.

I should rely on it.

If I change line 134: https://github.com/npm/cli/blob/829503b804f31b63a405ece48ea265b641b43392/lib/utils/exit-handler.js#L130-L140

to let exitCode = process.exitCode then I get the correct behavior.

This seems ok to you?

MichaelBitard avatar May 17 '23 14:05 MichaelBitard

process.exitCode || 1 would ensure it’s always nonzero, which seems like what’s desired.

ljharb avatar May 17 '23 14:05 ljharb

I think you mean process.exitCode || 0 because it should exit gracefully on most occasions no? or is exitHandler always called on error?

MichaelBitard avatar May 17 '23 14:05 MichaelBitard

oh, if it’s ok to sometimes have zero then your original suggestion is what’s wanted, i was just matching the current semantics,

ljharb avatar May 17 '23 15:05 ljharb

Yes I think that's it, if everything works well, then exitHandler is called and process.exitCode is undefined, so we don't want to force it to 1 here

MichaelBitard avatar May 17 '23 15:05 MichaelBitard

Hey guys, as of node v20.6.1 (npm v9.8.1) it looks like this issue has come back. Using node v18.17.1 (npm v9.6.7) works as expected.

claudiodea avatar Dec 21 '23 01:12 claudiodea

This is no longer an issue in 22.3.0 at least. I have no idea when it was fixed. This has happened multiple times in Node versions. I wonder if it is something that is actually tested now...

happycollision avatar Jun 19 '24 21:06 happycollision

I created a Gist to help test your particular version of NPM. It also protects you from harm if an error is found by adding a small syntax error to the top of your package.json file to render it useless for running npm run <script>.

https://gist.github.com/happycollision/445c5e3ff6fb6c6eaa1161a441bb7553

happycollision avatar Jul 19 '24 19:07 happycollision

I'm experiencing this in v20.5.1+ and v22.5.1. Only by reverting back to 18.20.4 did the exit codes start working correctly again.

BastianTrifork avatar Jul 23 '24 13:07 BastianTrifork

I'm experiencing this in v20.5.1+ and v22.5.1. Only by reverting back to 18.20.4 did the exit codes start working correctly again.

I've been experiencing the same problem in my CI builds (they'd always pass with broken tests/checks).

Reverting back to 18.20.4 (latest LTS) solved the issue.

My setup is basically this:

package.json (in a projects/my-project workspace)

   "test:main": "vitest run",
   "test:ci": "npm run test:main",

In node 20.10.0, the following command will always return an exit code 0 (regardless the actual result of the tests):

npm run test:ci -w projects/my-project

While in the same version this would return the exit code from vitest:

npm run test:main -w projects/my-project

(btw you can check the exit code of any command with echo $?)

Somehow, calling the test:main from within another npm script creates the problem with the exit codes.

Switching the node version to 18.20.4 makes this command npm run test:ci -w projects/my-project return the exit code of the other npm script (which in this case, is the result of calling vitest, which returns 1 if there're broken tests).

damianmr avatar Aug 15 '24 08:08 damianmr

@damianmr we had a very similar issue where updating the node version would lead to missing non-zero exit code only when being called indirectly - so like your test:main/test:ci scripts.

It turned out that we had npm (the actual package) as a transitive dependency, meaning there was an npm binary at .node_modules/.bin/npm, which was outdated (I think version 6? Something like that). So what happened was the following:

  1. npm run test:main uses npm from the $PATH, calls vitest from node_modules, everything is fine.
  2. npm run test:ci uses npm from the $PATH, calls npm from node_modules, which is outdated and has the exit code issue

This behavior only occured when updating from node 18 to 20 (with the corresponding npm versions), so it looked like the newer npm version was the culprit, when it actually was only partly at fault for suddenly using the local npm version for these multi-step scripts.

Maybe you have the same issue? We ended up "fixing" this by just adding npm@10 to our devDependencies.

tiborpilz avatar Aug 15 '24 09:08 tiborpilz

@damianmr we had a very similar issue where updating the node version would lead to missing non-zero exit code only when being called indirectly - so like your test:main/test:ci scripts.

It turned out that we had npm (the actual package) as a transitive dependency, meaning there was an npm binary at .node_modules/.bin/npm, which was outdated (I think version 6? Something like that). So what happened was the following:

  1. npm run test:main uses npm from the $PATH, calls vitest from node_modules, everything is fine.
  2. npm run test:ci uses npm from the $PATH, calls npm from node_modules, which is outdated and has the exit code issue

This behavior only occured when updating from node 18 to 20 (with the corresponding npm versions), so it looked like the newer npm version was the culprit, when it actually was only partly at fault for suddenly using the local npm version for these multi-step scripts.

Maybe you have the same issue? We ended up "fixing" this by just adding npm@10 to our devDependencies.

Thank you for the details, I'll have to investigate if I have npm as a transitive dependency.

damianmr avatar Aug 15 '24 10:08 damianmr

You're a hero @tiborpilz ! It turns out semantic-release has npm as a dependency, and their older version was causing issues with the exit codes.

Adding npm to devDependencies fixed it!

BastianTrifork avatar Aug 26 '24 13:08 BastianTrifork

If anyone still gets this issue check that an older npm version isn't being installed locally in your node_modules folder. We were having a issue due to a dependency install a old version of npm that wasn't handling node 20+ process exit codes correctly.

A fix was to manually specify npm version in your package.json "dependencies": { "npm": "^11.3.0" }, "resolutions": { "npm": "^11.3.0", }

jockjocko avatar Jun 18 '25 14:06 jockjocko