From 8a586f2fa1f43ccfe6089285da26d48f5a861c8b Mon Sep 17 00:00:00 2001 From: Ruslan Lesiutin Date: Wed, 22 Jan 2025 22:06:43 -0800 Subject: [PATCH] Move setUpReactDevTools to InitializeCore (#48871) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48871 # Changelog: [Internal] Forward-fixing D68380665. Requirements: - `setUpReactDevTools` should be called before `setUpErrorHandling` to avoid React DevTools mutating `console.error` call arguments. - `setUpReactDevTools` should be called after `setUpTimers`, because it is using `queueMicrotask`, see https://fb.workplace.com/groups/rn.panelapps/permalink/1120810879540337/. - `setUpTimers` should be called after `polyfillPromise`, because it uses on `global.Promise`. I went over bundles, which are not using `InitializeCore` and using either `setUpErrorHandling` or `setUpDeveloperTools` and updated their order of initialization accordingly. Reviewed By: javache Differential Revision: D68510100 fbshipit-source-id: 4331dcc7a7cb1dc438ca2ed5ccae49e736c41b2a --- .../Libraries/Core/InitializeCore.js | 7 +++++-- .../Libraries/Core/setUpErrorHandling.js | 5 ----- .../Libraries/Core/setUpReactDevTools.js | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/packages/react-native/Libraries/Core/InitializeCore.js b/packages/react-native/Libraries/Core/InitializeCore.js index f01d96e0df1194..5fb0bc191cd20c 100644 --- a/packages/react-native/Libraries/Core/InitializeCore.js +++ b/packages/react-native/Libraries/Core/InitializeCore.js @@ -31,10 +31,13 @@ const start = Date.now(); require('./setUpGlobals'); require('../../src/private/setup/setUpDOM').default(); require('./setUpPerformance'); -require('./setUpErrorHandling'); require('./polyfillPromise'); -require('./setUpRegeneratorRuntime'); require('./setUpTimers'); +if (__DEV__) { + require('./setUpReactDevTools'); +} +require('./setUpErrorHandling'); +require('./setUpRegeneratorRuntime'); require('./setUpXHR'); require('./setUpAlert'); require('./setUpNavigator'); diff --git a/packages/react-native/Libraries/Core/setUpErrorHandling.js b/packages/react-native/Libraries/Core/setUpErrorHandling.js index aa2c368d7dc2ef..c879e388c7519e 100644 --- a/packages/react-native/Libraries/Core/setUpErrorHandling.js +++ b/packages/react-native/Libraries/Core/setUpErrorHandling.js @@ -10,11 +10,6 @@ 'use strict'; -if (__DEV__) { - // React DevTools need to be set up before the console.error patch. - require('./setUpReactDevTools'); -} - if (global.RN$useAlwaysAvailableJSErrorHandling !== true) { /** * Sets up the console and exception handling (redbox) for React Native. diff --git a/packages/react-native/Libraries/Core/setUpReactDevTools.js b/packages/react-native/Libraries/Core/setUpReactDevTools.js index 4d1e026294c533..11a3529ba69d53 100644 --- a/packages/react-native/Libraries/Core/setUpReactDevTools.js +++ b/packages/react-native/Libraries/Core/setUpReactDevTools.js @@ -13,6 +13,22 @@ import type {Domain} from '../../src/private/debugging/setUpFuseboxReactDevToolsDispatcher'; import type {Spec as NativeReactDevToolsRuntimeSettingsModuleSpec} from '../../src/private/fusebox/specs/NativeReactDevToolsRuntimeSettingsModule'; +if (__DEV__) { + if (typeof global.queueMicrotask !== 'function') { + console.error( + 'queueMicrotask should exist before setting up React DevTools.', + ); + } + + // Keep in sync with ExceptionsManager/installConsoleErrorReporter + // $FlowExpectedError[prop-missing] + if (console._errorOriginal != null) { + console.error( + 'ExceptionsManager should be set up after React DevTools to avoid console.error arguments mutation', + ); + } +} + if (__DEV__) { // Register dispatcher on global, which can be used later by Chrome DevTools frontend require('../../src/private/debugging/setUpFuseboxReactDevToolsDispatcher');