fix: verify module identity in cache protocol to prevent stale modules#22081
Open
deyaaeldeen wants to merge 2 commits intovitejs:mainfrom
Open
fix: verify module identity in cache protocol to prevent stale modules#22081deyaaeldeen wants to merge 2 commits intovitejs:mainfrom
deyaaeldeen wants to merge 2 commits intovitejs:mainfrom
Conversation
The module runner's `urlToIdModuleMap` caches modules by URL. When a
bare specifier (e.g., `@azure/core-lro`) is used as the URL, the first
resolved version is cached and served to all subsequent importers, even
if they should receive a different physical package.
Additionally, the `{ cache: true }` protocol between the module runner
and server had no identity verification. The server confirms a module
was transformed without checking if the client's cached module matches
the server's resolved module.
Fix:
- Server now includes the resolved module ID in `{ cache: true }`
responses via the new optional `id` field on `CachedFetchResult`.
- Client verifies that its cached module ID matches the server's. On
mismatch, the client refetches the module without the cache flag.
This prevents silent data corruption when multiple versions of a
dependency exist in a monorepo with nested `node_modules`.
Fixes vitejs#22079
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
c12fc82 to
29c3502
Compare
- Import HotPayload from 'vite' instead of non-existent '#types/hot' - Handle null mod.id with nullish coalescing to match string | undefined type
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.
Problem
Two related bugs in the module runner cause stale modules to be served when multiple versions of a dependency exist in a monorepo:
urlToIdModuleMapkeyed by bare specifier: When a bare specifier like@azure/core-lrois used as the URL (because import analysis treated it as external and didn't resolve it), the first resolved version is cached and served to all subsequent importers.{ cache: true }protocol lacks identity verification: When the client sends{ cached: true }, the server confirms the module was transformed and returns{ cache: true }. But the server may have resolved the URL to a different physical package than what the client has cached. The client blindly uses its stale module.Together, these cause silent data corruption where imports get the wrong version of a dependency.
Fix
mod.id) inCachedFetchResultvia a new optionalidfield.{ cache: true, id }, verify that the cached module's normalized ID matches the server's. On mismatch, refetch the module without the cache flag.The
idfield is optional for backward compatibility — existing transports that don't include it continue working as before.Context
Discovered via vitest#10028 in the azure-sdk-for-js monorepo. Related to but independent from #22080 (SSR externalization cache). Both are needed for full correctness, but each fixes a distinct bug.
Fixes #22079