Skip to content

Commit

Permalink
Utils: Ensure require cache clearing is exhaustive
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mia-pi-git committed Apr 22, 2024
1 parent c1b1c86 commit 56c3b69
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,12 @@ export function clearRequireCache(options: {exclude?: string[]} = {}) {
}
}

export function uncacheModuleTree(mod: NodeJS.Module, excludes: string[], depth = 0) {
depth++;
if (depth >= 10) return;
if (!mod.children || excludes.some(p => mod.filename.includes(p))) return;
for (const child of mod.children) {
export function uncacheModuleTree(mod: NodeJS.Module, excludes: string[]) {
if (!mod.children?.length || excludes.some(p => mod.filename.includes(p))) return;
for (const [i, child] of mod.children.entries()) {
if (excludes.some(p => child.filename.includes(p))) continue;
uncacheModuleTree(child, excludes, depth);
mod.children?.splice(i, 1);
uncacheModuleTree(child, excludes);
}
delete (mod as any).children;
}
Expand Down

0 comments on commit 56c3b69

Please sign in to comment.