napi-rs / node-rs

Node.js bindings ❤️ Rust crates
https://node-rs.dev
MIT License
1.15k stars 34 forks source link

incompatible with yarn2 PnP #263

Closed cspotcode closed 3 years ago

cspotcode commented 3 years ago

In yarn2, any module you attempt to require() must be declared as a dependency in your own package.json. This means when @node-rs/helper attempts to, for example, require('@swc/core-linux'), it fails, because @swc/core-linux is not a dependency of @node-rs/helper

Fortunately, I think the solution is simple. @node-rs/helper needs to accept the caller's require function as an argument. Then when trying to require modules, it will use the caller's require, which is able to resolve the caller's dependencies. This is what we want, since we want to be sure that the modules we load match the versions that the caller wants to use.

For backwards compatibility, this can be optional, but it will probably cause issues with yarn 2 until the callers are updated to pass in a require function.

cspotcode commented 3 years ago

Here is where I'm encountering this failure: @swc/core is calling loadBinding: https://github.com/swc-project/swc/blob/master/node-swc/src/index.ts#L14

The dependencies are declared for @swc/core, so they can be require()d by @swc/core but not by @node-rs/helper https://github.com/swc-project/swc/blob/master/package.json#L80-L85