-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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(utils): Fix loadModule
when using PnP
#4077
Conversation
8f344d0
to
7a0b28e
Compare
Note that this should also be an improvement for a normal There could be cases where the current code fails to require the proper module, depending on how things are hoisted. For more details expand the "Why?" paragraph here: https://yarnpkg.com/advanced/rulebook/#packages-should-only-ever-require-what-they-formally-list-in-their-dependencies Also, it falls back to pre-existing implementation if this fails. |
7a0b28e
to
1f49b46
Compare
Also note that this requires node 12, but And it should fall back otherwise via the empty catch. |
@kamilogorek I noticed you added this function in #3483 If Care to explain with an example why this This PR does make things work but it's possible that the function could be rewritten and the overall implementation might be able to be simplified. Here's a super simple implementation as a replacement for function loadModule(moduleName) {
try {
return dynamicRequire(require.main, moduleName);
}
catch (e) {
return undefined;
}
} Notice the usage of Any thoughts on it? |
I left a large comment there in case we need to understand this again. Let me know if it's not clear enough though.
|
I read the comment, but I guess I need to understand it with an example. Or rather, would the implementation in my comment above fit the requirements? Because it is easier to understand, and it fixes the PnP problem. |
The main reason is that you cannot use linked packages without process based require and We had multiple issues with it in the past, and every change adds additional risk without lots of tests, see: And all the comments in related issues. Without process-based require: With process-based required: |
@kamilogorek can you try the code I suggested in that comment? function loadModule(moduleName) {
try {
return dynamicRequire(require.main, moduleName);
}
catch (e) {
return undefined;
}
} Notice the The problem with hardcoding |
It does appear to work, however, edit: It appears to be working fine, and we could swap the implementation. cc @lobsterkatie @AbhiPrasad @iker-barriocanal |
LGTM. What about adding a comment about why that block is added? Without context, it's hard to understand the reason for it being there. |
Yes, it does fix it. |
@iker-barriocanal I updated the comment and the implementation. Please double check that it passes all your requirements as per this last commit before merging. |
LGTM |
This pull request has gone three weeks without activity. In another week, I will close it. But! If you comment or otherwise update it, I will reset the clock, and if you label it "A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀 |
@AbhiPrasad @lobsterkatie is there any reason to not merge this? It's been a while and it seems to work fine. If there are no reasons, I'll merge it. |
I'm fine with merging, I think the worry is that we haven't tested all the use cases out yet. Maybe we need to add some tests alongside this function change to get more confidence. |
Fixes #4076
Uses
createRequire
to ensure that modules are correctly located and scoped to the main package that is running.See:
https://nodejs.org/docs/latest/api/modules.html#modules_accessing_the_main_module
https://nodejs.org/api/module.html#module_module_createrequire_filename
Before submitting a pull request, please take a look at our
Contributing guidelines and verify:
yarn lint
) & (yarn test
).