From b86c9483ca5995a15c1aeeb8ddafe2ea0c9db392 Mon Sep 17 00:00:00 2001 From: Zeya Peng Date: Tue, 14 Jul 2026 17:11:07 +0100 Subject: [PATCH] Bootstrap Metro from InitializeCore until setup-env is graph-reachable Summary: Fresh projects redbox on launch (e.g. "Failed to call into JavaScript module method RCTDeviceEventEmitter.emit() / HMRClient.setup(). Module has not been registered as callable. Registered callable JavaScript modules (n = 1): AppRegistry."). Core initialization never runs. Metro's `getModulesRunBeforeMainModule` (metro/src/lib/getAppendScripts.js) only emits the run-before `__r()` call for a module that is *already present in the bundle graph*, and silently skips it otherwise. #57475 switched the run-before target from `Libraries/Core/InitializeCore` to `src/setup-env.js`. `InitializeCore` is reachable in the graph (imported by `Libraries/ReactPrivate/ReactNativePrivateInitializeCore.js`); `src/setup-env.js` is imported by nothing, so Metro skips it and core init never runs. The two modules are functionally identical (both call `setUpDefaultReactNativeEnvironment().default()`). Evidence (bundle tail): - setup-env target: `__r(0);` (broken) - InitializeCore target: `__r(); __r(0);` (fixed) This is an INTERIM stopgap. It only changes the internal Metro bootstrap target back to `InitializeCore`; the public `react-native/setup-env` entry point and its deprecation of InitializeCore are unchanged. The durable fix is to make `setup-env` graph-reachable (or make Metro treat `getModulesRunBeforeMainModule` entries as graph roots), which will then allow InitializeCore to be removed. Changelog: [General][Fixed] - Fix apps failing to boot ("... not registered as callable") caused by core init not running. Test Plan: - `yarn test-release-local -t RNTestProject -p iOS`: app boots without redbox. - Bundle tail contains `__r(); __r(0);` instead of just `__r(0);`. --- packages/community-cli-plugin/src/utils/loadMetroConfig.js | 6 +++--- packages/metro-config/src/index.flow.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/community-cli-plugin/src/utils/loadMetroConfig.js b/packages/community-cli-plugin/src/utils/loadMetroConfig.js index 9b6a79ee104..7e2f5c3d002 100644 --- a/packages/community-cli-plugin/src/utils/loadMetroConfig.js +++ b/packages/community-cli-plugin/src/utils/loadMetroConfig.js @@ -58,15 +58,15 @@ function getCommunityCliDefaultConfig( return { resolver, serializer: { - // We can include multiple copies of setup-env here because Metro will + // We can include multiple copies of InitializeCore here because metro will // only add ones that are already part of the bundle getModulesRunBeforeMainModule: () => [ - require.resolve('react-native/setup-env', { + require.resolve('react-native/Libraries/Core/InitializeCore', { paths: [ctx.root], }), ...outOfTreePlatforms.map(platform => require.resolve( - `${ctx.platforms[platform].npmPackageName}/setup-env`, + `${ctx.platforms[platform].npmPackageName}/Libraries/Core/InitializeCore`, {paths: [ctx.root]}, ), ), diff --git a/packages/metro-config/src/index.flow.js b/packages/metro-config/src/index.flow.js index 2f4e44c187b..bb4f611e537 100644 --- a/packages/metro-config/src/index.flow.js +++ b/packages/metro-config/src/index.flow.js @@ -61,7 +61,7 @@ export function getDefaultConfig(projectRoot: string): ConfigT { serializer: { // NOTE: Overridden in community-cli-plugin getModulesRunBeforeMainModule: () => [ - require.resolve('react-native/setup-env'), + require.resolve('react-native/Libraries/Core/InitializeCore'), ], getPolyfills: () => require('@react-native/js-polyfills')(), isThirdPartyModule({path: modulePath}: Readonly<{path: string, ...}>) {