Skip to content

Commit 703f10f

Browse files
Copilotsapphi-red
andcommitted
Add test for PR #21562 - ESM external column numbers with prepareStackTrace
Co-authored-by: sapphi-red <[email protected]>
1 parent 287f990 commit 703f10f

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { outer } from '@vitejs/esm-external-column-test'
2+
3+
function userFn() {
4+
throw new Error('column test error')
5+
}
6+
7+
outer(userFn)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var _padding_______________________
2+
export function outer(fn) {
3+
return inner(fn)
4+
}
5+
function inner(fn) {
6+
return fn()
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "@vitejs/esm-external-column-test",
3+
"private": true,
4+
"type": "module",
5+
"version": "0.0.0",
6+
"main": "index.mjs"
7+
}

packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,38 @@ describe('module runner initialization', async () => {
134134
]
135135
`)
136136
})
137+
138+
it('esm external stack traces should have correct column numbers', async ({
139+
runner,
140+
}) => {
141+
const error = await getError(() =>
142+
runner.import('/fixtures/esm-external-column-test.js'),
143+
)
144+
const stack = error.stack!.split('\n')
145+
const innerFrame = stack.find((line) => line.includes('inner'))
146+
const outerFrame = stack.find((line) => line.includes('outer'))
147+
148+
// The ESM external module has: "var _padding...; export function outer(fn) { return inner(fn); } function inner(fn) { return fn(); }"
149+
// The exact columns depend on how Node.js loads the module.
150+
// The important thing is that they should NOT have 62 subtracted (which was the bug).
151+
// With the fix, columns should be in a reasonable range (> 60 for both).
152+
// Without the fix, columns would be incorrectly reduced by 62.
153+
expect(innerFrame).toBeDefined()
154+
expect(outerFrame).toBeDefined()
155+
156+
// Extract column numbers from stack frames like ":1:114)"
157+
const innerMatch = innerFrame!.match(/:(\d+):(\d+)\)/)
158+
const outerMatch = outerFrame!.match(/:(\d+):(\d+)\)/)
159+
160+
expect(innerMatch).toBeDefined()
161+
expect(outerMatch).toBeDefined()
162+
163+
const innerCol = parseInt(innerMatch![2])
164+
const outerCol = parseInt(outerMatch![2])
165+
166+
// Both columns should be > 60 (with the old bug, they would be around 11 and 52)
167+
// This verifies the 62-character offset is NOT being incorrectly applied
168+
expect(innerCol).toBeGreaterThan(60)
169+
expect(outerCol).toBeGreaterThan(60)
170+
})
137171
})

packages/vite/src/node/ssr/runtime/__tests__/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ export async function createModuleRunnerTester(
4444
ws: false,
4545
},
4646
ssr: {
47-
external: ['@vitejs/cjs-external', '@vitejs/esm-external'],
47+
external: [
48+
'@vitejs/cjs-external',
49+
'@vitejs/esm-external',
50+
'@vitejs/esm-external-column-test',
51+
],
4852
},
4953
optimizeDeps: {
5054
disabled: true,

0 commit comments

Comments
 (0)