yarn icon indicating copy to clipboard operation
yarn copied to clipboard

Hoisting in workspaces resolves incorrect module

Open tylergoodman opened this issue 8 years ago • 0 comments

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

Bug?

What is the current behavior? Currently, module hoisting can set up node_modules folders in such a way that the wrong version of a package is resolved.

If the current behavior is a bug, please provide the steps to reproduce.

https://github.com/tylergoodman/yarn_hoisting_test

  • install the node modules with yarn
  • run the dev script in the b workspace cd workspaces/b && yarn run dev

This causes yarn to run the next binary in the folder workspaces/b/node_modules/.bin/next, which points to workspaces/b/node_modules/next/dist/.bin/next That next script spawns a new node process in the same working directory, in this case workspaces/b/node_modules/next/dist/bin/next-dev

  • next-dev requires workspaces/b/node_modules/next/dist/server/index.js
  • which requires workspaces/b/node_modules/next/dist/server/config.js
  • which requires workspaces/b/next.config.js

Here's where the hoisting comes into play:

  • which requires @zeit/next-sass which resolves to node_modules/@zeit/next-sass/index.js
  • which requires extract-text-webpack-plugin which resolves to node_modules/extract-text-webpack-plugin/dist/cjs.js
  • which requires node_modules/extract-text-webpack-plugin/dist/index.js
  • which requires webpack/lib/Chunk which resolves to node_modules/webpack/lib/Chunk.js due to hoisting

The webpack version installed at node_modules/webpack is ^4.6.0, which is required by workspaces/a and was hoisted. The version that's required by extract-text-webpack-plugin is ^3.8.1, but since it has been hoisted, it resolves to the wrong version.

Note that the correct version of webpack required by next, and subsequently extract-text-webpack-plugin is installed at workspaces/b/node_modules/webpack

What is the expected behavior? The simplest solution seems to be that extract-text-webpack-plugin shouldn't be hoisted so that it resolves to the correct webpack version mentioned in its package.json. I'm aware the the nohoist option exists and have been using it, but I wanted to post this since it looks like a failure in the hoisting system since the chain of require calls that lead to this all look correct. Any help or insight regarding this is greatly appreciated.

Please mention your node.js, yarn and operating system version. MacOS

$ node -v
v9.1.0
$ yarn -v
1.6.0
$ sw_vers -productVersion 
10.13.3

tylergoodman avatar Apr 26 '18 18:04 tylergoodman