shiftx/esbuild-plugin-pnp

Plugin doesn't respect optional peerDependencies

Opened this issue · 1 comments

I have a project using this plugin and mongodb (mongoose) as a dependencies.
Mongodb has mongodb-client-encryption and aws4 as optional peerDependencies:

[...]
 "peerOptionalDependencies": {
    [...]
    "mongodb-client-encryption": "^1.0.0",
     [...]
  },
"peerDependenciesMeta": {
    [...]
    "mongodb-client-encryption": {
      "optional": true
    },
   [...]
    "aws4": {
      "optional": true
    }
  },
[...]

and tries to require them inside a try / catch, so esbuild sends them to this plugin via build.onResolve().

It fails with the following errors:
 > pnp:/home/anton/Projects/pnp-bug/.yarn/$$virtual/ws-virtual-535d9b46ac/0/cache/ws-npm-7.4.3-0b722707c3-493655b7c4.zip/node_modules/ws/lib/buffer-util.js: error: [pnp-plugin] ws tried to access bufferutil (a peer dependency) but it isn't provided by its ancestors; this makes the require call ambiguous and unsound.

Required package: bufferutil (via "bufferutil")
Required by: ws@virtual:d28d90fcce8f835b192578396e8f5173b2b8b328b7cfef0576840b7a542ac690165edbef41106c1d84e3785afed3d0e0f75720e95cf3e9fc4449f815cf17d425#npm:7.4.3 (via /home/anton/Projects/pnp-bug/.yarn/$$virtual/ws-virtual-535d9b46ac/0/cache/ws-npm-7.4.3-0b722707c3-493655b7c4.zip/node_modules/ws/lib/)
Ancestor breaking the chain: engine.io@npm:4.1.1
Ancestor breaking the chain: jsdom@virtual:51b2d3db5940cbda81797992a2f896453e18927005ba88742e0fe2eeb71cfb090b53a8594298c25967a351a8b709cd58d3612e5feba8c79a7b59ae3338b0722b#npm:16.4.0


    105 │   const bufferUtil = require('bufferutil');
        ╵                              ~~~~~~~~~~~~

 > pnp:/home/anton/Projects/pnp-bug/.yarn/$$virtual/mongodb-virtual-1a6d487e1f/0/cache/mongodb-npm-3.6.3-3206384cb1-85a8064180.zip/node_modules/mongodb/lib/operations/connect.js: error: [pnp-plugin] mongodb tried to access mongodb-client-encryption (a peer dependency) but it isn't provided by its ancestors; this makes the require call ambiguous and unsound.

Required package: mongodb-client-encryption (via "mongodb-client-encryption")
Required by: mongodb@virtual:4aa8df445eee8963fd8c1f58ef19305d83825503b1218ced8747fba919196c60e8a0029dc817a11e9c3f061c3db32887f679974bfcd5f68685331fd0236da61f#npm:3.6.3 (via /home/anton/Projects/pnp-bug/.yarn/$$virtual/mongodb-virtual-1a6d487e1f/0/cache/mongodb-npm-3.6.3-3206384cb1-85a8064180.zip/node_modules/mongodb/lib/operations/)
Ancestor breaking the chain: mongoose@https://github.com/raphaelschwinger/mongoose.git#commit=734e82191feb2f9b51a946c5b2589f4f55ff61af


    501 │       require.resolve('mongodb-client-encryption');
        ╵                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~

 > pnp:/home/anton/Projects/pnp-bug/.yarn/$$virtual/mongodb-virtual-1a6d487e1f/0/cache/mongodb-npm-3.6.3-3206384cb1-85a8064180.zip/node_modules/mongodb/lib/operations/connect.js: error: [pnp-plugin] mongodb tried to access mongodb-client-encryption (a peer dependency) but it isn't provided by its ancestors; this makes the require call ambiguous and unsound.

Required package: mongodb-client-encryption (via "mongodb-client-encryption")
Required by: mongodb@virtual:4aa8df445eee8963fd8c1f58ef19305d83825503b1218ced8747fba919196c60e8a0029dc817a11e9c3f061c3db32887f679974bfcd5f68685331fd0236da61f#npm:3.6.3 (via /home/anton/Projects/pnp-bug/.yarn/$$virtual/mongodb-virtual-1a6d487e1f/0/cache/mongodb-npm-3.6.3-3206384cb1-85a8064180.zip/node_modules/mongodb/lib/operations/)
Ancestor breaking the chain: mongoose@https://github.com/raphaelschwinger/mongoose.git#commit=734e82191feb2f9b51a946c5b2589f4f55ff61af


    512 │       let mongodbClientEncryption = require('mongodb-client-encryption');
        ╵                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~

 > pnp:/home/anton/Projects/pnp-bug/.yarn/$$virtual/mongodb-virtual-1a6d487e1f/0/cache/mongodb-npm-3.6.3-3206384cb1-85a8064180.zip/node_modules/mongodb/lib/core/auth/mongodb_aws.js: error: [pnp-plugin] mongodb tried to access aws4 (a peer dependency) but it isn't provided by its ancestors; this makes the require call ambiguous and unsound.

Required package: aws4 (via "aws4")
Required by: mongodb@virtual:4aa8df445eee8963fd8c1f58ef19305d83825503b1218ced8747fba919196c60e8a0029dc817a11e9c3f061c3db32887f679974bfcd5f68685331fd0236da61f#npm:3.6.3 (via /home/anton/Projects/pnp-bug/.yarn/$$virtual/mongodb-virtual-1a6d487e1f/0/cache/mongodb-npm-3.6.3-3206384cb1-85a8064180.zip/node_modules/mongodb/lib/core/auth/)
Ancestor breaking the chain: mongoose@https://github.com/raphaelschwinger/mongoose.git#commit=734e82191feb2f9b51a946c5b2589f4f55ff61af

    12 │   aws4 = require('aws4');

I currently worked around it by defining mongodb-client-encryption and aws4 as externals for this plugin, but IMO this should be somehow handled by the plugin automatically. Maybe some check like is this dependency optional then just show a warning instead of throwing an error would be a solution.

Hope this helps.

Thanks for reporting this! I'm planning to deprecate this plugin once the official yarn plugin and esbuild has better support for externals.