Skip to content

fix(vite): resolve vite env api sourcemaps in vite-node - #35758

Merged
danielroe merged 4 commits into
nuxt:mainfrom
Askerka00:fix/vite-ssr-stacktrace
Aug 2, 2026
Merged

danielroe merged 4 commits into
nuxt:mainfrom
Askerka00:fix/vite-ssr-stacktrace

Conversation

@Askerka00

Copy link
Copy Markdown
Contributor

Fixes #35587

Resolves development SSR stacktrace sourcemap resolution for Vue SFCs when using Vite 7 Environment API (devBundler: 'vite').

Changes

  • packages/vite/src/plugins/vite-node.ts: Attach mod.transformResult.map from ssrServer.environments.ssr.moduleGraph when missing during IPC fetchModule.
  • packages/vite/src/fix-stacktrace.ts: Decode IPC sourcemap via SourceMapConsumer (source-map-js) to map V8 setup line offsets back to original SFC line numbers.

Co-authored-by: Daniel Roe [email protected]

@Askerka00
Askerka00 requested a review from danielroe as a code owner July 20, 2026 21:09
@github-actions github-actions Bot added 5.x 🐛 bug Something isn't working as expected labels Jul 20, 2026
@Askerka00
Askerka00 force-pushed the fix/vite-ssr-stacktrace branch from 54e186e to 9066539 Compare July 20, 2026 21:12
@pkg-pr-new

pkg-pr-new Bot commented Jul 20, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@nuxt/kit

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

@nuxt/nitro-server

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

nuxt

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

@nuxt/rspack-builder

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

@nuxt/schema

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

@nuxt/vite-builder

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

@nuxt/webpack-builder

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

commit: 3659f47

@coderabbitai

This comment has been minimized.

@Askerka00
Askerka00 force-pushed the fix/vite-ssr-stacktrace branch from 9066539 to 02b40cd Compare July 20, 2026 21:16

@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.

Actionable comments posted: 2

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

18-43: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low value

Consider caching the fetched map/consumer per filePath within a single stack rewrite.

Each .vue stack frame triggers its own IPC round-trip (viteNodeFetch.fetchModule) and constructs a fresh SourceMapConsumer, even if multiple frames in the same stack reference the same file (e.g. recursive/nested calls). Caching per filePath inside the Promise.all would avoid redundant IPC calls and map parsing for the same error.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/vite/src/fix-stacktrace.ts` around lines 18 - 43, Cache the fetched
source map or SourceMapConsumer by filePath within the enclosing stack-rewrite
operation, and have each callback in the stackLines.map flow reuse the cached
promise/result instead of calling viteNodeFetch.fetchModule and constructing a
new consumer for repeated .vue frames. Keep unmatched and non-.vue lines
unchanged.
packages/vite/src/plugins/vite-node.ts (1)

444-462: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Gate the module-graph lookup to .vue modules to avoid needless overhead.

Per fix-stacktrace.ts, response.map is only ever consumed for stack frames ending in .vue. As written, this block runs (including the getModuleById/fileToModulesMap lookups and the linear fallback scan) for every module fetch lacking a .map, i.e. potentially every non-.vue JS/TS module fetched during SSR — adding avoidable work to a per-request dev-server hot path.

♻️ Proposed fix: restrict to `.vue` moduleIds
-            if (response && !response.map && ssrServer.environments?.ssr?.moduleGraph) {
+            if (response && !response.map && request.payload.moduleId.endsWith('.vue') && ssrServer.environments?.ssr?.moduleGraph) {
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/vite/src/plugins/vite-node.ts` around lines 444 - 462, Restrict the
module-graph lookup block around ssrServer.environments.ssr.moduleGraph to
request.payload.moduleId values ending in “.vue”. Keep the existing response/map
checks and module resolution behavior unchanged for Vue modules, while skipping
all graph lookups and fallback scanning for non-Vue modules.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/vite/src/fix-stacktrace.ts`:
- Around line 34-37: Update the originalPositionFor call in the stack-trace
mapping logic to pass the parsed stack-frame column decremented by one, while
leaving the line conversion unchanged.

In `@packages/vite/src/plugins/vite-node.ts`:
- Around line 448-458: Update the basename fallback in the module lookup around
graph.fileToModulesMap to require a path-separator boundary before baseName,
rather than using an unrestricted key.endsWith(baseName) match. Preserve exact
basename matches while preventing similarly suffixed filenames such as
IconButton.vue from matching Button.vue.

---

Nitpick comments:
In `@packages/vite/src/fix-stacktrace.ts`:
- Around line 18-43: Cache the fetched source map or SourceMapConsumer by
filePath within the enclosing stack-rewrite operation, and have each callback in
the stackLines.map flow reuse the cached promise/result instead of calling
viteNodeFetch.fetchModule and constructing a new consumer for repeated .vue
frames. Keep unmatched and non-.vue lines unchanged.

In `@packages/vite/src/plugins/vite-node.ts`:
- Around line 444-462: Restrict the module-graph lookup block around
ssrServer.environments.ssr.moduleGraph to request.payload.moduleId values ending
in “.vue”. Keep the existing response/map checks and module resolution behavior
unchanged for Vue modules, while skipping all graph lookups and fallback
scanning for non-Vue modules.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 07ce20cb-2909-4617-8bc9-78af9955f176

📥 Commits

Reviewing files that changed from the base of the PR and between f2a96d5 and 54e186e.

⛔ Files ignored due to path filters (2)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml, !pnpm-lock.yaml
  • test/fixtures/basic/package.json is excluded by !**/package.json
📒 Files selected for processing (6)
  • .github/workflows/ci.yml
  • knip.json
  • packages/nitro-server/src/index.ts
  • packages/vite/src/fix-stacktrace.ts
  • packages/vite/src/plugins/vite-node.ts
  • pnpm-workspace.yaml

Comment thread packages/vite/src/fix-stacktrace.ts Outdated
Comment on lines +34 to +37
const orig = consumer.originalPositionFor({
line: parseInt(lineNo, 10),
column: parseInt(colNo, 10),
})

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.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '\n== file list ==\n'
git ls-files 'packages/vite/src/fix-stacktrace.ts' 'packages/vite/package.json' 'pnpm-lock.yaml' 'package.json' | sed 's#^`#-` #'

printf '\n== relevant file outline ==\n'
ast-grep outline packages/vite/src/fix-stacktrace.ts --view expanded || true

printf '\n== relevant excerpt ==\n'
sed -n '1,220p' packages/vite/src/fix-stacktrace.ts

printf '\n== search for originalPositionFor usage ==\n'
rg -n "originalPositionFor|parseInt\\(colNo|colNo" packages/vite/src packages -g'*.ts' -g'*.js'

Repository: nuxt/nuxt

Length of output: 2913


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '\n== inspect installed source-map implementation references ==\n'
rg -n "source-map-js|source-map" package.json packages -g'package.json' -g'*.ts' -g'*.js' | sed -n '1,120p'

printf '\n== if lockfile has source-map package version, show it ==\n'
rg -n '"source-map-js"|"source-map"' pnpm-lock.yaml | sed -n '1,80p'

Repository: nuxt/nuxt

Length of output: 681


🏁 Script executed:

#!/bin/bash
set -euo pipefail

sed -n '1,200p' packages/vite/src/fix-stacktrace.ts

Repository: nuxt/nuxt

Length of output: 1842


🌐 Web query:

source-map-js originalPositionFor generated column 0-based V8 stack trace column 1-based docs

💡 Result:

When working with the source-map (or source-map-js) library and V8 stack traces, it is critical to account for differences in how lines and columns are indexed. 1. source-map library: In the originalPositionFor method, the library uses 1-based indexing for lines and 0-based indexing for columns [1][2]. Even though the underlying Source Map specification (v3) uses 0-based indexing for both lines and columns, the source-map library automatically performs the necessary translation to provide a consistent 1-based line/0-based column API for developers [1][3]. 2. V8 Stack Traces: V8 stack frames typically report line and column numbers using 1-based indexing [4][5]. Specifically, the V8 StackTrace API and V8::StackFrame class methods (such as GetLineNumber and GetColumn) return 1-based values [4][5]. Historically, there have been instances where internal V8 representations were 0-based, requiring the public API to add 1 to these values to maintain the expected 1-based format [6]. Summary of Indexing: Library/API | Line Indexing | Column Indexing --- | --- | --- source-map (originalPositionFor) | 1-based [1] | 0-based [1] V8 Stack Trace API | 1-based [4][5] | 1-based [4][5] Because of these differences, when mapping a V8 stack trace column to the source-map library, you must subtract 1 from the V8 column value to match the 0-based expectation of the source-map library.

Citations:


Subtract 1 from the stack-frame column before mapping.

source-map-js expects generated columns to be 0-based, but the stack trace column is 1-based, so passing colNo through directly shifts the mapped source column by one.

🐛 Proposed fix
             const orig = consumer.originalPositionFor({
               line: parseInt(lineNo, 10),
-              column: parseInt(colNo, 10),
+              column: Math.max(0, parseInt(colNo, 10) - 1),
             })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const orig = consumer.originalPositionFor({
line: parseInt(lineNo, 10),
column: parseInt(colNo, 10),
})
const orig = consumer.originalPositionFor({
line: parseInt(lineNo, 10),
column: Math.max(0, parseInt(colNo, 10) - 1),
})
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/vite/src/fix-stacktrace.ts` around lines 34 - 37, Update the
originalPositionFor call in the stack-trace mapping logic to pass the parsed
stack-frame column decremented by one, while leaving the line conversion
unchanged.

Comment thread packages/vite/src/plugins/vite-node.ts Resolved

@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.

♻️ Duplicate comments (1)
packages/vite/src/fix-stacktrace.ts (1)

42-47: ⚠️ Potential issue | 🟡 Minor

Correct column indexing for source-map-js mappings.

As pointed out in a previous review, source-map-js uses 0-based indexing for columns, whilst V8 stack traces use 1-based columns. You must subtract 1 from the stack-frame column before mapping to prevent shifting the source column.

Additionally, the original column returned by source-map-js is 0-based, so you should add 1 when injecting it back into the stack string to match V8's expectations.

🐛 Proposed fix
             const orig = consumer.originalPositionFor({
               line: parseInt(lineNo, 10),
-              column: parseInt(colNo, 10),
+              column: Math.max(0, parseInt(colNo, 10) - 1),
             })
             if (orig.line) {
-              return line.replace(`:${lineNo}:${colNo}`, `:${orig.line}:${orig.column || 0}`)
+              return line.replace(`:${lineNo}:${colNo}`, `:${orig.line}:${orig.column !== null ? orig.column + 1 : 0}`)
             }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/vite/src/fix-stacktrace.ts` around lines 42 - 47, Update the
source-map lookup around consumer.originalPositionFor to pass colNo minus one as
the mapping column, converting the V8 1-based stack column to source-map-js
indexing. When rebuilding the stack string in the orig.line branch, add one to
orig.column before inserting it, while preserving the existing fallback
behavior.
🧹 Nitpick comments (1)
packages/vite/src/fix-stacktrace.ts (1)

41-41: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Remove redundant await.

The SourceMapConsumer constructor from the source-map-js package is completely synchronous and returns the consumer instance directly (unlike the Mozilla source-map package which returns a Promise). The await keyword is unnecessary here.

♻️ Proposed refactor
-            const consumer = await new SourceMapConsumer(rawSourceMap as any)
+            const consumer = new SourceMapConsumer(rawSourceMap as any)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/vite/src/fix-stacktrace.ts` at line 41, Remove the redundant await
from the SourceMapConsumer construction in the fix-stacktrace flow, assigning
the synchronously returned consumer instance directly while preserving the
existing rawSourceMap input and subsequent usage.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Duplicate comments:
In `@packages/vite/src/fix-stacktrace.ts`:
- Around line 42-47: Update the source-map lookup around
consumer.originalPositionFor to pass colNo minus one as the mapping column,
converting the V8 1-based stack column to source-map-js indexing. When
rebuilding the stack string in the orig.line branch, add one to orig.column
before inserting it, while preserving the existing fallback behavior.

---

Nitpick comments:
In `@packages/vite/src/fix-stacktrace.ts`:
- Line 41: Remove the redundant await from the SourceMapConsumer construction in
the fix-stacktrace flow, assigning the synchronously returned consumer instance
directly while preserving the existing rawSourceMap input and subsequent usage.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 6ff7c7c2-4f8a-408e-9b4f-f78ad9a37013

📥 Commits

Reviewing files that changed from the base of the PR and between 54e186e and 02b40cd.

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

@codspeed

codspeed Bot commented Jul 20, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 21 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing Askerka00:fix/vite-ssr-stacktrace (3659f47) with main (8568cd2)2

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. ↩

  2. No successful run was found on main (e6509fd) during the generation of this report, so 8568cd2 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@danielroe

Copy link
Copy Markdown
Member

you mention Vite 7 - but we're on Vite 8 now. is this still relevant?

could you provide a regression test?

@Askerka00

Copy link
Copy Markdown
Contributor Author

@danielroe Yes, this is still relevant for Vite 8. It uses the same Environment API where sourcemaps are stored in Vite's in-memory ssr.moduleGraph.

I've also added a regression test in packages/vite/test/fix-stacktrace.test.ts

@danielroe danielroe changed the title fix(vite): resolve Vite 7 SSR stacktrace sourcemaps via IPC fix(vite): resolve vite env api sourcemaps in vite-node Aug 2, 2026
@danielroe
danielroe enabled auto-merge August 2, 2026 09:21
@danielroe
danielroe added this pull request to the merge queue Aug 2, 2026
Merged via the queue into nuxt:main with commit f52ca30 Aug 2, 2026
34 checks passed
@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

This PR has been merged into main, which is the development branch for the next major version of Nuxt.

It will also be backported to the 4.x branch and included in an upcoming 4.x release, unless it is a breaking change, or depends on a breaking or architectural change that has only happened on main.

If you think this change should (or shouldn't) be backported, feel free to leave a comment.

Thank you for contributing to Nuxt! 💚

@github-actions github-actions Bot mentioned this pull request Aug 2, 2026
2 of 5 tasks
danielroe added a commit that referenced this pull request Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The error overlay reports incorrectly identify the source of the error

2 participants