Skip to content

fix: verify module identity in cache protocol to prevent stale modules#22081

Open
deyaaeldeen wants to merge 2 commits intovitejs:mainfrom
deyaaeldeen:fix/module-runner-bare-specifier-cache
Open

fix: verify module identity in cache protocol to prevent stale modules#22081
deyaaeldeen wants to merge 2 commits intovitejs:mainfrom
deyaaeldeen:fix/module-runner-bare-specifier-cache

Conversation

@deyaaeldeen
Copy link
Copy Markdown

Problem

Two related bugs in the module runner cause stale modules to be served when multiple versions of a dependency exist in a monorepo:

  1. urlToIdModuleMap keyed by bare specifier: When a bare specifier like @azure/core-lro is 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.

  2. { 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

  • Server: Include the resolved module ID (mod.id) in CachedFetchResult via a new optional id field.
  • Client: When receiving { 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 id field 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

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>
@deyaaeldeen deyaaeldeen force-pushed the fix/module-runner-bare-specifier-cache branch from c12fc82 to 29c3502 Compare March 31, 2026 01:38
- Import HotPayload from 'vite' instead of non-existent '#types/hot'
- Handle null mod.id with nullish coalescing to match string | undefined type
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.

Module runner caches by bare specifier URL, serves wrong version for duplicate dependencies

1 participant