fix: unoptimized package can resolve to optimized deps#11410
fix: unoptimized package can resolve to optimized deps#11410csr632 wants to merge 10 commits intovitejs:mainfrom
Conversation
| if (resolvedSrc == null) { | ||
| try { | ||
| // this may throw errors if unable to resolve, e.g. aliased id | ||
| resolvedSrc = normalizePath(resolveFrom(id, path.dirname(importer))) |
There was a problem hiding this comment.
The bug come from this line: resolveFrom always use package.json#main as main field, not respecting vite's main fields (module field and custom mainFields). So the resolvedSrc here is not aligned with optimizedData.src.
This PR fix it by changing this line to use tryNodeResolve which is aligned with the resolve result during deps optimization.
|
Ideally we should drop |
|
/ecosystem-ci run |
|
📝 Ran ecosystem CI: Open
|
|
Thanks for working on this regerssion! I think that if you confirmed the linked PR was causing the issue in solid we should revert it while you check this. It seems that this PR would cause regressions in other CIs. What do you think? |
I agree. |
|
@bluwy Could you take a look at this PR? Also need some help on the failing CI of vite-plugin-svelte above. The histoire CI error also seems to related to svelte. |
fff689f to
68bdeef
Compare
|
I have added back the test and the fix reverted by #11412 Also tested with the solid template |
|
/ecosystem-ci run |
|
We really need to change that emoji reaction 😅 |
|
/ecosystem-ci run |
|
📝 Ran ecosystem CI: Open
|
I just push a commit that split
Now
@patak-dev @bluwy What do you think about this? If you agree on this, I can further polish |
90a9767 to
b51a312
Compare
|
Thanks for keeping the git diff clean for this one. I've been thinking of a cheaper way around this too, and maybe this works:
This should work for most case unless there's a bizarre dependency loop that messes the If that idea doesn't work, I think the split of FWIW currently the esbuild scanner uses vite/packages/vite/src/node/optimizer/scan.ts Lines 173 to 193 in 80f405e but we can't do that as it likely has perf downsides and overkill in most cases. so |
I think it will have issue when package has multiple entries. For example, |
|
Ah you're right 🥲 It looks like we need to always resolve the package then, and using |
b51a312 to
9e77ae1
Compare
| if (id.startsWith('node:') || isBuiltin(id)) { | ||
| url = id | ||
| } else { | ||
| const resolved = tryNodeResolve( |
There was a problem hiding this comment.
Calling tryNodeResolve with 4 args has same effect as calling tryNodeResolveCore, but tryNodeResolve has more mental burden. So I turn it into tryNodeResolveCore call here.
| } | ||
|
|
||
| const isIdESM = isESM || kind === 'dynamic-import' | ||
| let idFsPath = tryNodeResolve( |
There was a problem hiding this comment.
Calling tryNodeResolve with 4 args has same effect as calling tryNodeResolveCore, but tryNodeResolve has more mental burden. So I turn it into tryNodeResolveCore call here.
|
@bluwy Hi! This PR is now ready to be reviewed. |
|
@patak-dev @bluwy Hi! Could we try merging this fix in 4.1.0? |
|
/ecosystem-ci run |
|
📝 Ran ecosystem CI: Open
|
|
@csr632 thanks for your work here, this path looks good to me. We discussed with the team about moving forward, but the change is a bit bigger than I thought initially. I would also like to give @bluwy an opportunity to do a proper review here. We plan to release beta.1 for 4.1 so I think we should push this PR for 4.2. |
|
Sorry I had only had the time to do a full review. The refactor to make this work looks great to me, but it seems like this fails for |
|
I'll remove the 4.2 milestone, as the issues with vite-plugin-svelte and vitepress won't let us move forward with this in a minor. I'll add it to the Vite 5 milestone where we could merge and work with them to solve the issue, but we can still aim for 4.3 if these issues are resolved. |
|
Putting a note that I'll probably come around and rebase this soon. I'm refactoring some resolve stuff, and if all goes well this PR would help remove our reliance on the |
|
Ah my wording in the other PR accidentally closed this. Nonetheless, I'm still working on a fix locally and it'll be quite different from this PR, so there will be a new one soon. |
Description
#11290 make
tryOptimizedResolvestricter, removing some wrong code path that would resolve to optimized deps without considering the importer.But the wrong code path hid another bug in
tryOptimizedResolve. There are some cases that relied on the wrong code path to behave normally. Removing the code path in4.0.1make these cases fail, which reveal the new bug.The new bug is:
resolveFromalways usepackage.json#mainas the main field, which is not aligned with the vite's built in resolve algorithm (which is used to computeoptimizedData.src). SotryOptimizedResolvedoesn't match any package withpackage.json#modulefield:vite/packages/vite/src/node/plugins/resolve.ts
Line 911 in 0a07686
Here is an example(the test case in this PR):
Without this fix, the project import
package-aand gets the optimized bundle. Butpackage-bget the unoptimized module. So it ends up with two instance ofpackage-a.Fix: solidjs/solid#1426
Additional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123).