Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/vite/src/node/__tests__/plugins/hooks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { type Logger, createLogger } from '../../logger'
import type { Plugin } from '../../plugin'
import { preview } from '../../preview'
import { createServer } from '../../server'
import { DevEnvironment } from '../../server/environment'

const resolveConfigWithPlugin = (
plugin: Plugin,
Expand Down Expand Up @@ -445,6 +446,32 @@ describe('watcher add/unlink error handling', () => {
expect(logError).toHaveBeenCalled()
expect(logError).toHaveBeenCalledWith(error)
})

test("'error' event during environment initialization logs warning instead of crashing", async () => {
const error = Object.assign(new Error('watch failed'), { code: 'EBUSY' })

const logWarn = vi.fn()
const logger = createLogger('error')
logger.warn = logWarn

const init = DevEnvironment.prototype.init
const initSpy = vi
.spyOn(DevEnvironment.prototype, 'init')
.mockImplementation(function (options) {
const watcher = options?.watcher
if (this.name === 'client' && watcher) {
expect(() => watcher.emit('error', error)).not.toThrow()
}
return init.call(this, options)
})
onTestFinished(() => initSpy.mockRestore())

await createServerWithPlugin({ name: 'test' }, logger)

expect(logWarn).toHaveBeenCalledWith(
expect.stringContaining('file watcher error: watch failed'),
)
})
})

describe('closeServer hook', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,10 @@ export async function _createServer(
) as FSWatcher)
: createNoopWatcher(resolvedWatchOptions)

watcher.on('error', (error: Error) => {
config.logger.warn(colors.yellow(`file watcher error: ${error.message}`))
})

const environments: Record<string, DevEnvironment> = {}

await Promise.all(
Expand Down
Loading