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

fix: find the pnpapi the issuer belongs to #301

Merged
merged 10 commits into from
Feb 27, 2024

Conversation

merceyz
Copy link
Contributor

@merceyz merceyz commented Sep 2, 2021

What's the problem this PR addresses?

  • In some cases enhanced-resolve is outside the control of the pnpapi and can't require it
  • The issuer might not belong to the same pnpapi as enhanced-resolve
  • The issuer might not belong to a pnpapi at all and should use the node_modules resolution instead

Fixes arcanis/pnp-webpack-plugin#32 for Webpack 5

How did you fix it?

Use module.findPnpApi to locate the correct pnpapi for the issuer, if a pnpapi isn't found continue the normal resolution

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Sep 2, 2021

CLA Signed

The committers listed above are authorized under a signed CLA.

lib/PnpPlugin.js Outdated Show resolved Hide resolved
@alexander-akait
Copy link
Member

I think better to fix it on yarn side?

@jgornick
Copy link

jgornick commented Sep 3, 2021

I think better to fix it on yarn side?

@alexander-akait Can you elaborate on how it could be fixed on the yarn side? Thank you for your input!

@merceyz
Copy link
Contributor Author

merceyz commented Sep 3, 2021

It's not really a bug on our side, require('pnpapi') is supposed to return the pnpapi that the issuer (enhanced-resolve in this case) belongs to so it's working as intended

@jgornick
Copy link

jgornick commented Sep 8, 2021

@sokra @alexander-akait ^^^ Bump 😄

@codecov
Copy link

codecov bot commented Sep 8, 2021

Codecov Report

Attention: Patch coverage is 52.38095% with 10 lines in your changes are missing coverage. Please review.

Project coverage is 92.47%. Comparing base (a998c7d) to head (bf7b68f).
Report is 8 commits behind head on main.

Files Patch % Lines
lib/ResolverFactory.js 20.00% 6 Missing and 2 partials ⚠️
lib/PnpPlugin.js 90.00% 1 Missing ⚠️
lib/util/module-browser.js 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #301      +/-   ##
==========================================
- Coverage   92.85%   92.47%   -0.38%     
==========================================
  Files          43       44       +1     
  Lines        2042     2061      +19     
  Branches      598      603       +5     
==========================================
+ Hits         1896     1906      +10     
- Misses        118      125       +7     
- Partials       28       30       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jgornick
Copy link

@alexander-akait @sokra Just wanted to check-in again if you could help provide any insight on how to approach a fix for this issue? Again, I'm more than willing to help fix it too.

I've tried digging into this to figure out another way to help fallback to alternatives, but with the plugin system, I was getting lost as to how the flow of resolution works and where one might be able to intersect and fallback. I've also reached out to @arcanis to help provide any insight, and I think we're all kind of stuck.

Anything would be greatly appreciated! Thank you!

@alexander-akait
Copy link
Member

Let's wait @arcanis answer, I am not against these changes

@sokra
Copy link
Member

sokra commented Sep 20, 2021

How to reproduce a scenario where you have packages that are yarn managed and packages that are non-yarn managed?

@jgornick
Copy link

How to reproduce a scenario where you have packages that are yarn managed and packages that are non-yarn managed?

@sokra I'm sorry for not including that. Here's a link to a repo to reproduce that's using this patch as the fix: https://github.com/jgornick/yarn-webpack-node-modules-to-pnp

@jgornick
Copy link

How to reproduce a scenario where you have packages that are yarn managed and packages that are non-yarn managed?

@sokra I'm sorry for not including that. Here's a link to a repo to reproduce that's using this patch as the fix: https://github.com/jgornick/yarn-webpack-node-modules-to-pnp

@sokra Please let me know if there's anything else I can provide or help with 😄

@jgornick
Copy link

@sokra @alexander-akait ^^^ Bump again 😄

Specifically looking for any information with how to resolve #301 (comment). I have a reproducible repository setup at https://github.com/jgornick/yarn-webpack-node-modules-to-pnp

Thank you!

@arcanis
Copy link
Contributor

arcanis commented Sep 29, 2021

Let's wait @arcanis answer, I am not against these changes

Those changes seem reasonable to me (note that @merceyz is a core contributor on Yarn 🙂).

@jgornick
Copy link

I think the main issue here though is that enhanced-resolve doesn't have a way to fallback if this the PnP Plugin can't resolve a package. @merceyz is highlighting the issue here: https://github.com/webpack/enhanced-resolve/pull/301/files#diff-484df5a516d012258b8eb71e51625ef132b1c571ef3756b7c45433f4365e2e69R61

@alexander-akait
Copy link
Member

@jgornick Can you fix lint?

@jgornick
Copy link

jgornick commented Oct 1, 2021

I'll try to look at adding tests sometime today.

Is it fair to say that out of the PnpPlugin, if it can't resolve a module that it should indeed call the callback without any arguments? Or is there a better way we can tell enhanced-resolve to try fallbacks?

cc @sokra @alexander-akait

@jgornick
Copy link

jgornick commented Oct 7, 2021

Bump ^^^ @sokra @alexander-akait 😄

@merceyz
Copy link
Contributor Author

merceyz commented Oct 7, 2021

I would prefer if the FIXME was fixed before this is merged but as I said in #301 (comment) i'm not sure how and was hoping either of you did

@jgornick
Copy link

jgornick commented Oct 8, 2021

@sokra and @alexander-akait ... There should be a commit coming from @merceyz that should fix this issue so that if PnP is available, but still can't resolve a module, it'll fallback to trying node_modules again.

The only question I have is how can I satisfy the code coverage issues, especially in ResolverFactory? In order to test this, we'd have to mock a module so that the findPnpApi method is available. However, I don't see any other examples of that in the codebase, which makes me think it's an anti-pattern too 😄

One option I was thinking of is what if we made the findPnpApi an option coming into the resolver factory so that it could be provided too? Please let me know what you think so I can finish this PR up 😄

Thank you!

  • Joe

@merceyz merceyz marked this pull request as draft October 8, 2021 20:31
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Oct 8, 2021

CLA Signed

The committers are authorized under a signed CLA.

@jgornick
Copy link

@TheLarkInn and @alexander-akait Looks like this should be good to go.

@jgornick
Copy link

Just fix merge conflicts and we are good to go!

@TheLarkInn Just wanted to check-in to make sure you're good with the changes here?

@jgornick
Copy link

@TheLarkInn and @alexander-akait: Bump 😄

@jgornick
Copy link

jgornick commented Aug 7, 2023

Sorry for the weekly bump @alexander-akait and @TheLarkInn, but just trying to get this through as I think we're finally all ready to go 😄

@jgornick
Copy link

Weekly bump @TheLarkInn 😄

If we can get you to remove the changes requested flag, then we can at least get the workflows to run.

@jgornick
Copy link

Bump 😄

@alexander-akait, is there anything you can do to remove the blocker on here from @TheLarkInn?

@jgornick
Copy link

Just checking in again on this PR to see if we can get it merged?

cc @TheLarkInn @alexander-akait

@jgornick
Copy link

👋 Hey, it's me again 😄

Would love to get this merged so that we can stop referencing a custom build of enhanced-resolve.

cc @TheLarkInn @alexander-akait

@arcanis
Copy link
Contributor

arcanis commented Jan 29, 2024

@TheLarkInn Is there anything still blocking the merge?

@@ -5,6 +5,7 @@

"use strict";

const findPnpApi = require("module").findPnpApi;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove it from the top of file and put it inside processPnpApiOption

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and added some feedback

@alexander-akait
Copy link
Member

I'm a little concerned about performance, we need improve it

@webpack-bot
Copy link

@alexander-akait Thanks for your update.

I labeled the Pull Request so reviewers will review it again.

@TheLarkInn Please review the new changes.

@jgornick
Copy link

Thank you @alexander-akait!

@alexander-akait alexander-akait merged commit 7ae5ca4 into webpack:main Feb 27, 2024
20 of 22 checks passed
@merceyz merceyz deleted the merceyz/feat/multi-root branch February 28, 2024 10:28
styfle added a commit to vercel/next.js that referenced this pull request Mar 21, 2024
This PR upgrades `enhanced-resolve` to `5.16.0` so as to benefit from
webpack/enhanced-resolve#301, recently merged.

Without this diff, importing dependencies from files from external PnP
projects would fail. It's a little niche, but I'm working on a
documentation website that leverages that to allow deploying multiple
websites from the same template.

Co-authored-by: Sam Ko <sam@vercel.com>
Co-authored-by: Steven <steven@ceriously.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Shipped
Development

Successfully merging this pull request may close these issues.

pnp-webpack-plugin cannot work outside of pnp runtime.
8 participants