Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime): fix sourcemap with prepareStackTrace #16220

Merged
merged 7 commits into from Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 0 additions & 15 deletions packages/vite/rollup.config.ts
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
@@ -0,0 +1,7 @@
function crash(message: string) {
throw new Error(message)
}

export function main(): void {
crash('crash')
}
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
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
@@ -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
@@ -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/)