Skip to content

fix(vite): normalise development ssr app stacktrace - #33989

Merged
danielroe merged 29 commits into
mainfrom
fix/ssr_stacktrace
Mar 5, 2026
Merged

danielroe merged 29 commits into
mainfrom
fix/ssr_stacktrace

Conversation

@huang-julien

Copy link
Copy Markdown
Member

🔗 Linked issue

fix #33917

📚 Description

This PR fixes stacktraces for errors triggered within the vue application in SSR.
The issue lies in vite which uses an offset and the error coming from nitro which has no offset on the stack traces lines. This offset causes vite's ssrFixStacktrace to not be able to correctly get the position of the error from sourcemaps.

Not sure whether this should be fixed within Nuxt or if Vite should provide a way to trigger ssrFixStacktrace without offset.

We could also implement an utility to retrieve sourcemap from the vite runner and fix the stacktrace on nuxt's side instead of add an offset and calling ssrFixStacktrace from vite.

@bolt-new-by-stackblitz

Copy link
Copy Markdown

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

@huang-julien
huang-julien marked this pull request as draft December 30, 2025 10:01
@github-actions github-actions Bot added 4.x 🐛 bug Something isn't working as expected labels Dec 30, 2025
@pkg-pr-new

pkg-pr-new Bot commented Dec 30, 2025 •

Copy link
Copy Markdown

Open in StackBlitz

@nuxt/kit

npm i https://pkg.pr.new/@nuxt/kit@33989

@nuxt/nitro-server

npm i https://pkg.pr.new/@nuxt/nitro-server@33989

nuxt

npm i https://pkg.pr.new/nuxt@33989

@nuxt/rspack-builder

npm i https://pkg.pr.new/@nuxt/rspack-builder@33989

@nuxt/schema

npm i https://pkg.pr.new/@nuxt/schema@33989

@nuxt/vite-builder

npm i https://pkg.pr.new/@nuxt/vite-builder@33989

@nuxt/webpack-builder

npm i https://pkg.pr.new/@nuxt/webpack-builder@33989

commit: 3ffb7dc

@codspeed

codspeed Bot commented Dec 30, 2025 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 20 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing fix/ssr_stacktrace (cdbe8fa) with main (934c26f)

Open in CodSpeed

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@huang-julien

Copy link
Copy Markdown
Member Author

@danielroe test are throwing i'll fix it later.

Do you think we could open a PR to vite to allow calling ssrFixStacktrace without it using offset since we're fixing an error outside of the vite-runner ?
If not should we keep this implementation ? Or should we have an utility to fix stacktraces by retrieving the sourcemap from the vite runner instead of sending the error stracktrace to vite.
It's more about whether

  • We update the stacktrace to include the offset then call ssrFixStacktrace from vite
  • Open a PR to vite to allow calling ssrFixStacktrace without calculating the offset. This way, we don't need to add manually the offset before calling ssrFixStacktrace from vite
  • We get the sourcemap from vite, then fix the stacktrace ourself

@danielroe

danielroe commented Dec 30, 2025 •

Copy link
Copy Markdown
Member

the ideal scenario is that we implement this entirely in the vite-builder package (or upstream in vite or vite-node) as it sounds like it is entirely an artifact of the vite node dev server implementation we have

do you think that's possible?

@huang-julien

Copy link
Copy Markdown
Member Author

Just looked at vite-node. The fix should probably done here.
Vite-node could expose it's own ssrFixStacktrace as everything needed is already available in ViteNodeRunner.

I think i'll try to work on it. Once merged, this PR could get a rework to use the exposed ssrFixStacktrace from vite node runner.

@huang-julien

Copy link
Copy Markdown
Member Author

OK so let's add a ssrFixStacktrace to vite-node. Probably won't be used in Nuxt 5 since nitro's moving to vite plugin but it's nice to fix it for 4.x. I think this could also go in to 3.x
I've reworked the PR to use the PR from vite-node

@huang-julien

huang-julien commented Jan 24, 2026 •

Copy link
Copy Markdown
Member Author

@danielroe is there a reason we don't allow importing anything from vite-builder in nitro ? I think it should be OK.

@danielroe

Copy link
Copy Markdown
Member

it's not a dependency of nitro and it should be conceptually separate - it helps us build good abstractions internally.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/vite/src/fix-stacktrace.ts (1)

5-11: Model the runner cache as optional for type accuracy.

At runtime, runner is unset until Line 10. Declaring that explicitly improves type safety for future edits.

Suggested diff
-  let runner: ViteNodeRunner
+  let runner: ViteNodeRunner | undefined
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/vite/src/fix-stacktrace.ts` around lines 5 - 11, The runner variable
should be typed as optional to reflect that it's unset at startup; change the
declaration of runner to allow undefined (e.g., let runner: ViteNodeRunner |
undefined) and keep the lazy import in nitroApp.hooks.hook('error', ...) using
the existing runner ||= await import(...) pattern and the subsequent call to
runner.ssrFixStacktrace(error) (or use optional chaining
runner?.ssrFixStacktrace(error) if you prefer to guard the call) so TypeScript
knows the cache may be undefined until initialized.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/vite/src/fix-stacktrace.ts`:
- Around line 5-11: The runner variable should be typed as optional to reflect
that it's unset at startup; change the declaration of runner to allow undefined
(e.g., let runner: ViteNodeRunner | undefined) and keep the lazy import in
nitroApp.hooks.hook('error', ...) using the existing runner ||= await
import(...) pattern and the subsequent call to runner.ssrFixStacktrace(error)
(or use optional chaining runner?.ssrFixStacktrace(error) if you prefer to guard
the call) so TypeScript knows the cache may be undefined until initialized.

ℹ️ Review info

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b660be6 and c0bb47f.

📒 Files selected for processing (3)
  • packages/vite/build.config.ts
  • packages/vite/src/fix-stacktrace.ts
  • packages/vite/src/vite.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/vite/src/vite.ts
  • packages/vite/build.config.ts

@danielroe danielroe changed the title fix(vite,nitro): fix app stacktrace in SSR fix(vite): normalise development ssr app stacktrace Mar 2, 2026
@danielroe
danielroe enabled auto-merge March 4, 2026 20:43
@danielroe
danielroe added this pull request to the merge queue Mar 5, 2026
Merged via the queue into main with commit 6361902 Mar 5, 2026
98 of 100 checks passed
@danielroe
danielroe deleted the fix/ssr_stacktrace branch March 5, 2026 08:49
This was referenced Mar 5, 2026
danielroe added a commit that referenced this pull request Mar 9, 2026
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Daniel Roe <[email protected]>
danielroe added a commit that referenced this pull request Mar 9, 2026
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Daniel Roe <[email protected]>
This was referenced Mar 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.x 🐛 bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error stack highlights wrong line when an error is thrown in app

2 participants