version script is not failing when preversion script fails.


(Joe Pea) #1

What I Wanted to Do

I run tests in my preversion script. When I run npm version ... I expect that if preversion fails because of a failed test, then the preversion should quit and the version process should not continue.

What Happened Instead

If I run npm run preversion and it fails then echo $?shows a non-zero exit code as expected.

However, when I run npm version patch -m 'v%s' or similar, and the preversion part fails, the version process continues and eventually tries to publish the erroneous package to NPM.


Reproduction Steps

I have a branch here:

To reproduce, try the following after cloning and checking out that branch:

npm install
npm run test # note that it fails with exit code 255
npm run preversion # note that it fails with exit code 1
npm version patch -f -m "v%s" # it will not fail, it continues past preversion failure

Platform Info

$ npm --versions
{ infamous: '21.0.5',
  npm: '6.4.1',
  ares: '1.14.0',
  cldr: '33.1',
  http_parser: '2.8.0',
  icu: '62.1',
  modules: '64',
  napi: '3',
  nghttp2: '1.32.0',
  node: '10.9.0',
  openssl: '1.1.0i',
  tz: '2018e',
  unicode: '11.0',
  uv: '1.22.0',
  v8: '',
  zlib: '1.2.11' }
$ node -p process.platform

Request: move bugs back to
(Joe Pea) #2

If I change my scripts to the following, it still doesn’t work, and version script still runs:

"preversion": "npm test",
"version": "echo VERSION",
"postversion": "echo POSTVERSION",

I am expecting to not see the VERSION output, but I do. Note, I do see a non-zero exit code after npm version patch -f -m "v%s". Looks like postversion is not running after all, but I also expected version not to run.

Also note, it does create the commit, git log shows v21.0.6.

(Joe Pea) #3

Okay, apparently the -f option is what causes the problem. Without the -f option, the version script does not run after the failed preversion script, which is what I want.

This is not mentioned in the docs. This is what the docs say for step 1:

  1. Check to make sure the git working directory is clean before we get started. Your scripts may add files to the commit in future steps. This step is skipped if the --force flag is set.

It says that the -f option will allow us to skip the check for working directory is clean, which is great, that’s what I wanted!

But it does not mention that if the preversion script fails that it will continue to the version script regardless.

It would be great if that wasn’t the case, or if it at least was documented.

(Joe Pea) #4

The reason I specifically want to use -f is because I don’t want to have to deal with an unclean repo before publishing, and I set up my preversion script to be able to handle the stashing automatically, and the postversion to pop the stashed changes, to make things quicker.

I guess I need to make a new script for this purpose.

(Joe Pea) #5

Solved it for my case:

  • instead of having a preversion script, move it to another script like release:pre.
  • make release:patch, release:minor, etc, which call npm run release:pre && npm version patch -m "v%s", etc, respectively.

Now instead of running npm version patch -m "v%s" myself, I run npm run release:patch, etc.

It works great, and the release:pre script will always stop the process if it fails.

I left the version and postversion script in place, those work fine as expected (even with -f).

(Joe Pea) #6

There’s no reply here yet. Seems like isn’t being followed by NPM devs as closely as the GitHub issue tracker was. Maybe the issue tracker on GitHub should be restored for bugs.

(Kat Marchán) #7

We’re here. This one fell through the cracks. :slight_smile:

(Kat Marchán) #8

Update: This is working as expected. Because that’s what -f does.

(Joe Pea) #9

@zkat Ah, I was using -f so that I can run npm version even if the working tree has changes, otherwise it won’t. Looks like these two should be separate options then, as my preversion script will handle changes in the working tree, but I still don’t want version to succeed if preversion fails.