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.44k stars 2.73k forks source link

Yarn workspaces and root lifecycle scripts #5790

Open steelbrain opened 6 years ago

steelbrain commented 6 years ago

Do you want to request a feature or report a bug? Bug

What is the current behavior?

Image a yarn workspaces repository with the following struct

package.json
packages:
  - package-a:
      package.json
      index.js
  - package-b:
      package.json
      index.js

and package.json at root has a postinstall script and the children don't. When we do yarn at the top level it installs dependencies and run the postinstall script from top level manifest, which is good at the expected behavior.

But if you do yarn from any of the two packages, it will only invoke the local postinstall script and not the one in the top level manifest.

What is the expected behavior?

In yarn workspaces, any yarn or yarn add or yarn remove in children should invoke lifecycle scripts in top level manifest too.

Please mention your node.js, yarn and operating system version. Yarn version is v1.3.2 Node version is v8.11.1 OS is macOS

GAumala commented 6 years ago

What's the benefit of running yarn install in one of the package directories? Shouldn't it be the same as running it at the root directory since all modules are installed there?

I think it would be nice if yarn forwarded the postinstall "event" to the children, after running install at the root, so that all postinstall scripts run.

steelbrain commented 6 years ago

@GAumala Imagine adding a dependency to one of the package directories. You don't want to add a package to the root level, you wanna add it to that specific package.

GAumala commented 6 years ago

Right, I forgot that postinstall also runs after yarn add. Running the top level postinstall script after any yarn add seems reasonable to me.

denkristoffer commented 5 years ago

My workaround here is to add a postinstall script to every workspace that runs yarn workspaces run postinstall — it seems to work. Uses the script from the root package.json and I only have to change the command in one place 👍

Edit: Sorry, this doesn't actually work. I guess the script command must've been cached or something since it did act as I want it to for a minute 🙂

mattfysh commented 5 years ago

@denkristoffer - would "postinstall": "cd ../.. && yarn postinstall" work?

someonewasherebefore commented 5 years ago

@mattfysh It works, but then you can't use it because if you do a top-level yarn add -W all the postinstall hooks from packages will run. If you have 3 packages in the workspace, the script will run 3 times + 1 for the root workspace.

Maybe we need another hook for the root workspace?

love2me commented 3 years ago

Any update on this issue? I have the same problem.🍻

nkuehn commented 2 years ago

Confirm this issue. Scenario: yarn workspaces provide the yarn workspaces focus command which is intended to be run inside a specific packages folder to install only the dependencies needed there (great for CI to speed up jobs of certain packages).

That feature is de facto incompatible with postinstall configuration through .yarnrc.yml. At this point we're not speaking about a custom usage scenario or best / bad practice discussion but about built-in features that are not compatible when used as intended.

theKashey commented 1 year ago

The current documentation around postinstall hooks is a little misleading - they states that the hook will be executed when package's dependency tree changes are written to the disk. yarn.lock updates after one local package is being yarn add-ed to another one, but the hooks is not executed.

The nearest solution is afterWorkspaceDependencyAddition plugin hook and it is executed in the right place, but not in the right "time" - before any physical file gets updated.

But I've found a way - https://gist.github.com/theKashey/3614c91bcd6bd3fb116172297a3522fb