Yarn: Run postinstall after package upgrades

Created on 9 Mar 2017  ·  20Comments  ·  Source: yarnpkg/yarn

Do you want to request a feature or report a bug?
kind of neither, behavioral change

What is the current behavior?
After running a command like yarn upgrade {pkg} or yarn upgrade-interactive, the npm postinstall script is not being run.

What is the expected behavior?
I would expect the postinstall script to be run, since yarn is effectively installing an updated version of one or more packages.

Please mention your node.js, yarn and operating system version.
yarn 0.21.3, mac os latest

cat-bug good first issue help wanted needs-confirmation needs-repro-script triaged

Most helpful comment

I would like this to be reopened, since the issue was never resolved. It is not working on yarn version 1.7.1.

Our usecase is to patch a library via the postinstall. This then gets overwritten everytime we run yarn upgrade on an unrelated package, and will cause the application to crash without the patch. The patch then needs to be reapplied with yarn again.

Ideally yarn upgrade would respect postinstall, or a new lifecycle hook would be added, e.g. postupgrade.

All 20 comments

@evan-scott-zocdoc is this still a problem?

@BYK not sure, sorry. We moved off yarn internally.

@BYK I'd say yes. It's not really a problem, it's just a nice feature that would be cool to have if there are no downsides to it. postinstall is run after both yarn and yarn add, it'd be cool to run it after upgrade too.

@jakubzitny thanks. It feels almost like a bug to me but since there are no repro steps, I can't verify this issue. Do you have a specific case at hand that you can share with us?

since there are no repro steps

Just upgrade a package that has "postinstall" defined in the package.json. Like this one: https://github.com/jwhitmarsh/postinstall-test

You could try starting it at this commit: 9e0ca63f40fdb578c01dd6bf6424273aa7e3bb3b
And then upgrading to this commit: a15f70fc414ab0c68920bed77b40c8a56cd0ad23

Since upgrade internally calls add which extends install it is actually surprising that these scripts don't get run.

It should happen during the "Rebuilding all packages" phase.

Actually, if I yarn add the linked repo above by @evan-scott-zocdoc then yarn upgrade --verbose I can see the script being run (yarn v1.3.2)

[4/4] 📃  Rebuilding all packages...
⠁
⠁
⠁
[1/3] ⡀ fsevents
[-/3] ⡀ waiting...
[-/3] ⡀ waiting...
[-/3] ⡀ waiting...
verbose 11.344 node-pre-gyp info it worked if it ends with ok
node-pre-gyp info using [email protected]
[3/3] ⠂ postinstall-test: src/index.js -> lib/index.js
[-/3] ⠂ waiting...
[-/3] ⠂ waiting...
[-/3] ⠂ waiting...
verbose 13.717 > [email protected] postinstall /Users/jvalore/Projects/yarn-test/node_modules/postinstall-test
> postinstall-build na "npm run build"

npm WARN [email protected] No description
npm WARN The package babel-cli is included as both a dev and production dependency.

up to date in 0.961s

> [email protected] build /Users/jvalore/Projects/yarn-test/node_modules/postinstall-test
> babel src --out-dir lib

src/index.js -> lib/index.js
success Saved lockfile.

(postinstall-build na "npm run build" is the postinstall script)

Closing as this was opened against a now old version of Yarn, and I was unable to reproduce with Yarn 1.3.2; the postinstall script was run when I tried to reproduce it. If someone is still having this issue with Yarn v1.3.2, please open a new issue.

@rally25rs Please re-open, it's not solved at all.

With the following configuration:

"scripts": {
  "postinstall": "./yarn-scripts",
  "build": "encore dev",
  "watch": "encore dev --watch",
  "prod": "encore production"
}

If I run yarn upgrade, the yarn-scripts file is never called. It is called on yarn install.

Using last v1.3.2 version.

I can confirm that yarn v1.5.1 DOES NOT run prepostinstall scripts for upgrade and remove commands. It does run the scripts for install and add

steps to reproduce:

mkdir yarn_install_scripts_test
cd yarn_install_scripts_test
echo '{
  "name": "yarn_test",
  "version": "1.0.0",
  "scripts": {
    "preinstall": "touch preinstall",
    "postinstall": "touch postinstall"
  }
}' > package.json
yarn add [email protected]
ls
rm *install
yarn upgrade [email protected]
ls
yarn remove ramda
ls

the output of first ls will be:

node_modules package.json postinstall preinstall yarn.lock

and for the other two:

node_modules package.json yarn.lock

Ok, it looks like I have misunderstood the purpose of the prepostinstall scripts: they are supposed to be ran when package is installed as a dep of another package, but I was trying to use them to run some commands every time there is a modification to deps of my package ( like when adding of removing deps ).

To take a step back, what I was trying to achieve is to prevent yarn from messing up the content of a folder which I have symlinked into node_modules ( similarly to what's described here ), so I would remove the symlink in pre and add it back in postinstall ( see example below ). It worked for me for yarn install or add ran inside the package, but running upgrade or remove to modify some deps would cause the symlinked folder to be wiped clean.

here's an example of what I would have in my package.json:

{
  "name": "yarn_test",
  "version": "1.0.0",
  "scripts": {
    "preinstall": "if [ -L ./node_modules/my_module ]; then unlink ./node_modules/my_module; fi",
    "postinstall": "if ! [ -L ./node_modules/my_module ]; then ln -s ../my_module/ ./node_modules/my_module; fi"
  },
}

But it looks like what I really need is to add the folder as link dep with link:my_module like in this test fixture :)

I would like this to be reopened, since the issue was never resolved. It is not working on yarn version 1.7.1.

Our usecase is to patch a library via the postinstall. This then gets overwritten everytime we run yarn upgrade on an unrelated package, and will cause the application to crash without the patch. The patch then needs to be reapplied with yarn again.

Ideally yarn upgrade would respect postinstall, or a new lifecycle hook would be added, e.g. postupgrade.

yarn 1.13.0. issue still is not resolved.

Yarn 1.15.2, issue still is not resolved.

It would help if there was a trigger for upgrade. There is not.

Please allow us to target upgrade specifically with postupgrade if making postinstall be called for upgrades is not planned.

i think a postupgrade hook is a good solution.

1.16.0 still presents the same defect

I still would really like the postupgrade hook!

Confirming that this issue still exists in 1.21.1. Please re-open and fix.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MunifTanjim picture MunifTanjim  ·  3Comments

seansfkelley picture seansfkelley  ·  3Comments

ocolot picture ocolot  ·  3Comments

FLGMwt picture FLGMwt  ·  3Comments

sebmck picture sebmck  ·  3Comments