Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug?]: Mono repo and peerDependency : doesn't provide <pkg> , requested by <pkg> #6468

Closed
1 task
kopax opened this issue Aug 21, 2024 · 2 comments
Closed
1 task
Labels
bug Something isn't working stale Issues that didn't get attention waiting for feedback Will autoclose in a while unless more data are provided

Comments

@kopax
Copy link

kopax commented Aug 21, 2024

Self-service

  • I'd be willing to implement a fix

Describe the bug

When running yarn to install dependency, yarn is emitting a warning that package is not provided, while it is a peerDependency and should be provided also in devDependency.

However, when I provide the peerDependency in devDependency, as I am in a mono repo lerna, it appears that some dependency get installed at the workspace level in another node_modules, which cause typescript compilation error.

Thus, I had to remove them from dependency so they do not get downloaded multiple time

To reproduce

Create a mono repo

error TS2322: Type 'import("/home/dka/workspace/project/packages/utilities/node_modules/ethers/lib.commonjs/providers/network").Network' is not assignable to type 'import("/home/dka/workspace/project/node_modules/ethers/lib.commonjs/providers/network").Network'.

Environment

yarn dlx -q envinfo --preset jest

  System:
    OS: Linux 6.1 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
    CPU: (4) x64 AMD Ryzen 9 7940HS w/ Radeon 780M Graphics
  Binaries:
    Node: 20.10.0 - /tmp/xfs-f4424100/node
    Yarn: 4.3.1 - /tmp/xfs-f4424100/yarn
    npm: 10.5.0 - ~/.nvm/versions/node/v20.10.0/bin/npm

Additional context

No response

@kopax kopax added the bug Something isn't working label Aug 21, 2024
@clemyan
Copy link
Member

clemyan commented Aug 23, 2024

This is one of the fundamental limitations of node_modules resolution -- without using the --preserve-symlinks flag it is impossible to correctly resolve peer dependencies across symlinked dependencies (e.g. workspace)

Say we have this setup:

  • workspace-a
    • dev-depends on foo
    • peer-depends on foo
  • workspace-b
    • depends on workspace-a
    • depends on foo

If both workspaces' foo dependencies resolve to the same version, then it is deduped to the root node_modules, and all is well.

However, if the two foo dependencies resolve to different versions. Say workspace-a dev-depends on [email protected] but workspace-b depends on [email protected].

The correct behavior in this case is for workspace-a to resolve foo to [email protected] when used as a package (e.g. cd workspace-a && yarn run x) but resolve foo to [email protected] when used as a dependency of workspace-b. However, node_modules resolution can't distinguish the two cases (without --preserve-symlinks), because it only receive the source path (i.e. the path of the requiring module), not the require stack.

Thus workspace-a must either always resolve foo to [email protected] (which breaks the peer-dependency, as you have seen) or always resolve foo to [email protected] (which breaks the dev-dependency). There is no way to satisfy both.

Even removing workspace-a's dev dependency doesn't fully remove the problem. Say we have this:

We have the same problem of workspace-a must either always resolve foo to [email protected] or [email protected]. You will always have two instances of foo when typechecking either workspace-b or workspace-c.

The only fully correct way to solve this is to run all node processes with --preserve-symlinks (which not all packages are compatible with). Everything else you do will have subtle edge cases which breaks. Alternatively, you can acknowledge it may cause something to break at some point and do whatever works for your case.

@clemyan clemyan added the waiting for feedback Will autoclose in a while unless more data are provided label Aug 23, 2024
@yarnbot
Copy link
Collaborator

yarnbot commented Sep 22, 2024

Hi! 👋

It seems like this issue as been marked as probably resolved, or missing important information blocking its progression. As a result, it'll be closed in a few days unless a maintainer explicitly vouches for it.

@yarnbot yarnbot added the stale Issues that didn't get attention label Sep 22, 2024
@yarnbot yarnbot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stale Issues that didn't get attention waiting for feedback Will autoclose in a while unless more data are provided
Projects
None yet
Development

No branches or pull requests

3 participants