fix(runtime): fix sourcemap with prepareStackTrace (#16220)

This commit is contained in:
Hiroshi Ogawa 2024-03-21 18:07:03 +09:00 committed by GitHub
parent d7c5256996
commit dad7f4f5a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 66 additions and 15 deletions

View File

@ -202,21 +202,6 @@ function createRuntimeConfig(isProduction: boolean) {
isProduction ? false : './dist/node',
),
esbuildMinifyPlugin({ minify: false, minifySyntax: true }),
{
name: 'replace bias',
transform(code, id) {
if (id.includes('@jridgewell+trace-mapping')) {
return {
code: code.replaceAll(
'bias === LEAST_UPPER_BOUND',
'true' +
`/*${'bias === LEAST_UPPER_BOUND'.length - '/**/'.length - 'true'.length}*/`,
),
map: null,
}
}
},
},
bundleSizeLimit(45),
],
})

View File

@ -0,0 +1,7 @@
function crash(message: string) {
throw new Error(message)
}
export function main(): void {
crash('crash')
}

View File

@ -21,6 +21,11 @@ describe('vite-runtime initialization', async () => {
const serializeStack = (runtime: ViteRuntime, err: Error) => {
return err.stack!.split('\n')[1].replace(runtime.options.root, '<root>')
}
const serializeStackDeep = (runtime: ViteRuntime, err: Error) => {
return err
.stack!.split('\n')
.map((s) => s.replace(runtime.options.root, '<root>'))
}
it('source maps are correctly applied to stack traces', async ({
runtime,
@ -59,4 +64,16 @@ describe('vite-runtime initialization', async () => {
' at Module.throwError (<root>/fixtures/throws-error-method.ts:11:9)',
)
})
it('deep stacktrace', async ({ runtime }) => {
const methodError = await getError(async () => {
const mod = await runtime.executeUrl('/fixtures/has-error-deep.ts')
mod.main()
})
expect(serializeStackDeep(runtime, methodError).slice(0, 3)).toEqual([
'Error: crash',
' at crash (<root>/fixtures/has-error-deep.ts:2:9)',
' at Module.main (<root>/fixtures/has-error-deep.ts:6:3)',
])
})
})

View File

@ -98,6 +98,12 @@ describe.runIf(isServe)('stacktrace', () => {
})
}
}
test('with Vite runtime', async () => {
await execFileAsync('node', ['test-stacktrace-runtime.js'], {
cwd: fileURLToPath(new URL('..', import.meta.url)),
})
})
})
describe.runIf(isServe)('network-imports', () => {

View File

@ -0,0 +1,7 @@
function crash(message: string) {
throw new Error(message)
}
export function main(): void {
crash('crash')
}

View File

@ -0,0 +1,29 @@
import { fileURLToPath } from 'node:url'
import assert from 'node:assert'
import { createServer, createViteRuntime } from 'vite'
// same test case as packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts
// implemented for e2e to catch build specific behavior
const server = await createServer({
configFile: false,
root: fileURLToPath(new URL('.', import.meta.url)),
server: {
middlewareMode: true,
},
})
const runtime = await createViteRuntime(server, {
sourcemapInterceptor: 'prepareStackTrace',
})
const mod = await runtime.executeEntrypoint('/src/has-error-deep.ts')
let error
try {
mod.main()
} catch (e) {
error = e
} finally {
await server.close()
}
assert.match(error?.stack, /has-error-deep.ts:6:3/)