Uh oh!
There was an error while loading. Please reload this page.
fix(utils): Fix loadModule when using PnP - #4077
Conversation
8f344d0 to
7a0b28eCompareNote 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
1f49b46CompareAlso note that this requires node 12, but And it should fall back otherwise via the empty catch. |
andreialecu
commented
Oct 19, 2021
@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 functionloadModule(moduleName){try{returndynamicRequire(require.main,moduleName);}catch(e){returnundefined;}}Notice the usage of Any thoughts on it? |
kamilogorek
commented
Oct 21, 2021
I left a large comment there in case we need to understand this again. Let me know if it's not clear enough though. |
andreialecu
commented
Oct 21, 2021
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. |
kamilogorek
commented
Oct 21, 2021
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: |
andreialecu
commented
Oct 21, 2021
@kamilogorek can you try the code I suggested in that comment? functionloadModule(moduleName){try{returndynamicRequire(require.main,moduleName);}catch(e){returnundefined;}}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 |
iker-barriocanal
commented
Oct 28, 2021
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. |
andreialecu
commented
Oct 28, 2021
Yes, it does fix it. |
andreialecu
commented
Oct 28, 2021
@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. |
iker-barriocanal
commented
Oct 28, 2021
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 🥀 |
iker-barriocanal
commented
Nov 19, 2021
@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. |
AbhiPrasad
commented
Nov 19, 2021
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
createRequireto 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).