From 56c3b69fcd3b113ffc796eb8c36c032ea017d75b Mon Sep 17 00:00:00 2001 From: Mia <49593536+mia-pi-git@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:41:54 -0500 Subject: [PATCH] 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. --- lib/utils.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/utils.ts b/lib/utils.ts index ddc5b704cc34..beeb3f6be3bb 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -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; }