fix(vite-plugin): prevent Windows file locking by excluding project root from static directories #513
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.
Problem
The plugin was passing the entire Vite project root to Netlify's static file handler, which caused it to
stat()files in Vite's internal directories (e.g.,node_modules/.vite/deps). On Windows, this creates file locks that prevent Vite from atomically renaming temporary dependency files during prebundling, resulting inEPERMerrors when opening Sanity Studio or other apps that trigger Vite's dependency optimization.Root Cause
When Netlify's static handler processes requests, it checks if files exist by calling
stat()on potential paths. By including the entire project root in the static directories list, the handler was checking files in Vite's internal directories likenode_modules/.vite/deps/*.js. On Windows, even a read-onlystat()call can create a file handle that prevents Vite from renaming temporary files (e.g.,deps_tmp→deps), causing the EPERM error.Solution
The fix is to only include
viteDevServer.config.publicDirin the static directories list, not the entire project root. This aligns with Vite's contract that only the public directory contains static assets that should be served verbatim, while everything else (including Vite's internal prebundles, virtual modules, etc.) is Vite's private implementation detail.This change ensures Netlify's static handler never touches Vite's internal files, eliminating the file locking race condition on Windows while maintaining correct behavior for actual static assets.
Changes
viteDevServer.config.rootfrom the static directories listviteDevServer.config.publicDir(if it's a string) plus any user-supplied directoriesFixes #408