Skip to content

Commit d035290

Browse files
authored
fix: rebuild module imports after content entry deletion (#17713)
* fix: rebuild module imports after content entry deletion (#17707) * test: exercise debounced module-import trigger on delete/clear/clearAll (#17707) Prior tests called writeModuleImports() explicitly right after delete/clear, which forced the rebuild to run synchronously regardless of whether the new #writeModulesImportsDebounced() calls inside delete()/clear()/clearAll() were wired correctly. Reverting those three trigger lines still passed all existing assertions. Rewrite the delete/clear tests to rely on waitUntilSaveComplete() alone, add a clearAll()-specific test, and add a rename test (delete(oldId) + set(newId)) matching the issue's actual reported scenario. Verified: reverting the three trigger lines makes the delete/clear/clearAll tests fail; restoring them makes all pass. * fix: return early after writing an empty content-modules.mjs The zero-size branch in writeModuleImports() fell through to the generator below it, writing the file twice on every call where #moduleImports ends up empty (e.g. after deleting the last deferred-render entry) -- first 'export default new Map();', then a second, differently-formatted empty-map write. That path only became reachable once #moduleImports could shrink back to zero, which this PR introduces. Add the missing return, matching the pattern review comment. Also note in the changeset that addModuleImport() callers without a backing deferredRender entry no longer survive a write, now that #moduleImports is fully derived state.
1 parent f88c875 commit d035290

3 files changed

Lines changed: 175 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes `content-modules.mjs` not removing entries for deleted or renamed content files, which could cause Vite to attempt to resolve non-existent modules
6+
7+
As part of this fix, `#moduleImports` is now fully rebuilt from `deferredRender` entries before every write, so a module import added only through the public `addModuleImport()` API without a corresponding `deferredRender` entry in the store will no longer be preserved across writes.

‎packages/astro/src/content/mutable-data-store.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,22 @@ export class MutableDataStore extends ImmutableDataStore {
5959
collection.delete(String(key));
6060
this.#saveToDiskDebounced();
6161
this.#writeAssetsImportsDebounced();
62+
this.#writeModulesImportsDebounced();
6263
}
6364
}
6465

6566
clear(collectionName: string) {
6667
this._collections.delete(collectionName);
6768
this.#saveToDiskDebounced();
6869
this.#writeAssetsImportsDebounced();
70+
this.#writeModulesImportsDebounced();
6971
}
7072

7173
clearAll() {
7274
this._collections.clear();
7375
this.#saveToDiskDebounced();
7476
this.#writeAssetsImportsDebounced();
77+
this.#writeModulesImportsDebounced();
7578
}
7679

7780
addAssetImport(assetImport: string, filePath?: string) {
@@ -123,6 +126,27 @@ export class MutableDataStore extends ImmutableDataStore {
123126
}
124127
}
125128

129+
/**
130+
* Rebuilds #moduleImports from the current entries in _collections.
131+
* This ensures stale module entries are removed when content files are
132+
* deleted or renamed, preventing Vite from attempting to resolve
133+
* non-existent files listed in content-modules.mjs.
134+
*/
135+
#rebuildModuleImports() {
136+
this.#moduleImports.clear();
137+
for (const collection of this._collections.values()) {
138+
for (const entry of collection.values()) {
139+
const typedEntry = entry as DataEntry;
140+
if (typedEntry.deferredRender && typedEntry.filePath) {
141+
const id = contentModuleToId(typedEntry.filePath);
142+
if (id) {
143+
this.#moduleImports.set(typedEntry.filePath, id);
144+
}
145+
}
146+
}
147+
}
148+
}
149+
126150
async writeAssetImports(filePath: PathLike) {
127151
this.#assetsFile = filePath;
128152
this.#rebuildAssetImports();
@@ -164,13 +188,15 @@ export default new Map([${exports.join(', ')}]);
164188

165189
async writeModuleImports(filePath: PathLike) {
166190
this.#modulesFile = filePath;
191+
this.#rebuildModuleImports();
167192

168193
if (this.#moduleImports.size === 0) {
169194
try {
170195
await this.#writeFileAtomic(filePath, 'export default new Map();');
171196
} catch (err) {
172197
throw new AstroError(AstroErrorData.UnknownFilesystemError, { cause: err });
173198
}
199+
return;
174200
}
175201

176202
if (!this.#modulesDirty && existsSync(filePath)) {

‎packages/astro/test/units/content-collections/mutable-data-store.test.ts‎

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,148 @@ describe('MutableDataStore', () => {
128128
);
129129
});
130130

131+
it('removes stale module imports when an entry is deleted (via debounced write trigger)', async () => {
132+
const modulesFilePath = path.join(tmpDir, 'content-modules-delete.mjs');
133+
const store = new MutableDataStore();
134+
const scoped = store.scopedStore('docs');
135+
136+
scoped.set({
137+
id: 'page-a',
138+
data: {},
139+
filePath: 'src/content/docs/page-a.mdx',
140+
deferredRender: true,
141+
});
142+
143+
scoped.set({
144+
id: 'page-b',
145+
data: {},
146+
filePath: 'src/content/docs/page-b.mdx',
147+
deferredRender: true,
148+
});
149+
150+
await store.writeModuleImports(modulesFilePath);
151+
const contentBefore = await fs.readFile(modulesFilePath, 'utf-8');
152+
assert.ok(contentBefore.includes('page-a.mdx'), 'should contain page-a before deletion');
153+
assert.ok(contentBefore.includes('page-b.mdx'), 'should contain page-b before deletion');
154+
155+
// Do NOT call writeModuleImports() again here: that would rebuild and write
156+
// synchronously regardless of whether delete() actually schedules a rewrite.
157+
// Rely solely on the debounced trigger that delete() is supposed to schedule,
158+
// flushed via waitUntilSaveComplete(), so this test exercises the real
159+
// dev-server code path (a filesystem delete triggers a rewrite on its own).
160+
scoped.delete('page-a');
161+
await store.waitUntilSaveComplete();
162+
163+
const contentAfter = await fs.readFile(modulesFilePath, 'utf-8');
164+
assert.ok(
165+
!contentAfter.includes('page-a.mdx'),
166+
'should NOT contain page-a after the entry is deleted',
167+
);
168+
assert.ok(contentAfter.includes('page-b.mdx'), 'should still contain page-b');
169+
});
170+
171+
it('removes stale module imports when a collection is cleared (via debounced write trigger)', async () => {
172+
const modulesFilePath = path.join(tmpDir, 'content-modules-clear.mjs');
173+
const store = new MutableDataStore();
174+
const scoped = store.scopedStore('docs');
175+
176+
scoped.set({
177+
id: 'page-1',
178+
data: {},
179+
filePath: 'src/content/docs/page-1.mdx',
180+
deferredRender: true,
181+
});
182+
183+
await store.writeModuleImports(modulesFilePath);
184+
const contentBefore = await fs.readFile(modulesFilePath, 'utf-8');
185+
assert.ok(contentBefore.includes('page-1.mdx'), 'should contain page-1 before clear');
186+
187+
scoped.clear();
188+
await store.waitUntilSaveComplete();
189+
190+
const contentAfter = await fs.readFile(modulesFilePath, 'utf-8');
191+
assert.ok(
192+
!contentAfter.includes('page-1.mdx'),
193+
'should NOT contain page-1 after the collection is cleared',
194+
);
195+
});
196+
197+
it('removes stale module imports when the entire store is cleared via clearAll (via debounced write trigger)', async () => {
198+
const modulesFilePath = path.join(tmpDir, 'content-modules-clear-all.mjs');
199+
const store = new MutableDataStore();
200+
const docsScoped = store.scopedStore('docs');
201+
const blogScoped = store.scopedStore('blog');
202+
203+
docsScoped.set({
204+
id: 'page-1',
205+
data: {},
206+
filePath: 'src/content/docs/page-1.mdx',
207+
deferredRender: true,
208+
});
209+
blogScoped.set({
210+
id: 'post-1',
211+
data: {},
212+
filePath: 'src/content/blog/post-1.mdx',
213+
deferredRender: true,
214+
});
215+
216+
await store.writeModuleImports(modulesFilePath);
217+
const contentBefore = await fs.readFile(modulesFilePath, 'utf-8');
218+
assert.ok(contentBefore.includes('page-1.mdx'), 'should contain page-1 before clearAll');
219+
assert.ok(contentBefore.includes('post-1.mdx'), 'should contain post-1 before clearAll');
220+
221+
store.clearAll();
222+
await store.waitUntilSaveComplete();
223+
224+
const contentAfter = await fs.readFile(modulesFilePath, 'utf-8');
225+
assert.ok(
226+
!contentAfter.includes('page-1.mdx'),
227+
'should NOT contain page-1 after clearAll',
228+
);
229+
assert.ok(
230+
!contentAfter.includes('post-1.mdx'),
231+
'should NOT contain post-1 after clearAll',
232+
);
233+
});
234+
235+
it('removes the old module import and adds the new one when an entry is renamed (issue #17707)', async () => {
236+
// A rename is how glob.ts actually models it: delete the old id, then set the new one.
237+
const modulesFilePath = path.join(tmpDir, 'content-modules-rename.mjs');
238+
const store = new MutableDataStore();
239+
const scoped = store.scopedStore('docs');
240+
241+
scoped.set({
242+
id: 'otp',
243+
data: {},
244+
filePath: 'src/content/docs/otp.mdx',
245+
deferredRender: true,
246+
});
247+
248+
await store.writeModuleImports(modulesFilePath);
249+
const contentBefore = await fs.readFile(modulesFilePath, 'utf-8');
250+
assert.ok(contentBefore.includes('otp.mdx'), 'should contain the original file before rename');
251+
252+
// Rename: delete the old entry, then set the new one under a new id/filePath.
253+
scoped.delete('otp');
254+
scoped.set({
255+
id: 'the-otp',
256+
data: {},
257+
filePath: 'src/content/docs/the-otp.mdx',
258+
deferredRender: true,
259+
});
260+
await store.waitUntilSaveComplete();
261+
262+
const contentAfter = await fs.readFile(modulesFilePath, 'utf-8');
263+
assert.ok(
264+
!contentAfter.includes('"src/content/docs/otp.mdx"'),
265+
'should NOT reference the old (renamed-away) file path',
266+
);
267+
assert.ok(
268+
contentAfter.includes('the-otp.mdx'),
269+
'should reference the new (renamed-to) file path',
270+
);
271+
});
272+
131273
it('reproduces race condition: concurrent writeToDisk() calls lose data', async () => {
132274
const filePath = pathToFileURL(path.join(tmpDir, 'data-store.json'));
133275
const store = await MutableDataStore.fromFile(filePath);

0 commit comments

Comments
 (0)