From e162ea7698b53f4ecf7b20b1f3f142bdd48586ee Mon Sep 17 00:00:00 2001 From: Rafael Oleza Date: Mon, 5 Feb 2018 21:38:51 -0800 Subject: [PATCH] Break infinite loop when trying to hot patch circular dependencies Reviewed By: BYK Differential Revision: D6853993 fbshipit-source-id: 9e42d207738102e019fccea517411fccd0fb6b1b --- packages/metro/src/lib/polyfills/require.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/metro/src/lib/polyfills/require.js b/packages/metro/src/lib/polyfills/require.js index a1212168..546b7443 100644 --- a/packages/metro/src/lib/polyfills/require.js +++ b/packages/metro/src/lib/polyfills/require.js @@ -47,6 +47,7 @@ type ModuleDefinition = {| verboseName?: string, |}; type ModuleMap = {[key: ModuleID]: ModuleDefinition, __proto__: null}; +type PatchedModules = {[ModuleID]: boolean}; type RequireFn = (id: ModuleID | VerboseModuleNameForDev) => Exports; type VerboseModuleNameForDev = string; @@ -271,7 +272,11 @@ if (__DEV__) { return hot; }; - const acceptAll = function(dependentModules, inverseDependencies) { + const acceptAll = function( + dependentModules, + inverseDependencies, + patchedModules, + ) { if (!dependentModules || dependentModules.length === 0) { return true; } @@ -283,6 +288,7 @@ if (__DEV__) { /*factory*/ undefined, /*dependencyMap*/ undefined, inverseDependencies, + patchedModules, ), ); @@ -304,7 +310,14 @@ if (__DEV__) { factory?: FactoryFn, dependencyMap?: DependencyMap, inverseDependencies: {[key: ModuleID]: Array}, + patchedModules: PatchedModules = {}, ) { + if (id in patchedModules) { + // Do not patch the same module more that once during an update. + return true; + } + patchedModules[id] = true; + const mod = modules[id]; if (!mod && factory) { @@ -342,7 +355,11 @@ if (__DEV__) { } // accept parent modules recursively up until all siblings are accepted - return acceptAll(inverseDependencies[id], inverseDependencies); + return acceptAll( + inverseDependencies[id], + inverseDependencies, + patchedModules, + ); } };