Skip to content

fix: normalize path in moduleGraph.onFileChange on Windows (fix #22264) - #22265

Open
galsakuri wants to merge 1 commit into
vitejs:mainfrom
galsakuri:fix/22264-normalize-path-onfilechange-windows
Open

galsakuri wants to merge 1 commit into
vitejs:mainfrom
galsakuri:fix/22264-normalize-path-onfilechange-windows

Conversation

@galsakuri

Copy link
Copy Markdown

Ran into #22264 while investigating HMR silently doing nothing on Windows. The root cause is that on Windows, chokidar emits paths with backslashes (C:\project\src\foo.ts), but fileToModulesMap keys are stored with forward slashes. When onFileChange calls getModulesByFile(file) without normalizing first, the Map lookup misses every time and no modules get invalidated.

The fix is one line, file = normalizePath(file) at the top of EnvironmentModuleGraph.onFileChange. normalizePath was already imported in the file (it's used a few lines down in createFileOnlyEntry), so nothing else needed to change. The server-level watcher handler in index.ts already normalizes before calling into the module graph, but onFileChange is a public method and other callers like plugins can reach it directly with raw paths, so the normalization belongs here defensively.

Added two tests: a cross-platform one that passes a double-slash path (/project//src/foo.ts) to prove path.posix.normalize is being applied before the lookup, and a Windows-specific one using an actual backslash path that uses { skip: !isWindows } (same pattern as sourcemap.spec.ts) so it's skipped on macOS/Linux CI but will run on Windows runners.

@galsakuri

Copy link
Copy Markdown
Author

the failing CI check is in hmr-ssr.spec.ts (SSR accept-exports timeout) looks unrelated to this change since it doesn't touch that code path, seems like it might be a flaky test on the Windows runner.

@bluwy

bluwy commented Apr 27, 2026

Copy link
Copy Markdown
Member

Isn't the path already normalized before calling EnvironmentModuleGraph.onFileChange?

const onFileChange = async (file: string) => {
file = normalizePath(file)

Unless the issue is somewhere else invoking EnvironmentModuleGraph.onFileChange without normalizing first. In that case, can you show where is doing that?


If we're looking purely from EnvironmentModuleGraph standpoint and improving its API so it normalizes on behalf, I think that makes sense, in which there's a few other methods that we need updating at the same time. But since this PR tries to fix an HMR issue, I'm trying to understand how this fixes it first.

@galsakuri

Copy link
Copy Markdown
Author

Isn't the path already normalized before calling EnvironmentModuleGraph.onFileChange?

const onFileChange = async (file: string) => {
file = normalizePath(file)

Unless the issue is somewhere else invoking EnvironmentModuleGraph.onFileChange without normalizing first. In that case, can you show where is doing that?

If we're looking purely from EnvironmentModuleGraph standpoint and improving its API so it normalizes on behalf, I think that makes sense, in which there's a few other methods that we need updating at the same time. But since this PR tries to fix an HMR issue, I'm trying to understand how this fixes it first.

you're right, the path is already normalized upstream. I'll extend the fix to all file-accepting methods in EnvironmentModuleGraph (getModulesByFile, onFileChange, onFileDelete) so the API normalizes on behalf of callers, consistent with how createFileOnlyEntry already does it.

@galsakuri
galsakuri force-pushed the fix/22264-normalize-path-onfilechange-windows branch from 8e84eae to 9749194 Compare April 27, 2026 15:38

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants