fix: avoid null-compiler crash in handleHotUpdate during vite.config restart - #835
Open
HeldvonKosmos wants to merge 1 commit into
Open
HeldvonKosmos wants to merge 1 commit into
HeldvonKosmos wants to merge 1 commit into
Conversation
options.value.compiler is only assigned in the buildStart hook, not in
configResolved. Vite's restartServer() (triggered by any change to
vite.config.*) creates the new watcher and plugin instances via
_createServer(..., { listen: false }) but only calls buildStart later,
inside server.listen(). The gap between those two points — spent
awaiting server.close() of the old server — is a window in which a
file-change event can reach handleHotUpdate on a plugin instance whose
compiler is still null.
handleHotUpdate dereferenced options.value.compiler.invalidateTypeCache
unconditionally, before the file filter was even checked, so it threw
TypeError: null is not an object
(evaluating 'options.value.compiler.invalidateTypeCache')
for any changed file, not just .vue files, surfacing as Vite's HMR
overlay. Reproducible end-to-end with:
touch vite.config.ts; sleep <0.1-0.8>; touch <any file>
on a project whose watched tree is large enough that server.close()
measurably outlasts the restart.
Add an early return so a hot-update callback tolerates being invoked
before buildStart, and a regression test that reproduces the crash
deterministically by calling handleHotUpdate on a fresh plugin
instance before buildStart has run.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
options.value.compileris only assigned in thebuildStarthook, not inconfigResolved. Vite'srestartServer()(triggered by any change tovite.config.*) creates the new watcher and plugininstances via
_createServer(..., { listen: false }), but only callsbuildStartlater, insideserver.listen(). The gap between those two points — spent awaitingserver.close()of the old server— is a window in which a file-change event can reach
handleHotUpdateon a plugin instance whosecompileris stillnull.handleHotUpdatedereferencedoptions.value.compiler.invalidateTypeCacheunconditionally, before the file filter was even checked, so it threw:TypeError: null is not an object
(evaluating 'options.value.compiler.invalidateTypeCache')
for any changed file, not just
.vuefiles, surfacing as Vite's HMR overlay.Reproduction
Deterministic on a project whose watched tree is large enough that server.close() measurably outlasts the restart.
Fix
Add an early return so a hot-update callback tolerates being invoked before buildStart, plus a regression test (handleHotUpdate-race.spec.ts) that reproduces the crash deterministically — no timing
needed — by calling handleHotUpdate on a fresh plugin instance before buildStart has run.