Skip to content

Commit

Permalink
fix(runtime): fix sourcemap with prepareStackTrace (#16220)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Mar 21, 2024
1 parent d7c5256 commit dad7f4f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 15 deletions.
15 changes: 0 additions & 15 deletions packages/vite/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function crash(message: string) {
throw new Error(message)
}

export function main(): void {
crash('crash')
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)',
])
})
})
6 changes: 6 additions & 0 deletions playground/ssr-html/__tests__/ssr-html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
7 changes: 7 additions & 0 deletions playground/ssr-html/src/has-error-deep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function crash(message: string) {
throw new Error(message)
}

export function main(): void {
crash('crash')
}
29 changes: 29 additions & 0 deletions playground/ssr-html/test-stacktrace-runtime.js
Original file line number Diff line number Diff line change
@@ -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/)

0 comments on commit dad7f4f

Please sign in to comment.