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.37k stars 2.72k forks source link

Yarn workspaces not hoisting dependencies #8982

Open Aure77 opened 10 months ago

Aure77 commented 10 months ago

What is the current behavior? In a monorepo, yarn workspace doesn't hoist one of my dependency & duplicate it on each workspace. I don't understand why.

yarn why react-native-reanimated                                                                                              
yarn why v1.22.19
[1/4] 🤔  Why do we have the module "react-native-reanimated"...?
[2/4] 🚚  Initialising dependency graph...
[3/4] 🔍  Finding dependency...
[4/4] 🚡  Calculating file sizes...
=> Found "react-native-reanimated@3.4.2"
info Reasons this module exists
   - "_project_#app1" depends on it
   - Hoisted from "_project_#app1#react-native-reanimated"
   - Hoisted from "_project_#app2#react-native-reanimated"
   - Hoisted from "_project_#app3#react-native-reanimated"
   - Hoisted from "_project_#storybook#react-native-reanimated"
info Disk size without dependencies: "6.02MB"
info Disk size with unique dependencies: "6.02MB"
info Disk size with transitive dependencies: "6.02MB"
info Number of shared dependencies: 44
✨  Done in 0.56s.

"why" command say that "react-native-reanimated" dep is hoisted. But if I try to find react-native-reanimated into node_modules I found multiple record:

find . -name "react-native-reanimated" -type d                                                                                
./node_modules/react-native-reanimated
./apps/app1/node_modules/react-native-reanimated
./apps/app2/node_modules/react-native-reanimated
./apps/app3/node_modules/react-native-reanimated
./apps/storybook/node_modules/react-native-reanimated

So it's not hoisted to root node_modules... I use the exact same version of "react-native-reanimated" in my workspaces. My only doubt is about peer dependencies of third party dependencies.

If I try to found every ref of this deps, I found that:

grep -R --include="package.json" "react-native-reanimated\":" .  
./node_modules/@react-navigation/drawer/package.json:    "react-native-reanimated": "~2.2.0",
./node_modules/@react-navigation/drawer/package.json:    "react-native-reanimated": ">= 1.0.0",
./node_modules/react-native-reanimated-carousel/package.json:        "react-native-reanimated": "2.8.0",
./node_modules/react-native-reanimated-carousel/package.json:        "react-native-reanimated": ">=2.7.0"
./node_modules/react-native-draggable-flatlist/package.json:    "react-native-reanimated": ">=2.8.0"
./node_modules/react-native-draggable-flatlist/package.json:    "react-native-reanimated": "^2.8.0",
./node_modules/react-native-screens/package.json:    "react-native-reanimated": "^2.2.0",
./node_modules/react-native-gesture-handler/package.json:    "react-native-reanimated": "^2.3.1",
./packages/components/package.json:    "react-native-reanimated": "3.4.2",
./apps/app1/package.json:    "react-native-reanimated": "3.4.2",
./apps/app1/package.json:    "react-native-reanimated": "3.4.2",
./apps/app1/package.json:    "react-native-reanimated": "3.4.2",
./apps/storybook/package.json:    "react-native-reanimated": "3.4.2",

and most of third party libs are referencing react-native-reanimated as peer or dev dependency...

So Why this lib is not hoisted at root ?

Node version: 16 Yarn version: 1.22.19

Aure77 commented 9 months ago

Repro: https://github.com/Aure77/react-native-yarn-hoist-issue

sarat1669 commented 6 months ago

@Aure77 did you find any resolution for this?