-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Conversation
Run & review this pull request in StackBlitz Codeflow. |
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]', | ||
) |
There was a problem hiding this comment.
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. 😅
093de39
to
90ca10f
Compare
This comment was marked as resolved.
This comment was marked as resolved.
2e69cb8
to
6772daf
Compare
@@ -69,6 +69,7 @@ describe.runIf(isServe)('serve', () => { | |||
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` | |||
{ | |||
"mappings": "AAAA;EACE,UAAU;AACZ;;ACAA;EACE,UAAU;AACZ", | |||
"sourceRoot": "/root", |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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}`, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
/ecosystem-ci run |
📝 Ran ecosystem CI: Open
|
The Nuxt issue is the same one we have currently in main, let's merge this one for the next patch |
Description
fix #12885.
sourceRoot
for better resolving the absolute source path which could helping resolve absolute stack traceAdditional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).