-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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: unoptimized package can resolve to optimized deps #11410
Conversation
@@ -899,8 +906,23 @@ export async function tryOptimizedResolve( | |||
// lazily initialize resolvedSrc | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
@@ -230,7 +231,7 @@ async function nodeImport( | |||
if (id.startsWith('node:') || isBuiltin(id)) { | |||
url = id | |||
} else { | |||
const resolved = tryNodeResolve( |
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.
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.
@@ -997,12 +997,18 @@ async function bundleConfigFile( | |||
} | |||
|
|||
const isIdESM = isESM || kind === 'dynamic-import' | |||
let idFsPath = tryNodeResolve( |
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.
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
tryOptimizedResolve
stricter, 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.1
make these cases fail, which reveal the new bug.The new bug is:
resolveFrom
always usepackage.json#main
as the main field, which is not aligned with the vite's built in resolve algorithm (which is used to computeoptimizedData.src
). SotryOptimizedResolve
doesn't match any package withpackage.json#module
field: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-a
and gets the optimized bundle. Butpackage-b
get 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
).