Skip to content
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(ssr): crash on circular import #14441

Merged
merged 9 commits into from Mar 13, 2024
Merged

fix(ssr): crash on circular import #14441

merged 9 commits into from Mar 13, 2024

Conversation

Dunqing
Copy link
Contributor

@Dunqing Dunqing commented Sep 22, 2023

Description

An interesting bug, I stumbled upon ssrTransform changing the order of import/export. Yes! This problem is essentially caused by a change in the order of import/export.

But I'm not sure it's completely fixed.

Fixes: #14433
Fixes: #14010

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@stackblitz
Copy link

stackblitz bot commented Sep 22, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@Dunqing Dunqing changed the title fix(ssr): crash for circular import fix(ssr): crash on circular import Sep 22, 2023
@Dunqing Dunqing marked this pull request as ready for review September 22, 2023 12:26
@bluwy bluwy added p3-minor-bug An edge case that only affects very specific usage (priority) feat: ssr labels Sep 25, 2023
Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for digging into this.

Looks like this makes a partial revert of #12274 + #12530. I think it makes sense that the "import" hoisting behaviour doesn't exactly apply to export {} and export *. I remember testing a lot of these edge cases, but I didn't hit this.

packages/vite/src/node/ssr/ssrTransform.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@rtsao rtsao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I think there is an unresolved nit regarding types of defineImport

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
@Dunqing
Copy link
Contributor Author

Dunqing commented Oct 7, 2023

I think this PR can be merged first

@bluwy
Copy link
Member

bluwy commented Oct 9, 2023

/ecosystem-ci run

@vite-ecosystem-ci
Copy link

vite-ecosystem-ci bot commented Oct 9, 2023

📝 Ran ecosystem CI: Open

suite result
analogjs ✅ success
astro ✅ success
histoire ✅ success
ladle ✅ success
laravel ✅ success
marko ✅ success
nuxt ✅ success
nx ❌ failure
previewjs ✅ success
qwik ❌ failure
rakkas ✅ success
sveltekit ✅ success
unocss ✅ success
vike ✅ success
vite-plugin-pwa ✅ success
vite-plugin-react ✅ success
vite-plugin-react-pages ✅ success
vite-plugin-react-swc ✅ success
vite-plugin-svelte ✅ success
vite-plugin-vue ✅ success
vite-setup-catalogue ✅ success
vitepress ✅ success
vitest ✅ success

@bluwy
Copy link
Member

bluwy commented Oct 9, 2023

Besides nx which seems to take too long to test, the rest seems to match main, so merging this would be fine by me. I find the still incorrect import/export execution order a little worrying however, but we could fix that later on.

bluwy
bluwy previously approved these changes Oct 9, 2023
@bluwy bluwy added this to the 5.0 milestone Oct 9, 2023
@Dunqing
Copy link
Contributor Author

Dunqing commented Oct 9, 2023

I find the still incorrect import/export execution order a little worrying however, but we could fix that later on.

How did you find out about this? I want to do my part for this.

@bluwy
Copy link
Member

bluwy commented Oct 9, 2023

In the import and export ordering test, I have a comment that explained the desired execution order of console.logs. With this PR's new snapshot, if you run the logs in your head you should get "mod foo", "bar1", "mod a", "mod b", "bar2".

I'm not exactly sure how to fix it but thanks for helping with this.

Comment on lines +1 to +5
export function getB() {
return '__B__'
}

export { A } from './a'
Copy link
Member

@sapphi-red sapphi-red Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export function getB() {
return '__B__'
}
export { A } from './a'
export { A } from './a'
export function getB() {
return '__B__'
}

If I change this file like above, the test fails with getB is not a function. But this code should also work as it works if I run it with node ./playground/ssr/src/circular-import/index.js (the .js extension needs to be appended to imports).

I guess while this PR fixes in some cases, it breaks in some cases.

I don't know a way to fix this completely but I feel I definitely need to read https://262.ecma-international.org/14.0/#sec-example-cyclic-module-record-graphs

related: #14048 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Yes, It's a little tricky to fix completely. I'm guessing it would need to be in two phases as you said in #14048 (comment)

For this PR, I think the current import/export order is more intuitive.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've discussed this with Sapphi. While we'd like this fix too, the current PR does improves some areas with circular imports and doesn't regress this case (getB is not a function would also happen in main). So we can defer this to later and get this PR merged for now.

@bluwy bluwy removed this from the 5.0 milestone Oct 13, 2023
@bluwy
Copy link
Member

bluwy commented Oct 13, 2023

Putting this out of 5.0 milestone for now as this fix could be done in a patch in the future. We're trying to scope down the milestone to release on time.

@Dunqing
Copy link
Contributor Author

Dunqing commented Oct 13, 2023

Putting this out of 5.0 milestone for now as this fix could be done in a patch in the future. We're trying to scope down the milestone to release on time.

What time for release 5.0?

@bluwy
Copy link
Member

bluwy commented Oct 13, 2023

The schedule now is by the end of the month, so trying to finish up all of those in the milestone while having enough time for user testing.

@Dunqing
Copy link
Contributor Author

Dunqing commented Oct 13, 2023

The schedule now is by the end of the month, so trying to finish up all of those in the milestone while having enough time for user testing.

Thanks to the vite team for all their hard work and I can't wait for the release of vite 5!

@lysekjan
Copy link

Hello, any info about this issue?
I trying it on Vite@5.0.7 and Vitest@1.04 but it seems the the bug is still there. Or am I doing something wrong?

https://github.com/lysekjan/vitest-imports

Version

vitest 1.0.4
vite 5.0.7

@patak-dev patak-dev added this to the 5.2 milestone Feb 12, 2024
@bluwy bluwy merged commit 8cd846c into vitejs:main Mar 13, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: ssr p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vite errors on cyclic imports SSR crashes from undefined imports
6 participants