From 3cc24a0f0374b82a80f2d4d7017447a89d14eac4 Mon Sep 17 00:00:00 2001 From: skovhus Date: Sun, 19 Nov 2023 12:31:56 +0100 Subject: [PATCH 1/2] fix(hmr): prevent infinite recursion --- packages/vite/src/node/server/hmr.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index 3aa5996bff9751..13be6ea0e5ff3a 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -361,11 +361,13 @@ function propagateUpdate( * @param nodeChain The chain of nodes/imports that lead to the node. * (The last node in the chain imports the `node` parameter) * @param currentChain The current chain tracked from the `node` parameter + * @param traversedModules The set of modules that have been traversed */ function isNodeWithinCircularImports( node: ModuleNode, nodeChain: ModuleNode[], currentChain: ModuleNode[] = [node], + traversedModules = new Set(), ): HasDeadEnd { // To help visualize how each parameters work, imagine this import graph: // @@ -383,6 +385,12 @@ function isNodeWithinCircularImports( // It works by checking if any `node` importers are within `nodeChain`, which // means there's an import loop with a HMR-accepted module in it. + if (traversedModules.has(node)) { + // To avoid infinite recursion, we only check each module once. + return false + } + traversedModules.add(node) + for (const importer of node.importers) { // Node may import itself which is safe if (importer === node) continue @@ -416,6 +424,7 @@ function isNodeWithinCircularImports( importer, nodeChain, currentChain.concat(importer), + traversedModules, ) if (result) return result } From c9233eb7acf62bf853903aa1ee1481906bb47bbc Mon Sep 17 00:00:00 2001 From: skovhus Date: Mon, 20 Nov 2023 09:34:29 +0100 Subject: [PATCH 2/2] docs: update code documentation --- packages/vite/src/node/server/hmr.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index 13be6ea0e5ff3a..45ba8d5344081f 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -361,7 +361,7 @@ function propagateUpdate( * @param nodeChain The chain of nodes/imports that lead to the node. * (The last node in the chain imports the `node` parameter) * @param currentChain The current chain tracked from the `node` parameter - * @param traversedModules The set of modules that have been traversed + * @param traversedModules The set of modules that have traversed */ function isNodeWithinCircularImports( node: ModuleNode, @@ -386,7 +386,6 @@ function isNodeWithinCircularImports( // means there's an import loop with a HMR-accepted module in it. if (traversedModules.has(node)) { - // To avoid infinite recursion, we only check each module once. return false } traversedModules.add(node)