Berry: [Bug] packageExtensions not working

Created on 25 Apr 2020  ยท  5Comments  ยท  Source: yarnpkg/berry

Describe the bug

Some packages still aren't able to resolve the warning even after fixing them with the packageExtension config in .yarnrc.yml

Error: apollo-utilities tried to access graphql (a peer dependency) but it isn't provided by its ancestors; this makes the require call ambiguous and unsound.

In my .yarnrc.yml I have the following:

packageExtensions:
  "apollo-utilities@*":
    dependencies:
      "graphql": "^15.0.0"

To Reproduce

Checkout - https://github.com/jeffsee55/yarn-2-package-extension-issue

  1. yarn install
  2. yarn generate - you should see the error above

To see it working without pnp:

  1. Checkout to previous commit (https://github.com/jeffsee55/yarn-2-package-extension-issue/commit/ae60a37c6f4b6b36e8585b020c6aab5fc65821f8) which will change it to not use pnp.
  2. yarn install
  3. yarn generate - you should see the graphql-codegen cli start in your terminal.

I understand this is ultimately an issue with the package manager but would like to learn me about why this sometimes still doesn't work and how I might be able to dig into it deeper.

Environment if relevant (please complete the following information):

  • OS: [e.g. OSX, Linux, Windows, ...]
  • Node version 12.16.0
  • Yarn version 2.0.0-rc.30

Additional context

Also - if this is in a monorepo it hurts worse because that means I need to undo all of my pnp setup for other packages. Is there any way to just disable pnp for a single package in a workspace?

bug

Most helpful comment

Package extensions are correct, they add the dependency to the right package. The problem is that for peer dependencies, there might be multiple problematic intermediaries. Take this:

. -> A -> B -> C -> D -peer-> X
  -> X

Since C doesn't provide X, Yarn throws an error saying that D is trying to access a dependency it doesn't list. However, it falls short of telling you that the reason for that is that C doesn't provide it!

D tried to access X (a peer dependency) but it isn't provided by its ancestors; this makes the require call ambiguous and unsound.

Required package: X (via "X")
Required by: [email protected] (via /path/to/D/index.js)

Making it difficult to fix, since you need to use yarn why to understand who's dependending on D, and which one is omitting to list a peer dependency on X. Then once you figure it out, you need to start again because B doesn't list it either, so you get the same problem ๐Ÿ˜ž

This will be fixed with https://github.com/yarnpkg/berry/pull/1362, which will cause Yarn to list the also broken peer dependency links when throwing the error:

D tried to access X (a peer dependency) but it isn't provided by its ancestors; this makes the require call ambiguous and unsound.

Required package: X (via "X")
Required by: [email protected] (via /path/to/D/index.js)
Ancestor breaking the chain: [email protected]

All 5 comments

I and a colleague have spent several hours on this issue over the past two days, getting increasingly frustrated. I don't think what you're seeing is a bug at all, I think the fix you're trying in packageExtensions just isn't correct.

run yarn. You'll get this message:

โžค YN0000: โ”Œ Resolution step
โžค YN0002: โ”‚ apollo-upload-client@npm:13.0.0 doesn't provide graphql@^0.11.3 || ^0.12.3 || ^0.13.0 || ^14.0.0 || ^15.0.0 requested by apollo-link@npm:1.2.14
โžค YN0002: โ”‚ apollo-upload-client@npm:13.0.0 doesn't provide graphql@^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 requested by apollo-link-http-common@npm:0.2.16
โžค YN0060: โ”‚ apollo-link@npm:1.2.14 [74d3c] provides graphql@npm:15.0.0 with version 15.0.0 which doesn't satisfy ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 requested by apollo-utilities@npm:1.3.3
โžค YN0000: โ”” Completed

I can't reproduce your issue -- it seems to run fine. But I suspect if you fix the first warning there (ie give apollo-link@* a dependency on graphql) it should sort the issue.
The reason I say this is that what I've been fighting with today and yesterday is a React app built with Webpack. I install Apollo according to the Apollo React docs recommendation, try to build, and I get your error:

Error: apollo-utilities tried to access graphql (a peer dependency) but it isn't provided by its ancestors; this makes the require call ambiguous and unsound.

We tried all kinds of variations on your solution, except that needing to specify dependents for apollo-utilities wasn't the fix to the problem: we added apollo-client and apollo-utilities as standard dependents of @apollo/react-hooks, ran yarn, got a slightly different warning, added graphql as a dependency of apollo-link-error@* and everything just worked perfectly once that was done.

I think this is the solution here, I'm just going off me and my colleague fighting with Yarn for ages until we actually read the warnings it gives on install properly. Also, I assume loose mode would fix the issues as well, but I've literally only just seen it in the docs.

Package extensions are correct, they add the dependency to the right package. The problem is that for peer dependencies, there might be multiple problematic intermediaries. Take this:

. -> A -> B -> C -> D -peer-> X
  -> X

Since C doesn't provide X, Yarn throws an error saying that D is trying to access a dependency it doesn't list. However, it falls short of telling you that the reason for that is that C doesn't provide it!

D tried to access X (a peer dependency) but it isn't provided by its ancestors; this makes the require call ambiguous and unsound.

Required package: X (via "X")
Required by: [email protected] (via /path/to/D/index.js)

Making it difficult to fix, since you need to use yarn why to understand who's dependending on D, and which one is omitting to list a peer dependency on X. Then once you figure it out, you need to start again because B doesn't list it either, so you get the same problem ๐Ÿ˜ž

This will be fixed with https://github.com/yarnpkg/berry/pull/1362, which will cause Yarn to list the also broken peer dependency links when throwing the error:

D tried to access X (a peer dependency) but it isn't provided by its ancestors; this makes the require call ambiguous and unsound.

Required package: X (via "X")
Required by: [email protected] (via /path/to/D/index.js)
Ancestor breaking the chain: [email protected]

Thanks to both of you for the input, @arcanis that's helpful to know - I tried just about every combination I could think of but the complexity in your example explains why I wasn't going to get it right any time soon.

TIL - yarn why is what would have helped me solve this on my own. Thanks again!

EDIT: happy to close this

hmm... I guess I'm still not getting it right...

โžค YN0000: [@bb/apollo-test-util]: ../../../../.yarn/cache/apollo-engine-reporting-protobuf-npm-0.5.2-15aa08f143-f49534868b.zip/node_modules/apollo-engine-reporting-protobuf/dist/protobuf.d.ts(1,23): error TS2307: Cannot find module 'long' or its corresponding type declarations.
> yarn why -R apollo-engine-reporting-protobuf
โ”œโ”€ @bb/apollo-test-util@workspace:app-lib/graph/packages/apollo-test-util
โ”‚  โ””โ”€ apollo-server-types@npm:0.5.1 [98c53] (via npm:^0.5 [98c53])
โ”‚     โ””โ”€ apollo-engine-reporting-protobuf@npm:0.5.2 (via npm:^0.5.2)

this is the latest thing I've tried...
/.yarnrc.yml

packageExtensions:
  '@bb/apollo-test-util@*':
    dependencies:
      apollo-server-types@^0.5:
        dependencies:
          [email protected]:
            dependencies:
              long: '^4'
            devDependencies:
              '@types/long': '^4'

oh I figured it out, I guess in my first iteration I missed a carat so I tried the above

packageExtensions:
  apollo-engine-reporting-protobuf@^0.5:
    dependencies:
      long: '^4'
      '@types/long': '^4'
Was this page helpful?
0 / 5 - 0 ratings

Related issues

thealjey picture thealjey  ยท  4Comments

benwainwright picture benwainwright  ยท  3Comments

larixer picture larixer  ยท  4Comments

juanpicado picture juanpicado  ยท  4Comments

Mike-Dax picture Mike-Dax  ยท  3Comments