-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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): use optional chaining to prevent "undefined is not an object" happening in ssrRewriteStacktrace
#19612
Conversation
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 think adding this small safety measure is okay, but the reproduction might be somewhat specific to react-router, which is using vite-node's source map support at the same time. https://github.com/remix-run/react-router/blob/796e9ae10f74ba453b738f9d80049885f24caccf/packages/react-router-dev/vite/vite-node.ts#L47-L49
Normally bun's error seems to have <anonymous>
in the stack like below, but vite-node's Error.prepareStackTrace
probably strips it out somehow, which might be the actual part of bun's node incompatibility.
Error: __TEST_ERROR__
at <anonymous> (/xxx/src/test.tsx:5:20)
at test (/xxx/src/test.tsx:6:5)
at <anonymous> (/xxx/repro.js:8:9)
at processTicksAndRejections (native:7:39)
I knew there was something going on further up the chain, but I couldn't quite figure it out. This makes things a lot clearer, thanks for pointing that out. |
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.
My best guess is that bun might have a different behavior in CallSite.getFunctionName
https://github.com/vitest-dev/vitest/blob/0c2924b7aeb7eaf1b1fa0e78d2da76a7f3197b50/packages/vite-node/src/source-map-handler.ts#L404-L411 If someone can debug this, it would be better to be reported on bun, but adding optional chain looks fine for me.
Or maybe that's not it. Node has eval
but Bun has <anonymous>
, which probably makes vite-node to do a different thing 🤔
|
ssrRewriteStacktrace
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.
Putting Bun's Node compat bug aside, I think we should merge this PR as the regex allows varName
to be undefined and the code should have been written like this.
| datasource | package | from | to | | ---------- | ------- | ----- | ----- | | npm | vite | 6.2.1 | 6.2.2 | ## [v6.2.2](https://github.com/vitejs/vite/blob/HEAD/packages/vite/CHANGELOG.md#small622-2025-03-14-small) - fix: await client buildStart on top level buildStart ([#19624](vitejs/vite#19624)) ([b31faab](vitejs/vite@b31faab)), closes [#19624](vitejs/vite#19624) - fix(css): inline css correctly for double quote use strict ([#19590](vitejs/vite#19590)) ([d0aa833](vitejs/vite@d0aa833)), closes [#19590](vitejs/vite#19590) - fix(deps): update all non-major dependencies ([#19613](vitejs/vite#19613)) ([363d691](vitejs/vite@363d691)), closes [#19613](vitejs/vite#19613) - fix(indexHtml): ensure correct URL when querying module graph ([#19601](vitejs/vite#19601)) ([dc5395a](vitejs/vite@dc5395a)), closes [#19601](vitejs/vite#19601) - fix(preview): use preview https config, not server ([#19633](vitejs/vite#19633)) ([98b3160](vitejs/vite@98b3160)), closes [#19633](vitejs/vite#19633) - fix(ssr): use optional chaining to prevent "undefined is not an object" happening in \`ssrRewriteStac ([4309755](vitejs/vite@4309755)), closes [#19612](vitejs/vite#19612) - feat: show friendly error for malformed `base` ([#19616](vitejs/vite#19616)) ([2476391](vitejs/vite@2476391)), closes [#19616](vitejs/vite#19616) - feat(worker): show asset filename conflict warning ([#19591](vitejs/vite#19591)) ([367d968](vitejs/vite@367d968)), closes [#19591](vitejs/vite#19591) - chore: extend commit hash correctly when ambigious with a non-commit object ([#19600](vitejs/vite#19600)) ([89a6287](vitejs/vite@89a6287)), closes [#19600](vitejs/vite#19600)
Description
This PR handles a case where
varName
passed from a RegExp match on stacktrace lines isundefined
and causesssrRewriteStacktrace
to fail onvarName.trim()
.This bug was encounted in the Vite dev server running with Bun via the command
bunx --bun vite
.I have a reproduction which demonstrates the error in a simple manner. There is also a patch for Vite on the branch "fix-patch" which demonstrates the fixed behaviour.
I'm not the first to encounter this according to this issue over at Bun.
Explaination
Upon inspecting the stack traces and comparing them between Node and Bun, the first line of the stack trace tends to contain "eval", which means the function safely accesses
varName.trim()
and continues to completion. However, in Bun,varName
isundefined
which causes the function to fail when it tries to access.trim()
where it doesn't exist.If it weren't for modules outside of the module graph being skipped after
moduleGraph.getModuleById()
, isundefined
failure would be common as anonymous entries in the stacktrace usually occur.The existance of
if (!trimmedVarName ...
indicates to me that this was expected, buttrimmedVarName
can't beundefined
because of the lack of optional chaining since the function fails if the value is not a string beforeundefined
can be assigned.Bun stacktrace:
Node stacktrace: