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): this in exported function should be undefined #18329

Merged
merged 3 commits into from
Oct 18, 2024

Conversation

sapphi-red
Copy link
Member

Description

this in exported function should be undefined but it was not.
reproduction in Node: https://stackblitz.com/edit/node-7bhlmx?file=index.js (make sure you run it locally, it works differently in stackblitz stackblitz/webcontainer-core#1532)

I used an identity function to avoid the this binding. (0, foo.bar) can be used as well, but I thought it will be difficult for the same reason with #3682.

@sapphi-red sapphi-red added feat: ssr p2-edge-case Bug, but has workaround or limited in scope (priority) labels Oct 11, 2024
Copy link

stackblitz bot commented Oct 11, 2024

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

@patak-dev

This comment was marked as outdated.

@vite-ecosystem-ci

This comment was marked as outdated.

@sapphi-red
Copy link
Member Author

/ecosystem-ci run

@sapphi-red
Copy link
Member Author

vite-environment-examples and vitest are failing with snapshot diff caused by stacktrace change (Module.foo is now foo) or the output change which is expected.
redwoodjs fail should be fixed by the last commit.

@vite-ecosystem-ci
Copy link

📝 Ran ecosystem CI on 17be758: Open

suite result latest scheduled
remix failure failure
sveltekit failure failure
vike failure failure
vite-environment-examples failure success
vitest failure success

analogjs, astro, histoire, ladle, laravel, marko, nuxt, previewjs, quasar, qwik, rakkas, redwoodjs, storybook, unocss, vite-plugin-pwa, vite-plugin-react, vite-plugin-react-swc, vite-plugin-svelte, vite-plugin-vue, vite-setup-catalogue, vitepress, vuepress

@hi-ogawa
Copy link
Collaborator

This change makes sense, but it might be affecting Vitest coverage a bit. Other than stacktrace, I found one coverage test is failing when testing with a local build. It's only one test case with Vue (specifically this one pnpm -C test/coverage-test test /vue.test -- --project v8), so it might be an issue in different parts though. I'll check with the team.

hi-ogawa added a commit to hi-ogawa/vitest that referenced this pull request Oct 12, 2024
Copy link
Contributor

@AriPerkkio AriPerkkio left a comment

Choose a reason for hiding this comment

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

Is it intentional that __vite_ssr_identity__ is left on source maps? It's not code that end user wrote, it's generated code added during transform. Leaving it in source maps doesn't sound correct to me. It will make coverage results wrong, but also makes debuggers stop in transformed code.

This is also an issue in other Vite's generated helpers so this concern might be out-of-scope of this PR.

We've been removing these from source maps on Vitest's side manually. I think this pattern has to be added there as well:

https://github.com/vitest-dev/vitest/blob/5e6de2742f329b7e65fe3dff4827b5134d777009/packages/coverage-v8/src/provider.ts#L381-L386

@sapphi-red
Copy link
Member Author

@AriPerkkio What do you mean by left on source map? Is it the mapping from __vite_ssr_identity__( to the space on the original source in this example with sourcemap visualizer? If so, it is not intentional. That said, I don't know how to do that in a performant way, I think we need to improve magic-string to achieve that.

Copy link
Contributor

@AriPerkkio AriPerkkio left a comment

Choose a reason for hiding this comment

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

Is it the mapping from __vite_ssr_identity__( to the space on the original source in this example with sourcemap visualizer?

That actually looks correct, the line 3 is not included there. Somehow it ends up in source maps in Vitest though: https://evanw.github.io/source-map-visualization/#MjIyO.... But that might be an issue just on Vitest side.

That said, I don't know how to do that in a performant way, I think we need to improve magic-string to achieve that.

The prepend and append methods do not modify the source maps, as there is no previous mapping when adding new content. So the implementation on this PR is correct in that way.

@patak-dev patak-dev added this to the 6.0 milestone Oct 14, 2024
@patak-dev patak-dev merged commit bae6a37 into vitejs:main Oct 18, 2024
14 checks passed
@sapphi-red sapphi-red deleted the fix/ssr-this-in-exported-function branch October 21, 2024 05:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: ssr p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants