fix: skip watching non-existent public directory#21864
Open
patil-amrut wants to merge 1 commit intovitejs:mainfrom
Open
fix: skip watching non-existent public directory#21864patil-amrut wants to merge 1 commit intovitejs:mainfrom
patil-amrut wants to merge 1 commit intovitejs:mainfrom
Conversation
When publicDir is configured but the directory does not exist, initPublicFiles returns an empty Set (truthy), causing the non-existent path to be added to chokidar's watch list. This silently breaks HMR as chokidar fails to detect file changes. Check publicFiles.size instead of just publicFiles to ensure only existing, non-empty public directories are watched. Fixes vitejs#19864
sapphi-red
requested changes
Mar 23, 2026
Member
sapphi-red
left a comment
There was a problem hiding this comment.
The non-existent path gets added to chokidar's watch list, silently breaking HMR
There’s a logical leap here. Could you elaborate?
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.
When publicDir is configured but the directory does not exist, initPublicFiles returns an empty Set (truthy), causing the non-existent path to be added to chokidar's watch list. This silently breaks HMR as chokidar fails to detect file changes.
Check publicFiles.size instead of just publicFiles to ensure only existing, non-empty public directories are watched.
Fixes #19864
What does this PR solve?
Fixes a bug where Hot Module Replacement (HMR) silently breaks when
publicDiris configured invite.config.jsbut the specified directory does not exist on disk.Why was this approach chosen?
The root cause was in the file watcher setup in
packages/vite/src/node/server/index.ts. WhenpublicDiris configured but doesn't exist:initPublicFiles()callsrecursiveReaddir()which returns[]for non-existent directoriesSet(publicFiles = new Set([]))publicDir && publicFilesevaluates totrueThe fix checks
publicFiles?.sizeinstead of justpublicFilesto ensure only existing, non-empty public directories are watched.Testing
Fixes #19864