Skip to content

Commit dad7f4f

Browse files
authored
fix(runtime): fix sourcemap with prepareStackTrace (#16220)
1 parent d7c5256 commit dad7f4f

File tree

6 files changed

+66
-15
lines changed

6 files changed

+66
-15
lines changed

packages/vite/rollup.config.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -202,21 +202,6 @@ function createRuntimeConfig(isProduction: boolean) {
202202
isProduction ? false : './dist/node',
203203
),
204204
esbuildMinifyPlugin({ minify: false, minifySyntax: true }),
205-
{
206-
name: 'replace bias',
207-
transform(code, id) {
208-
if (id.includes('@jridgewell+trace-mapping')) {
209-
return {
210-
code: code.replaceAll(
211-
'bias === LEAST_UPPER_BOUND',
212-
'true' +
213-
`/*${'bias === LEAST_UPPER_BOUND'.length - '/**/'.length - 'true'.length}*/`,
214-
),
215-
map: null,
216-
}
217-
}
218-
},
219-
},
220205
bundleSizeLimit(45),
221206
],
222207
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function crash(message: string) {
2+
throw new Error(message)
3+
}
4+
5+
export function main(): void {
6+
crash('crash')
7+
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ describe('vite-runtime initialization', async () => {
2121
const serializeStack = (runtime: ViteRuntime, err: Error) => {
2222
return err.stack!.split('\n')[1].replace(runtime.options.root, '<root>')
2323
}
24+
const serializeStackDeep = (runtime: ViteRuntime, err: Error) => {
25+
return err
26+
.stack!.split('\n')
27+
.map((s) => s.replace(runtime.options.root, '<root>'))
28+
}
2429

2530
it('source maps are correctly applied to stack traces', async ({
2631
runtime,
@@ -59,4 +64,16 @@ describe('vite-runtime initialization', async () => {
5964
' at Module.throwError (<root>/fixtures/throws-error-method.ts:11:9)',
6065
)
6166
})
67+
68+
it('deep stacktrace', async ({ runtime }) => {
69+
const methodError = await getError(async () => {
70+
const mod = await runtime.executeUrl('/fixtures/has-error-deep.ts')
71+
mod.main()
72+
})
73+
expect(serializeStackDeep(runtime, methodError).slice(0, 3)).toEqual([
74+
'Error: crash',
75+
' at crash (<root>/fixtures/has-error-deep.ts:2:9)',
76+
' at Module.main (<root>/fixtures/has-error-deep.ts:6:3)',
77+
])
78+
})
6279
})

playground/ssr-html/__tests__/ssr-html.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ describe.runIf(isServe)('stacktrace', () => {
9898
})
9999
}
100100
}
101+
102+
test('with Vite runtime', async () => {
103+
await execFileAsync('node', ['test-stacktrace-runtime.js'], {
104+
cwd: fileURLToPath(new URL('..', import.meta.url)),
105+
})
106+
})
101107
})
102108

103109
describe.runIf(isServe)('network-imports', () => {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function crash(message: string) {
2+
throw new Error(message)
3+
}
4+
5+
export function main(): void {
6+
crash('crash')
7+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { fileURLToPath } from 'node:url'
2+
import assert from 'node:assert'
3+
import { createServer, createViteRuntime } from 'vite'
4+
5+
// same test case as packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts
6+
// implemented for e2e to catch build specific behavior
7+
8+
const server = await createServer({
9+
configFile: false,
10+
root: fileURLToPath(new URL('.', import.meta.url)),
11+
server: {
12+
middlewareMode: true,
13+
},
14+
})
15+
16+
const runtime = await createViteRuntime(server, {
17+
sourcemapInterceptor: 'prepareStackTrace',
18+
})
19+
20+
const mod = await runtime.executeEntrypoint('/src/has-error-deep.ts')
21+
let error
22+
try {
23+
mod.main()
24+
} catch (e) {
25+
error = e
26+
} finally {
27+
await server.close()
28+
}
29+
assert.match(error?.stack, /has-error-deep.ts:6:3/)

0 commit comments

Comments
 (0)