Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions packages/vite/src/node/server/__tests__/moduleGraph.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,53 @@ describe('moduleGraph', () => {
ssrModule1importersValues[1],
)
})

it('hard-invalidates importers when dependency is deleted (#23527)', async () => {
const moduleGraph = new EnvironmentModuleGraph('client', async (url) => ({
id: url,
}))
const depMod = await moduleGraph.ensureEntryFromUrl('/dep.js', false)
const importerMod = await moduleGraph.ensureEntryFromUrl(
'/main.js',
false,
)

depMod.importers.add(importerMod)
importerMod.importedModules.add(depMod)
importerMod.staticImportedUrls = new Set([depMod.url])

importerMod.transformResult = {
code: 'import { value } from "/dep.js";',
map: null,
etag: '123',
}

// Normal file change soft-invalidates the importer
moduleGraph.onFileChange(depMod.file!)
expect(importerMod.invalidationState).toEqual({
code: 'import { value } from "/dep.js";',
map: null,
etag: '123',
})
expect(depMod.isDeleted).toBe(false)

// Reset importer to simulate fresh transform result
importerMod.invalidationState = undefined
importerMod.transformResult = {
code: 'import { value } from "/dep.js";',
map: null,
etag: '123',
}

// File deletion must hard-invalidate the importer
moduleGraph.onFileDelete(depMod.file!)
expect(depMod.isDeleted).toBe(true)
expect(importerMod.invalidationState).toBe('HARD_INVALIDATED')
expect(importerMod.transformResult).toBe(null)

// Re-ensuring entry resets isDeleted flag
await moduleGraph.ensureEntryFromUrl('/dep.js', false)
expect(depMod.isDeleted).toBe(false)
})
})
})
9 changes: 6 additions & 3 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,9 @@ export async function handleHMRUpdate(
for (const environment of environments) {
const mods = new Set(environment.moduleGraph.getModulesByFile(file))
if (type === 'create') {
for (const mod of mods) {
mod.isDeleted = false
}
for (const mod of environment.moduleGraph._hasResolveFailedErrorModules) {
mods.add(mod)
}
Expand Down Expand Up @@ -828,7 +831,7 @@ function propagateUpdate(
return false
}

if (node.isSelfAccepting) {
if (node.isSelfAccepting && !node.isDeleted) {
// isSelfAccepting is only true for js and css
const boundary = node as EnvironmentModuleNode & { type: 'js' | 'css' }
boundaries.push({
Expand All @@ -844,7 +847,7 @@ function propagateUpdate(
// are used outside of me".
// Also, the imported module (this one) must be updated before the importers,
// so that they do get the fresh imported module when/if they are reloaded.
if (node.acceptedHmrExports) {
if (node.acceptedHmrExports && !node.isDeleted) {
// acceptedHmrExports is only true for js and css
const boundary = node as EnvironmentModuleNode & { type: 'js' | 'css' }
boundaries.push({
Expand All @@ -861,7 +864,7 @@ function propagateUpdate(
for (const importer of node.importers) {
const subChain = [...currentChain, importer]

if (importer.acceptedHmrDeps.has(node)) {
if (importer.acceptedHmrDeps.has(node) && !node.isDeleted) {
// acceptedHmrDeps has value only for js and css
const boundary = importer as EnvironmentModuleNode & {
type: 'js' | 'css'
Expand Down
11 changes: 11 additions & 0 deletions packages/vite/src/node/server/mixedModuleGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,17 @@ export class ModuleNode {
get ssrInvalidationState(): TransformResult | 'HARD_INVALIDATED' | undefined {
return this._ssrModule?.invalidationState
}
get isDeleted(): boolean {
return Boolean(this._clientModule?.isDeleted || this._ssrModule?.isDeleted)
}
set isDeleted(value: boolean) {
if (this._clientModule) {
this._clientModule.isDeleted = value
}
if (this._ssrModule) {
this._ssrModule.isDeleted = value
}
}
}

function mapIterator<T, K = T>(
Expand Down
25 changes: 20 additions & 5 deletions packages/vite/src/node/server/moduleGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ export class EnvironmentModuleNode {
* @internal
*/
staticImportedUrls?: Set<string>
/**
* Whether the module's file has been deleted from disk.
* @internal
*/
isDeleted?: boolean

/**
* @param setIsSelfAccepting - set `false` to set `isSelfAccepting` later. e.g. #7870
Expand Down Expand Up @@ -147,6 +152,7 @@ export class EnvironmentModuleGraph {
if (mods) {
const seen = new Set<EnvironmentModuleNode>()
mods.forEach((mod) => {
mod.isDeleted = false
this.invalidateModule(mod, seen)
})
}
Expand All @@ -155,10 +161,13 @@ export class EnvironmentModuleGraph {
onFileDelete(file: string): void {
const mods = this.getModulesByFile(file)
if (mods) {
const seen = new Set<EnvironmentModuleNode>()
mods.forEach((mod) => {
mod.isDeleted = true
mod.importedModules.forEach((importedMod) => {
importedMod.importers.delete(mod)
})
this.invalidateModule(mod, seen)
})
}
}
Expand Down Expand Up @@ -211,13 +220,16 @@ export class EnvironmentModuleGraph {
mod.ssrError = null

mod.importers.forEach((importer) => {
if (!importer.acceptedHmrDeps.has(mod)) {
if (!importer.acceptedHmrDeps.has(mod) || mod.isDeleted) {
// If the importer statically imports the current module, we can soft-invalidate the importer
// to only update the import timestamps. If it's not statically imported, e.g. watched/glob file,
// we can only soft invalidate if the current module was also soft-invalidated. A soft-invalidation
// doesn't need to trigger a re-load and re-transform of the importer.
// But we exclude direct CSS files as those cannot be soft invalidated.
// We also cannot soft-invalidate importers if the current module was deleted, as importers
// must be re-transformed to re-resolve the deleted import specifier.
const shouldSoftInvalidateImporter =
!mod.isDeleted &&
(importer.staticImportedUrls?.has(mod.url) || softInvalidate) &&
importer.type === 'js'
this.invalidateModule(
Expand Down Expand Up @@ -349,7 +361,7 @@ export class EnvironmentModuleGraph {
// Quick path, if we already have a module for this rawUrl (even without extension)
rawUrl = removeImportQuery(removeTimestampQuery(rawUrl))
let mod = this._getUnresolvedUrlToModule(rawUrl)
if (mod) {
if (mod && !('then' in mod ? false : mod.isDeleted)) {
return mod
}
const modPromise = (async () => {
Expand All @@ -375,8 +387,11 @@ export class EnvironmentModuleGraph {
}
// multiple urls can map to the same module and id, make sure we register
// the url to the existing module in that case
else if (!this.urlToModuleMap.has(url)) {
this.urlToModuleMap.set(url, mod)
else {
mod.isDeleted = false
if (!this.urlToModuleMap.has(url)) {
this.urlToModuleMap.set(url, mod)
}
}
this._setUnresolvedUrlToModule(rawUrl, mod)
return mod
Expand Down Expand Up @@ -421,7 +436,7 @@ export class EnvironmentModuleGraph {
async resolveUrl(url: string): Promise<ResolvedUrl> {
url = removeImportQuery(removeTimestampQuery(url))
const mod = await this._getUnresolvedUrlToModule(url)
if (mod?.id) {
if (mod?.id && !mod.isDeleted) {
return [mod.url, mod.id, mod.meta]
}
return this._resolveUrl(url)
Expand Down
Loading