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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to require pnp from baseDir if it is null #62

Open
lmiller1990 opened this issue Apr 7, 2023 · 1 comment
Open

Try to require pnp from baseDir if it is null #62

lmiller1990 opened this issue Apr 7, 2023 · 1 comment

Comments

@lmiller1990
Copy link

lmiller1990 commented Apr 7, 2023

Hi! 馃憢

Firstly, thanks for your work on this project! 馃檪

Today I used patch-package to patch resolve-package-path@4.0.3 for the project I'm working on.

Correctly grab pnpapi if the Yarn 3 project is not the same as the process.cwd(). This is useful because this module executes require('pnpapi) as soon as it is required, and that cwd may not necessarily be the one with the project.

Here is the diff that solved my problem:

diff --git a/node_modules/resolve-package-path/lib/index.js b/node_modules/resolve-package-path/lib/index.js
index a1463f7..00d83b8 100644
--- a/node_modules/resolve-package-path/lib/index.js
+++ b/node_modules/resolve-package-path/lib/index.js
@@ -89,6 +89,14 @@ function resolvePackagePath(target, baseDir, _cache) {
             // the custom `pnp` code here can be removed when yarn 1.13 is the
             // current release. This is due to Yarn 1.13 and resolve interoperating
             // together seamlessly.
+            if (!pnp) {
+                try {
+                    pnp = require(require.resolve('pnpapi', { paths: [baseDir] }))
+                } 
+                catch (e) {
+                    // not in Yarn PnP; not a problem
+                }
+            }
             pkgPath = pnp
                 ? pnp.resolveToUnqualified(target + '/package.json', baseDir)
                 : (0, resolve_package_path_1.default)(cache, target, baseDir);

This issue body was partially generated by patch-package.

@lmiller1990
Copy link
Author

Came up in Cypress where we use your module - thanks for this! PR: cypress-io/cypress#26452

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant