From e65ff84fae1f8f0adcce5bbcbe85f9f156f20830 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Wed, 1 Jul 2026 01:57:49 +0900 Subject: [PATCH] fix(ssr): align named export function call stacktrace column with Node --- .../node/ssr/__tests__/ssrTransform.spec.ts | 55 +++++++++++++++++++ .../fixtures/has-error-toplevel-dep.js | 3 + .../__tests__/fixtures/has-error-toplevel.js | 3 + .../__tests__/server-source-maps.spec.ts | 19 +++++++ packages/vite/src/node/ssr/ssrTransform.ts | 24 +++++++- 5 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 packages/vite/src/node/ssr/runtime/__tests__/fixtures/has-error-toplevel-dep.js create mode 100644 packages/vite/src/node/ssr/runtime/__tests__/fixtures/has-error-toplevel.js diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index 47b94f8179b7e5..6c84d5ec9c4301 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -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') diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/has-error-toplevel-dep.js b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/has-error-toplevel-dep.js new file mode 100644 index 00000000000000..b960ef34d850a9 --- /dev/null +++ b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/has-error-toplevel-dep.js @@ -0,0 +1,3 @@ +export function crash() { + throw new Error('crash') +} diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/has-error-toplevel.js b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/has-error-toplevel.js new file mode 100644 index 00000000000000..391bb5c25076c4 --- /dev/null +++ b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/has-error-toplevel.js @@ -0,0 +1,3 @@ +import { crash } from './has-error-toplevel-dep.js' + +crash() diff --git a/packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts b/packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts index 6caeea61be17b1..ea3c5c8717433f 100644 --- a/packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts +++ b/packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts @@ -104,6 +104,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 (/fixtures/has-error-toplevel-dep.js:2:9)', + ' at /fixtures/has-error-toplevel.js:3:1', + ]) + }) + it('should not crash when sourceMappingURL pattern appears in string literals', async ({ runner, server, diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index bdb0f42802ca45..8ebaa63e8b59a7 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -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. + 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)