Skip to content

Dev server module graph keeps nodes of deleted files for the life of the server #23907

Description

@Sheraff

Investigated and written with the help of an AI coding agent (Claude); reproduced locally.

Describe the bug

When a file that the dev server has transformed is deleted, its module node stays in urlToModuleMap, idToModuleMap and fileToModulesMap (with its transformResult) for the life of the server. onFileDelete only removes the module from its importees' importers.

A re-created path reuses its node, so the graph grows by one node per distinct deleted path: renames, generated files, branch switches. In a long dev session with file-based routing (routes added, renamed, removed) this is a steady leak while the number of live files stays constant.

The repro runs 200 cycles of: create src/gen/mod-<i>.js, import it from src/entry.js, transform both, delete it, wait for the watcher's unlink. At most two files exist at any time.

cycle=   0 liveFiles=1 idToModuleMap=1 urlToModuleMap=1 fileToModulesMap=1 heapUsed=13.4MB
cycle= 100 liveFiles=1 idToModuleMap=101 urlToModuleMap=101 fileToModulesMap=101 heapUsed=14.7MB
cycle= 200 liveFiles=1 idToModuleMap=201 urlToModuleMap=201 fileToModulesMap=201 heapUsed=15.1MB

last deleted file exists on disk: false; still in fileToModulesMap: true; in idToModuleMap: true; in urlToModuleMap: true

Same counts when calling moduleGraph.onFileDelete(file) directly (--direct), and on vite 7.3.7.

Expected: once the deletion is handled, the deleted file's nodes are gone from the maps (sizes stay constant) and their transform results can be collected.

Suggested direction (untested): after the delete HMR update has been computed, also remove the file's modules from fileToModulesMap, urlToModuleMap, idToModuleMap and etagToModuleMap. Related, not duplicates: #14315 (added onFileDelete), #23527 / #23533, #23781.

Reproduction

https://github.com/Sheraff/vite-module-graph-deleted-files

Steps to reproduce

npm install
node --expose-gc repro.mjs

No browser needed.

System Info

System:
  OS: Linux 6.12 Debian GNU/Linux 13 (trixie)
  CPU: (32) x64 AMD Ryzen 9 9955HX 16-Core Processor
Binaries:
  Node: 26.10.0
  npm: 12.2.0
npmPackages:
  vite: 8.3.5 => 8.3.5

Used Package Manager

npm

Logs

No response

Validations

Activity

  1. OKAN-SOFTWARE commented on Oct 29, 2026

    @OKAN-SOFTWARE

    Thanks for adding the repeated-cycle regression test! It confirms that the same module node is reused for a single filename.

    I noticed one additional edge case: onFileDelete() sets mod.transformResult = null directly, bypassing updateModuleTransformResult(), which normally removes the previous ETag from etagToModuleMap.

    Because the new test assigns a different ETag each cycle, older ETag mappings may accumulate even though fileToModulesMap remains at size 1.

    Could we verify that getModuleByEtag() no longer returns the old ETag after deletion, and clean up that mapping if necessary?

    Also, the original #23907 reproduction uses distinct filenames across cycles, whereas this test reuses /test.js. Testing that original scenario would help distinguish reduced source-memory retention from complete graph-map cleanup.

    I also noticed three SSR HMR failures in the latest Windows CI run. I'm not sure whether they're related, so those may need separate verification.

    Thanks for iterating on this!

  2. added
    p2-nice-to-have
    on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    p2-nice-to-have

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions