Skip to content

Module runner: deleting a pruned self-accepting module logs "Failed to reload" #24140

Description

@Sheraff

Written with the help of an AI coding agent (Claude); reproduced locally.

Describe the bug

In the module runner, a self-accepting module that is no longer imported (the server sent prune for it) still
reacts to updates. Deleting its file then makes the runner fetch the deleted URL:

[vite] (ssr) hmr update /left.js
[vite] (ssr) Failed to load url /left.js (resolved id: /…/left.js). Does the file exist?
[vite] (ssr) Failed to reload /left.js. This could be due to syntax errors or importing non-existent modules. (see errors above)

Nothing imports /left.js anymore, so I'd expect the delete to be silent.

Reproduction

npm i vite@8.3.8, then node repro.mjs:

// repro.mjs
import fs from 'node:fs'
import { createServer, createServerModuleRunner } from 'vite'

const root = new URL('./app/', import.meta.url).pathname
fs.rmSync(root, { recursive: true, force: true })
fs.mkdirSync(root)
const write = (f, code) => fs.writeFileSync(root + f, code)
const wait = (ms) => new Promise((r) => setTimeout(r, ms))
const mod = (tag) =>
  `export const tag = '${tag}'\nif (import.meta.hot) import.meta.hot.accept(() => {})\n`

write('left.js', mod('left'))
write('entry.js', `export { tag } from './left.js'\n`)

const server = await createServer({ root, configFile: false, server: { middlewareMode: true, ws: false } })
const runner = createServerModuleRunner(server.environments.ssr)
console.log((await runner.import('/entry.js')).tag) // left

write('right.js', mod('right'))
write('entry.js', `export { tag } from './right.js'\n`) // server sends `prune /left.js`
await wait(800)

fs.rmSync(root + 'left.js') // server sends `update /left.js` -> "Failed to reload /left.js"
await wait(1500)

console.log((await runner.import('/entry.js')).tag) // right
await server.close()

Deleting left.js while entry.js still imports it errors too; that one is expected.

Expected

No update and no error for a module the runner has pruned.

Suggested fix

Forget the module's HMR registration on prune. In HMRClient.prunePaths: paths.forEach((path) => { this.dataMap.delete(path); this.hotModulesMap.delete(path); this.disposeMap.delete(path); this.pruneMap.delete(path) }). With that patched into the runner, the delete above is silent, and the still-imported case still errors.

System Info

vite 8.3.8 and 8.4.0-beta.1 (also 8.1.7 and 7.3.7), Node 26.12.1, Linux x64. No plugins.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions