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(node-resolve): mark module as external if resolved module is external #799
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e9b4c28
fix(node-resolve): mark module as external if resolved module is exte…
tjenkinson 23465bf
don't use ?.
tjenkinson bab212a
prevent infinite loops
tjenkinson 995d616
check both importee and importer in nested this.resolve
tjenkinson b2d5c6b
Merge branch 'master' into node-resolve-external-regex
tjenkinson b374698
upgrade to rollup 2.42.0 to simplify nested `this.resolve`
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import 'external'; |
1 change: 1 addition & 0 deletions
1
packages/node-resolve/test/fixtures/resolved-external/node_modules/external/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not think having a global flag here is a good idea. As resolving is inherently asynchronous, concurrent resolves can mess up the value of this flag. I fear this problem needs to be solved in Rollup core by correctly tracking uses of the
skipSelf
flag: If a plugin usesthis.resolve
with theskipSelf
flag, then Rollup needs to remember this when callingresolveId
of other plugins so that when another plugin subsequently callsthis.resolve
for THE SAME ID (that is important), the original plugin is still skipped (and this should be remembered transitively). I believe this is the issue you are trying to solve?I thought about this myself some time ago but did not implement anything yet, maybe I should.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yep. I was thinking it would only be called once at a time on a single instance, but wasn't thinking about multiple ids in parallel.
I think that would work for this case, although the difference with what you proposed is that the
node-resolve
plugin would be skipped completely whenthis.resolve
invoked from commonjs meaning later plugins might get invoked that wouldn't usually, vs now where it would still stop whennode-resolve
returns something (although what it returns now is not really the correct thing, given it could map to an external).Not sure where the best place to break the chain is, and also when breaking the chain if there is a sensible value that could be returned or if we should actually signal an infinite loop back to the thing that called
this.resolve
.Confusing problem to think about :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. But to my understanding, this change is not essential to the functionality you are implementing, right? So you could remove it from here without any regressions and one could always add something later, be it here or in Rollup core.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite understanding. The change here to do the nested
this.resolve
is needed (unless there is another solution I'm missing) to fix #609I don't mind if the fix is entirely here (with a tweak to keep a
inNestedResolve
flag against the id) or partly here (to do thethis.resolve
) but with a core change to prevent the loops.Also this isn't a blocker for me btw. I just saw the issue and decided to attempt to fix it, so not sure it's a particularly urgent problem to solve.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah sorry, should have read the entire thread. The problem is that this is an issue in core that affects all plugins. But to fix it just for node-resolve, you could maintain a data structure that contains the source/importer of what we are just resolving to remember to skip exactly this call when resolving. From Rollup side, each combination will be resolved exactly once. But you could clean up the data structure once a resolution is done to account for plugins resolving the same modules at a later stage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with removing that extra test and simplifying the plugin to use the core change. The edge case is so many layers deep to think about it does seem unlikely, and there's still the way of opting out if needed in the future like you mentioned in the docs.
It's bugging me though I still can't actually understand why the extra loop round is fixing the test with commonjs 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the question is more why it was broken in the first place without the second round. The reason is that the CommonJS plugin was doing this:
passing special options to the node-resolve plugin to change resolution, and this is the call that was skipped. Admittedly we could also try to hash the custom options object in the loop prevention, but this is dangerous as we cannot go for object identity as it is likely recreated for each call, and there is no requirement to have it serialisable via JSON stringify.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could do a JSON.stringify in a try-catch as a "best effort" approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But now I have written this nice description and I am feeling the actual logic is getting more and more complex. Maybe we start with what we have now and see if this causes issues anywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhh thanks that was the missing piece. Agree ideally the
custom
should also be part of the key, but waiting until that actually causes a problem (if it does) seems reasonable. Will update this PR