Repository navigation
fix(vite): resolve vite env api sourcemaps in vite-node - #35758
Conversation
54e186e to
9066539
Compare
@nuxt/kit
@nuxt/nitro-server
nuxt
@nuxt/rspack-builder
@nuxt/schema
@nuxt/vite-builder
@nuxt/webpack-builder
commit: |
This comment has been minimized.
This comment has been minimized.
Co-authored-by: Daniel Roe <[email protected]>
9066539 to
02b40cd
Compare
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (2)
packages/vite/src/fix-stacktrace.ts (1)
18-43: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low valueConsider caching the fetched map/consumer per
filePathwithin a single stack rewrite.Each
.vuestack frame triggers its own IPC round-trip (viteNodeFetch.fetchModule) and constructs a freshSourceMapConsumer, even if multiple frames in the same stack reference the same file (e.g. recursive/nested calls). Caching perfilePathinside thePromise.allwould 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 winGate the module-graph lookup to
.vuemodules to avoid needless overhead.Per
fix-stacktrace.ts,response.mapis only ever consumed for stack frames ending in.vue. As written, this block runs (including thegetModuleById/fileToModulesMaplookups and the linear fallback scan) for every module fetch lacking a.map, i.e. potentially every non-.vueJS/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
⛔ Files ignored due to path filters (2)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml,!pnpm-lock.yamltest/fixtures/basic/package.jsonis excluded by!**/package.json
📒 Files selected for processing (6)
.github/workflows/ci.ymlknip.jsonpackages/nitro-server/src/index.tspackages/vite/src/fix-stacktrace.tspackages/vite/src/plugins/vite-node.tspnpm-workspace.yaml
| const orig = consumer.originalPositionFor({ | ||
| line: parseInt(lineNo, 10), | ||
| column: parseInt(colNo, 10), | ||
| }) |
There was a problem hiding this comment.
🎯 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.tsRepository: 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:
- 1: https://registry.npmjs.org/source-map
- 2: https://www.npmjs.com/package/source-map
- 3: https://raw.githubusercontent.com/mozilla/source-map/master/README.md
- 4: https://v8.github.io/api/head/classv8_1_1StackFrame.html
- 5: https://chromium.googlesource.com/v8/v8/+/ae1d91913bd59c4c9da63088f2cc7375b958ef38/include/v8-debug.h
- 6: https://chromium.googlesource.com/v8/v8/+/b16d51efa9111564d7e274376a8839d1886019ec
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.
| 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.
There was a problem hiding this comment.
♻️ Duplicate comments (1)
packages/vite/src/fix-stacktrace.ts (1)
42-47:⚠️ Potential issue | 🟡 MinorCorrect column indexing for
source-map-jsmappings.As pointed out in a previous review,
source-map-jsuses 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-jsis 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 valueRemove redundant
await.The
SourceMapConsumerconstructor from thesource-map-jspackage is completely synchronous and returns the consumer instance directly (unlike the Mozillasource-mappackage which returns a Promise). Theawaitkeyword 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
📒 Files selected for processing (2)
packages/vite/src/fix-stacktrace.tspackages/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
Merging this PR will not alter performance
Comparing Footnotes
|
|
you mention Vite 7 - but we're on Vite 8 now. is this still relevant? could you provide a regression test? |
Co-authored-by: Daniel Roe <[email protected]>
|
@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 |
vite-node
|
This PR has been merged into It will also be backported to the If you think this change should (or shouldn't) be backported, feel free to leave a comment. Thank you for contributing to Nuxt! 💚 |
Co-authored-by: Askerka00 <[email protected]> Co-authored-by: Daniel Roe <[email protected]>
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: Attachmod.transformResult.mapfromssrServer.environments.ssr.moduleGraphwhen missing during IPCfetchModule.packages/vite/src/fix-stacktrace.ts: Decode IPC sourcemap viaSourceMapConsumer(source-map-js) to map V8 setup line offsets back to original SFC line numbers.Co-authored-by: Daniel Roe [email protected]