diff --git a/packages/vite/rollup.config.ts b/packages/vite/rollup.config.ts index 7a0a3305b6b794..716e75dbe2f48b 100644 --- a/packages/vite/rollup.config.ts +++ b/packages/vite/rollup.config.ts @@ -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), ], }) diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/has-error-deep.ts b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/has-error-deep.ts new file mode 100644 index 00000000000000..8da094a3fa4800 --- /dev/null +++ b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/has-error-deep.ts @@ -0,0 +1,7 @@ +function crash(message: string) { + throw new Error(message) +} + +export function main(): void { + crash('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 c482a0464827f4..fd8973235af0b6 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 @@ -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, '') } + const serializeStackDeep = (runtime: ViteRuntime, err: Error) => { + return err + .stack!.split('\n') + .map((s) => s.replace(runtime.options.root, '')) + } it('source maps are correctly applied to stack traces', async ({ runtime, @@ -59,4 +64,16 @@ describe('vite-runtime initialization', async () => { ' at Module.throwError (/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 (/fixtures/has-error-deep.ts:2:9)', + ' at Module.main (/fixtures/has-error-deep.ts:6:3)', + ]) + }) }) diff --git a/playground/ssr-html/__tests__/ssr-html.spec.ts b/playground/ssr-html/__tests__/ssr-html.spec.ts index fa9d12c7f6da41..92a2713420f2c3 100644 --- a/playground/ssr-html/__tests__/ssr-html.spec.ts +++ b/playground/ssr-html/__tests__/ssr-html.spec.ts @@ -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', () => { diff --git a/playground/ssr-html/src/has-error-deep.ts b/playground/ssr-html/src/has-error-deep.ts new file mode 100644 index 00000000000000..8da094a3fa4800 --- /dev/null +++ b/playground/ssr-html/src/has-error-deep.ts @@ -0,0 +1,7 @@ +function crash(message: string) { + throw new Error(message) +} + +export function main(): void { + crash('crash') +} diff --git a/playground/ssr-html/test-stacktrace-runtime.js b/playground/ssr-html/test-stacktrace-runtime.js new file mode 100644 index 00000000000000..c2b8f670b5a089 --- /dev/null +++ b/playground/ssr-html/test-stacktrace-runtime.js @@ -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/)