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): hoist export to handle cyclic import better #18983

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

hi-ogawa
Copy link
Collaborator

@hi-ogawa hi-ogawa commented Dec 17, 2024

Description

Currently Object.defineProperty(__vite_ssr_exports__, ...) is not hoisted and this is different from what bundlers normally do (see my notes in https://github.com/hi-ogawa/reproductions/tree/main/cyclic-import). By defining __vite_ssr_exports__ property early before await __vite_ssr_import__(...) calls, it fixes certain cyclic import scenarios.

For export * from "...", bundlers statically analyze the export names and hoist them too, but this is not possible for Vite SSR, so __vite_ssr_exportAll__ is kept as is.

One potential issue with this change is that this can technically break a "working" code with cyclic import which were previously forgiven. This was actually the case of our own unit test. See #18983 (comment) for details.

todo

@hi-ogawa hi-ogawa force-pushed the fix-ssr-transform-hoist-export branch from ff8cf4b to 3cc86da Compare December 17, 2024 09:09
package.json Outdated Show resolved Hide resolved
playground/hmr-ssr/__tests__/hmr-ssr.spec.ts Outdated Show resolved Hide resolved
@hi-ogawa
Copy link
Collaborator Author

/ecosystem-ci run

Copy link

pkg-pr-new bot commented Dec 17, 2024

Open in Stackblitz

npm i https://pkg.pr.new/vite@18983

commit: 958467a

@vite-ecosystem-ci
Copy link

📝 Ran ecosystem CI on 3cc86da: Open

suite result latest scheduled
histoire failure failure
laravel failure success
marko failure success
nuxt failure success
redwoodjs failure failure
remix failure failure
vike failure failure
vite-plugin-svelte failure failure
vitest failure success
waku failure failure

analogjs, astro, ladle, previewjs, quasar, qwik, rakkas, storybook, sveltekit, unocss, vite-environment-examples, vite-plugin-pwa, vite-plugin-react, vite-plugin-react-swc, vite-plugin-vue, vite-setup-catalogue, vitepress, vuepress

vitest.config.ts Outdated
Comment on lines 18 to 21
// TODO:
// importing non entry file can be broken due to cyclic import e.g.
// pnpm exec tsx packages/vite/src/node/server/index.ts
setupFiles: ['./packages/vite/src/node/index.ts'],
Copy link
Collaborator Author

@hi-ogawa hi-ogawa Dec 17, 2024

Choose a reason for hiding this comment

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

Interestingly, export hoisting can make cyclic import handling stricter as seen by:

$ pnpm test-unit packages/vite/src/node/ssr/runtime/__tests__/server-runtime.spec.ts 

FAIL  packages/vite/src/node/ssr/runtime/__tests__/server-runtime.spec.ts [ packages/vite/src/node/ssr/runtime/__tests__/server-runtime.spec.ts ]
ReferenceError: Cannot access 'serverConfigDefaults' before initialization
 ❯ Module.get [as serverConfigDefaults] packages/vite/src/node/server/index.ts:4:116

but this case might be legitimate since this is the same error as tsx:

$ pnpm exec tsx packages/vite/src/node/server/index.ts
/home/hiroshi/code/others/vite/packages/vite/src/node/config.ts:648
  server: serverConfigDefaults,
          ^


ReferenceError: Cannot access 'serverConfigDefaults' before initialization

I'd imagine previously these were undefined (still wrong but no hard error) since __vite_ssr_import_x_.serverConfigDefaults getter was not defined. But now this hits uninitialized const serverConfigDefaults access due to getter.

Just temporarily, I added this silly setupFiles, so that modules will be initialized in a known safe order.

@hi-ogawa hi-ogawa force-pushed the fix-ssr-transform-hoist-export branch from 4e35256 to 2245aaa Compare December 18, 2024 02:14
Comment on lines 854 to 856
// it throws a same error as browser case,
// but it doesn't auto reload and it calls `hot.accept` called with `undefined`
await untilUpdated(() => el(), '')
Copy link
Collaborator Author

@hi-ogawa hi-ogawa Dec 18, 2024

Choose a reason for hiding this comment

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

It turns out the error now became same as playground/hmr but module runner doesn't auto-reload, so the existing test failed.

 ReferenceError: Cannot access 'c' before initialization

I adjust this test case for now to make it pass.

@hi-ogawa
Copy link
Collaborator Author

/ecosystem-ci run

@vite-ecosystem-ci
Copy link

📝 Ran ecosystem CI on 83a7a1f: Open

suite result latest scheduled
histoire failure failure
ladle failure failure
nuxt success failure
redwoodjs failure failure
remix failure failure
vite-plugin-svelte failure failure
vitest failure failure
waku failure failure

analogjs, astro, laravel, marko, previewjs, quasar, qwik, rakkas, storybook, sveltekit, unocss, vike, vite-environment-examples, vite-plugin-pwa, vite-plugin-react, vite-plugin-react-swc, vite-plugin-vue, vite-setup-catalogue, vitepress, vuepress

@vite-ecosystem-ci
Copy link

📝 Ran ecosystem CI on 83a7a1f: Open

suite result latest scheduled
histoire failure failure
ladle failure failure
nuxt success failure
redwoodjs failure failure
remix failure failure
vite-plugin-svelte failure failure
vitest failure failure
waku failure failure

analogjs, astro, laravel, marko, previewjs, quasar, qwik, rakkas, storybook, sveltekit, unocss, vike, vite-environment-examples, vite-plugin-pwa, vite-plugin-react, vite-plugin-react-swc, vite-plugin-vue, vite-setup-catalogue, vitepress, vuepress

@hi-ogawa hi-ogawa added the p2-to-be-discussed Enhancement under consideration (priority) label Dec 25, 2024
@hi-ogawa hi-ogawa marked this pull request as ready for review December 25, 2024 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-to-be-discussed Enhancement under consideration (priority) trigger: preview
Projects
Status: Discussing
Development

Successfully merging this pull request may close these issues.

Vite SSR Imports not loading classes correctly - constructors not loaded
1 participant