Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,61 @@ test('sourcemap is correct for hoisted imports', async () => {
})
})

test('sourcemap call site of imported binding maps to callee start', async () => {
// When calling an imported binding, the call is wrapped with `(0, ...)` to
// avoid binding `this`. V8 anchors the call-site stack frame to the
// argument list `(` of such a parenthesized callee. The sourcemap should map
// that `(` back to the start of the original callee identifier (matching
// Node), instead of the `(`'s own column. See #19625.
const wrap = '(0,__vite_ssr_import_0__.fn)'

// map the call's argument list `(` (the first `(` after the `(0, ...)` wrap)
const mapArgParen = (generated: string, map: unknown) => {
const afterWrap = generated.indexOf(wrap) + wrap.length
const parenIndex = generated.indexOf('(', afterWrap)
let line = 1
let column = 0
for (let i = 0; i < parenIndex; i++) {
if (generated[i] === '\n') {
line++
column = 0
} else {
column++
}
}
return originalPositionFor(new TraceMap(map as any), { line, column })
}

// The callee `fn` always starts at line 2, column 0. Whitespace and comments
// between the callee and the `(` must not change where the frame points.
for (const call of [
'fn()',
'fn ()',
'fn /* c */ ()',
'fn\n()',
'fn(1)',
'fn /* c */ (1)',
]) {
const code = `import { fn } from 'vue'\n${call}`
const result = (await ssrTransform(code, null, 'input.js', code))!
expect(mapArgParen(result.code, result.map), call).toMatchObject({
source: 'input.js',
line: 2,
column: 0,
})
}

// Optional calls are anchored to the `(` by Node itself (not the callee), so
// they must keep mapping to the original `(` (here line 2, column 4).
const code = `import { fn } from 'vue'\nfn?.()`
const result = (await ssrTransform(code, null, 'input.js', code))!
expect(mapArgParen(result.code, result.map)).toMatchObject({
source: 'input.js',
line: 2,
column: 4,
})
})

test('sourcemap with multiple sources', async () => {
const code = readFixture('bundle.js')
const map = readFixture('bundle.js.map')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function crash() {
throw new Error('crash')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { crash } from './has-error-toplevel-dep.js'

crash()
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ describe('module runner initialization', async () => {
])
})

it('call site of an imported binding matches Node column', async ({
runner,
server,
}) => {
// Calls to imported bindings are wrapped with `(0, ...)` to avoid binding
// `this`. V8 anchors the call-site stack frame of such a parenthesized
// callee to the argument list `(`, but the frame should still point to the
// start of the callee (matching Node), i.e. column 1 here, not the `(`.
// See #19625.
const error = await getError(() =>
runner.import('/fixtures/has-error-toplevel.js'),
)
expect(serializeStackDeep(server, error).slice(0, 3)).toEqual([
'Error: crash',
' at crash (<root>/fixtures/has-error-toplevel-dep.js:2:9)',
' at <root>/fixtures/has-error-toplevel.js:3:1',
])
})

it('should not crash when sourceMappingURL pattern appears in string literals', async ({
runner,
server,
Expand Down
24 changes: 21 additions & 3 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,29 @@ async function ssrTransformScript(
s.prependRight(topNode.start, `const ${id.name} = ${binding};\n`)
}
} else if (parent.type === 'CallExpression') {
s.update(id.start, id.end, binding)
// wrap with (0, ...) to avoid method binding `this`
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors#method_binding
s.prependRight(id.start, `(0,`)
s.appendLeft(id.end, `)`)
if (id === parent.callee && !parent.optional) {
// V8 anchors the call-site stack frame of a parenthesized callee to
// the argument list `(`. Absorb everything from the callee up to the
// first argument (or the call's end for an empty argument list) into
// the replaced chunk so that `(` maps back to the start of the callee,
// keeping stack trace columns aligned with Node (which points to the
// callee). Optional calls (`?.()`) are excluded because Node itself
// anchors them to the `(`. See #19625.
Comment on lines +375 to +381

@hi-ogawa hi-ogawa Jul 6, 2026 •

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.

Not sure how much trusted but AI said this adjustment happens here https://github.com/v8/v8/blob/0c7a9d0d8c05491e8053d7259a1a6b11e75c1907/src/parsing/parser-base.h#L4037-L4043

The comment calls out the case of IIFE function(){}(), but parenthesized case seems to also hit this case.

Also, it looks like bun isn't affected (and already correct) since they already have different callee mapping approach that doesn't rely on argument list (.

const argsStart = parent.arguments.length
? parent.arguments[0].start
: parent.end
s.update(
id.start,
argsStart,
`(0,${binding})${code.slice(id.end, argsStart)}`,
)
} else {
s.update(id.start, id.end, binding)
s.prependRight(id.start, `(0,`)
s.appendLeft(id.end, `)`)
}
} else if (
// don't transform class name identifier
!(parent.type === 'ClassExpression' && id === parent.id)
Expand Down
Loading