Skip to content

Commit 56c3b69

Browse files
committed
Utils: Ensure require cache clearing is exhaustive
Previously, this was limited by a depth check that always got hit, meaning it never fully properly cleaned out the module tree (most likely due to the fact the uncacheModuleTree call would loop around to the same module through recursion, meaning mod.children never got deleted, so it never stopped looping). This commit fixes that.
1 parent c1b1c86 commit 56c3b69

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

lib/utils.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,12 @@ export function clearRequireCache(options: {exclude?: string[]} = {}) {
314314
}
315315
}
316316

317-
export function uncacheModuleTree(mod: NodeJS.Module, excludes: string[], depth = 0) {
318-
depth++;
319-
if (depth >= 10) return;
320-
if (!mod.children || excludes.some(p => mod.filename.includes(p))) return;
321-
for (const child of mod.children) {
317+
export function uncacheModuleTree(mod: NodeJS.Module, excludes: string[]) {
318+
if (!mod.children?.length || excludes.some(p => mod.filename.includes(p))) return;
319+
for (const [i, child] of mod.children.entries()) {
322320
if (excludes.some(p => child.filename.includes(p))) continue;
323-
uncacheModuleTree(child, excludes, depth);
321+
mod.children?.splice(i, 1);
322+
uncacheModuleTree(child, excludes);
324323
}
325324
delete (mod as any).children;
326325
}

0 commit comments

Comments
 (0)