Skip to content

Commit

Permalink
fix(ssr): pretty print plugin error in ssrLoadModule (#19290)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Jan 27, 2025
1 parent 97569ef commit 353c467
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
43 changes: 42 additions & 1 deletion packages/vite/src/node/ssr/__tests__/ssrLoadModule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { fileURLToPath } from 'node:url'
import path from 'node:path'
import fs from 'node:fs'
import { stripVTControlCharacters } from 'node:util'
import { expect, test } from 'vitest'
import { expect, onTestFinished, test, vi } from 'vitest'
import { createServer } from '../../server'
import { normalizePath } from '../../utils'

Expand Down Expand Up @@ -251,3 +251,44 @@ test('file url', async () => {
)
expect(modWithSpace.msg).toBe('works')
})

test('plugin error', async () => {
const server = await createServer({
configFile: false,
root,
logLevel: 'error',
plugins: [
{
name: 'test-plugin',
resolveId(source) {
if (source === 'virtual:test') {
return '\0' + source
}
},
load(id) {
if (id === '\0virtual:test') {
return this.error('test-error')
}
},
},
],
})
onTestFinished(() => server.close())

const spy = vi
.spyOn(server.config.logger, 'error')
.mockImplementation(() => {})
try {
await server.ssrLoadModule('virtual:test')
expect.unreachable()
} catch {}
expect(
stripVTControlCharacters(spy.mock.lastCall![0])
.split('\n')
.slice(0, 2)
.join('\n'),
).toMatchInlineSnapshot(`
"Error when evaluating SSR module virtual:test: test-error
Plugin: test-plugin"
`)
})
5 changes: 4 additions & 1 deletion packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { ViteDevServer } from '../server'
import { unwrapId } from '../../shared/utils'
import type { DevEnvironment } from '../server/environment'
import type { NormalizedServerHotChannel } from '../server/hmr'
import { buildErrorMessage } from '../server/middlewares/error'
import { ssrFixStacktrace } from './ssrStacktrace'
import { createServerModuleRunnerTransport } from './runtime/serverModuleRunner'

Expand Down Expand Up @@ -47,7 +48,9 @@ async function instantiateModule(
}

environment.logger.error(
colors.red(`Error when evaluating SSR module ${url}:\n|- ${e.stack}\n`),
buildErrorMessage(e, [
colors.red(`Error when evaluating SSR module ${url}: ${e.message}`),
]),
{
timestamp: true,
clear: environment.config.clearScreen,
Expand Down

0 comments on commit 353c467

Please sign in to comment.