From 2f33eece41182ee8aaee2bf0da8f9933fc63b773 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Fri, 8 Aug 2025 04:22:16 -0700 Subject: [PATCH] Fix retry logic in Fantom when requesting bundles from Metro (#53161) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/53161 Changelog: [internal] This fixes a bug in the retry logic in Fantom when requesting bundles from Metro, where we cache the error from a previous attempt and use it to determine we didn't succeed after the attemps. Reviewed By: rshest Differential Revision: D79881488 fbshipit-source-id: 566b2d700db2f9653b9ea9acd577d7eb03770b76 --- .../react-native-fantom/runner/bundling.js | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/private/react-native-fantom/runner/bundling.js b/private/react-native-fantom/runner/bundling.js index 8a1fa3234c7..0d4077237b3 100644 --- a/private/react-native-fantom/runner/bundling.js +++ b/private/react-native-fantom/runner/bundling.js @@ -22,8 +22,8 @@ type BundleOptions = { const PROJECT_ROOT = path.resolve(__dirname, '..', '..', '..'); export async function createBundle(options: BundleOptions): Promise { - let bundleResult; - let bundleError; + let lastBundleResult; + let lastBundleError; // Retry in case Metro hasn't seen the changes in the filesystem yet. // TODO(T231910841): Remove this when Metro fixes consistency issues when resolving HTTP requests. @@ -33,22 +33,32 @@ export async function createBundle(options: BundleOptions): Promise { await sleep(500); } + lastBundleError = null; + lastBundleResult = null; + try { - bundleResult = await fetch(getBundleURL(options)); + lastBundleResult = await fetch(getBundleURL(options)); } catch (e) { - bundleError = e; + lastBundleError = e; } attemps++; - } while (attemps < 3 && (bundleError || bundleResult?.status === 404)); + } while ( + attemps < 3 && + (lastBundleError || lastBundleResult?.status === 404) + ); - if (bundleError || bundleResult?.ok !== true) { + if (lastBundleError || lastBundleResult?.ok !== true) { throw new Error( - `Failed to request bundle from Metro: ${bundleError?.message ?? (await bundleResult?.text()) ?? ''}`, + `Failed to request bundle from Metro: ${lastBundleError?.message ?? (await lastBundleResult?.text()) ?? ''}`, ); } - await fs.promises.writeFile(options.out, await bundleResult.text(), 'utf8'); + await fs.promises.writeFile( + options.out, + await lastBundleResult.text(), + 'utf8', + ); } export async function createSourceMap(options: BundleOptions): Promise {