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(vite-node): fix source map of inlined node_modules #7557

Merged

Conversation

hi-ogawa
Copy link
Contributor

@hi-ogawa hi-ogawa commented Feb 25, 2025

Description

Related

Currently ssr transform tries to preserve lines by best effort vitejs/vite#19004, but this is not guaranteed since user plugin can also change lines and also there are some not-so-edge cases ssr transform has't handle it yet (I added it as a test case).

Considering Vite SSR / module runner is also injecting source map (regardless of user code or inlined deps), I think we should allow this for Vitest / vite-node as well.

Also as an important context, preserving lines for transform is hard for the ecosystem for the following aspects:

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

Copy link

netlify bot commented Feb 25, 2025

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit d0f42b0
🔍 Latest deploy log https://app.netlify.com/sites/vitest-dev/deploys/67c28669b698970008e11732
😎 Deploy Preview https://deploy-preview-7557--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@@ -0,0 +1,11 @@
import 'node:path'

export function testStack() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently ssr transform doesn't preserve lines with export, so this repro fails on main.

@hi-ogawa
Copy link
Contributor Author

New test is not running on CI due to #7558. We need to merge that first.

@hi-ogawa hi-ogawa marked this pull request as ready for review February 26, 2025 04:34
@hi-ogawa hi-ogawa added the p2-to-be-discussed Enhancement under consideration (priority) label Feb 26, 2025
Copy link
Member

@sheremet-va sheremet-va left a comment

Choose a reason for hiding this comment

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

What happens if file already has a sourcermapUrl that points to a sourcemap file. What ends up running?

@sheremet-va sheremet-va added p3-minor-bug An edge case that only affects very specific usage (priority) and removed p2-to-be-discussed Enhancement under consideration (priority) labels Feb 28, 2025
@hi-ogawa
Copy link
Contributor Author

hi-ogawa commented Mar 1, 2025

What happens if file already has a sourcermapUrl that points to a sourcemap file. What ends up running?

Vite's transform pipeline starts with extractSourcemapFromFile and it will strip all sourceMappingURL, so Vitest won't see sourceMappingURL comment on our end. I'll add a test case to verify the correctness of collapsed sourcemap from node_modules.

Comment on lines +120 to +146
FAIL error-in-package.test.js > transpiled
Error: __TEST_STACK_TRANSPILED__
❯ innerTestStack (NODE_MODULES)/@vitest/test-dep-error/transpiled.ts:22:9
❯ testStack (NODE_MODULES)/@vitest/test-dep-error/transpiled.ts:12:3
❯ error-in-package.test.js:16:22
14|
15| test('transpiled', () => {
16| testStackTranspiled()
| ^
17| })
18|

⎯⎯[3/4]⎯

FAIL error-in-package.test.js > transpiled inline
Error: __TEST_STACK_TRANSPILED_INLINE__
❯ innerTestStack (NODE_MODULES)/@vitest/test-dep-error/transpiled-inline.ts:22:9
❯ testStack (NODE_MODULES)/@vitest/test-dep-error/transpiled-inline.ts:12:3
❯ error-in-package.test.js:20:28
18|
19| test('transpiled inline', () => {
20| testStackTranspiledInline()
| ^
21| })
22|

⎯⎯[4/4]⎯
Copy link
Contributor Author

@hi-ogawa hi-ogawa Mar 1, 2025

Choose a reason for hiding this comment

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

Error frames don't show the original ts file content because it's not found by getModuleById.

const nearest
= error instanceof TypeCheckError
? error.stacks[0]
: stacks.find((stack) => {
try {
return (
project.server
&& project.getModuleById(stack.file)
&& existsSync(stack.file)

After removing getModuleById check, frame gets shown properly like below, but this probably doesn't matter since published packages wouldn't ship .ts file, so existsSync would fail anyways for actual case.

 FAIL  stacktraces/error-in-package.test.js > transpiled
Error: __TEST_STACK_TRANSPILED__
 ❯ innerTestStack ../../../node_modules/.pnpm/@vitest+test-dep-error@file+test+cli+deps+error/node_modules/@vitest/test-dep-error/transpiled.ts:22:9
     20|  */
     21| function innerTestStack() {
     22|   throw new Error('__TEST_STACK_TRANSPILED__')
       |         ^
     23| }
     24| 
 ❯ testStack ../../../node_modules/.pnpm/@vitest+test-dep-error@file+test+cli+deps+error/node_modules/@vitest/test-dep-error/transpiled.ts:12:3
 ❯ stacktraces/error-in-package.test.js:16:22

@hi-ogawa hi-ogawa requested a review from sheremet-va March 1, 2025 06:30
Copy link
Member

@sheremet-va sheremet-va left a comment

Choose a reason for hiding this comment

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

Thank you! This looks good to me 👍🏻

@sheremet-va sheremet-va merged commit 34aa322 into vitest-dev:main Mar 7, 2025
9 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants