-
- Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Self-service
- I'd be willing to implement a fix
Describe the bug
Since Yarn 4.3.0 (this PR I think: #6205), Yarn logs YN0002 for missing peer dependencies even when peerDependenciesMeta.optional is set to true.
This is counter to the previous and documented behaviour here:
The author of
packageBcan fix this problem by marking thepackagePeerpeer dependency as optional - but only if the peer dependency is actually optional, of course!
Note that with this bug, even using packageExtensions to override the definition does not work as a consumer (e.g., adding the dependency there). The only solution is to add a real dependency in the package.json.
To reproduce
I have a full reproduction here: https://github.com/MorrisonCole/repro-yarn-optional-peers-required
To reproduce with just a package.json though, using Yarn 4.3.0 or later:
{ "dependencies": { "@material-ui/core": "4.12.4", "react": "17.0.2", "react-dom": "17.0.2" }, "packageManager": "yarn@4.3.0" }@material-ui/core@4.12.4 declares the @types/react peer dependency as optional as follows:
"peerDependencies": { "@types/react": "^16.8.6 || ^17.0.0", "react": "^16.8.0 || ^17.0.0", "react-dom": "^16.8.0 || ^17.0.0" }, "peerDependenciesMeta": { "@types/react": { "optional": true } }Output on Yarn 4.3.0 ❌
➤ YN0000: · Yarn 4.3.0 ➤ YN0000: ┌ Resolution step ➤ YN0000: └ Completed ➤ YN0000: ┌ Post-resolution validation ➤ YN0002: │ repro-yarn-optional-peers-required@workspace:. doesn't provide @types/react (p7178c), requested by @material-ui/core and other dependencies. ➤ YN0086: │ Some peer dependencies are incorrectly met by your project; run yarn explain peer-requirements <hash> for details, where <hash> is the six-letter p-prefixed code. ➤ YN0000: └ Completed ➤ YN0000: ┌ Fetch step ➤ YN0000: └ Completed ➤ YN0000: ┌ Link step ➤ YN0000: └ Completed ➤ YN0000: · Done with warnings in 0s 57ms Output on Yarn 4.2.2 ✅
➤ YN0000: · Yarn 4.2.2 ➤ YN0000: ┌ Resolution step ➤ YN0000: └ Completed ➤ YN0000: ┌ Fetch step ➤ YN0000: └ Completed ➤ YN0000: ┌ Link step ➤ YN0000: └ Completed ➤ YN0000: · Done in 0s 56ms Environment
System: OS: macOS 15.3.2 CPU: (10) arm64 Apple M1 Max Binaries: Node: 23.10.0 - /private/var/folders/d3/sgc51r7j6cq_7vjfst990hg00000gp/T/xfs-18cc1f9b/node Yarn: 4.8.1 - /private/var/folders/d3/sgc51r7j6cq_7vjfst990hg00000gp/T/xfs-18cc1f9b/yarn npm: 11.2.0 - ~/.nvm/versions/node/v23.10.0/bin/npm bun: 1.0.18 - ~/.bun/bin/bunAdditional context
This effectively blocks an upgrade of a large monorepo, since the only workaround is to add all optional peer dependencies as real dev dependencies.
Related:
- Rework peer requirement and warning system #6205 (comment)
- [Bug?]: yarn does not taken into account peerDependenciesMeta.optional #4653
- [Bug?]: yarn does not take into account peerDependenciesMeta.optional for privately published packages #5153 (although this is definitely not limited to privately published packages)