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): stacktrace uses abs path with or without sourcemap #12902

Merged
merged 9 commits into from
May 15, 2023

Conversation

fi3ework
Copy link
Member

Description

fix #12885.

  • add sourceRoot for better resolving the absolute source path which could helping resolve absolute stack trace
  • make all stack trace using an absolute path.

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 Apr 18, 2023

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

Comment on lines 84 to 91
const reg = new RegExp(
// TODO: ts without sourcemaps will resolve column to 8 which should be 9
path.resolve(__dirname, '../src') + '/error\\.' + ext + ':2:[89]',
)
Copy link
Member Author

Choose a reason for hiding this comment

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

not sure why this happened, we could fix this in another PR. I think it's not introduced here. 😅

@fi3ework fi3ework force-pushed the fix-stacktrace branch 2 times, most recently from 093de39 to 90ca10f Compare April 18, 2023 19:37
@fi3ework

This comment was marked as resolved.

@fi3ework fi3ework force-pushed the fix-stacktrace branch 7 times, most recently from 2e69cb8 to 6772daf Compare April 19, 2023 18:38
@@ -69,6 +69,7 @@ describe.runIf(isServe)('serve', () => {
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
{
"mappings": "AAAA;EACE,UAAU;AACZ;;ACAA;EACE,UAAU;AACZ",
"sourceRoot": "/root",
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is correct? The file is absolute, so it might work for the browser (as a pathname), but this will be incorrect in Node.js (should be a filepath).

Why do we need to specify sourceRoot anyway?

Copy link
Member Author

Choose a reason for hiding this comment

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

When the sources are relative paths, provide a sourceRoot to assemble the absolute paths. I think this is useful for user to locate the error source.

The throw error before PR, I think /src/error.js:2:9 is not easy to find the source file especially when using relative path.

# sourcemaps enabled: true
Error: e
    at Module.error (/src/error.js:2:9)
    at file:///Users/fi3ework/Downloads/vite-main/playground/ssr-html/test-stacktrace.js:36:7

After PR:
All types of imports will using an absolute path in error stack.

# sourcemaps enabled: true
# source file extension: js
Error: e
    at Module.error (/Users/fi3ework/OSS/vite/playground/ssr-html/src/error.js:2:9)
    at file:///Users/fi3ework/OSS/vite/playground/ssr-html/test-stacktrace.js:44:9
Error: e
    at Module.error (/Users/fi3ework/OSS/vite/playground/ssr-html/src/error.js:2:9)
    at file:///Users/fi3ework/OSS/vite/playground/ssr-html/test-stacktrace.js:44:9
Error: e
    at Module.error (/Users/fi3ework/OSS/vite/playground/ssr-html/src/error.js:2:9)
    at file:///Users/fi3ework/OSS/vite/playground/ssr-html/test-stacktrace.js:44:9

Copy link
Member

Choose a reason for hiding this comment

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

Source route doesn't have to be absolute as far as I know.

In your example code will not work correctly in SSR because it's not absolute to the file system.

How does it know that /root is actually relative to the root of the project and not an absolute path on the file system?

Copy link
Member Author

@fi3ework fi3ework May 1, 2023

Choose a reason for hiding this comment

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

Source route doesn't have to be absolute as far as I know.

Yeah, it doesn't have to be. But it will be easy to locate the source file IMO. Does this behavior break something I don't know? 🤔

In your example code will not work correctly in SSR because it's not absolute to the file system.

The example generated by running pnpm test-serve -t stacktrace in playground/ssr-html. Could you explain in more detail, Thanks!

How does it know that /root is actually relative to the root of the project and not an absolute path on the file system?

/root is replaced by an absolute path in E2E test to normalize the actual paths in different runners. If not matched, it should keep as-is.

Copy link
Member

Choose a reason for hiding this comment

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

@fi3ework Would changing sourceURL=${mod.url} to sourceURL=${mod.file} work?
I guess Node.js is treating the original file path as /src/error.js. We want Node.js to treat the original file path as /Users/fi3ework/OSS/vite/playground/ssr-html/src/error.js (or file:///Users/fi3ework/OSS/vite/playground/ssr-html/src/error.js).

`\n//# sourceURL=${mod.url}${sourceMapSuffix}`,

Copy link
Member

Choose a reason for hiding this comment

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

/root is replaced by an absolute path in E2E test to normalize the actual paths in different runners. If not matched, it should keep as-is.

I see then. I assumed it replaces /src/file.ts with <root>/file.ts, and not <fsRootPath>/src/file.ts. It should be fine from my side then.

Copy link
Member Author

Choose a reason for hiding this comment

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

@sapphi-red Basically could work except for Windows 😅. Check out https://github.com/vitejs/vite/actions/runs/4939126748/jobs/8829577947?pr=12902#step:14:50. The error location was incorrectly resolved to 4 instead of 2 for two cases (others are good). I don't think I can fix it quickly without a Windows device. 😅 (had reverted the tweak)

Copy link
Member

@sapphi-red sapphi-red May 13, 2023

Choose a reason for hiding this comment

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

Thanks for testing 💚

According to the sourcemap spec, when the sources are relative paths, it will be resolved from the place of the file. So it should work without specifing sourceRoot. Also setting sourceRoot will cause a problem that #12079 solved.

Vite was lacking support for resolving relative sources and that was causing the stacktrace to be like that. I pushed some commits implementing that. Coincidentally I found the reason of #12902 (comment) and fixed that too. It would be nice if you could review my change.

Copy link
Member Author

Choose a reason for hiding this comment

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

Using id is a much more clever solution than url. Nice work! LGTM.

@sapphi-red
Copy link
Member

/ecosystem-ci run

@sapphi-red sapphi-red added p3-minor-bug An edge case that only affects very specific usage (priority) feat: ssr labels May 13, 2023
@vite-ecosystem-ci
Copy link

vite-ecosystem-ci bot commented May 13, 2023

📝 Ran ecosystem CI: Open

suite result
astro ✅ success
histoire ✅ success
iles ✅ success
ladle ✅ success
laravel ✅ success
marko ✅ success
nuxt ❌ failure
previewjs ✅ success
qwik ✅ success
rakkas ✅ success
sveltekit ✅ success
unocss ✅ success
vite-plugin-ssr ✅ 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

@patak-dev
Copy link
Member

The Nuxt issue is the same one we have currently in main, let's merge this one for the next patch

@patak-dev patak-dev merged commit 88c855e into vitejs:main May 15, 2023
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.

Inconsistent source maps
4 participants