From ce7a602edfe1e7121b4e7b05b80e338304dcf579 Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Thu, 3 Apr 2025 12:22:17 -0700 Subject: [PATCH] Add flag to turn off legacy warning (#50249) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/50249 This change introduces a flag to turn off the legacy architecture warning if they become too annoying. The flag can be set in the Info.plist of the React Native architecture and it is controlled by the key: `RCTLegacyWarningsEnabled`. * If the key is missing or with a value of `YES`, logs are enabled * If the key has a value of `NO`, react native will not output any log. We decided to use the Info.plist file to configure the logs because in that way it will work also with React Native prebuilds. ## Changelog: [iOS][Added] - Add flag to enable or disable legacy warning. Reviewed By: cortinico Differential Revision: D71814001 fbshipit-source-id: b6ae6b032ff7add6bae3d73dba490adeaceffa1f --- packages/react-native/React/Base/RCTBridge.mm | 3 ++- packages/react-native/React/Base/RCTUtils.h | 4 ++++ packages/react-native/React/Base/RCTUtils.mm | 12 ++++++++++++ .../React/Fabric/Mounting/RCTComponentViewFactory.mm | 2 +- .../ios/ReactCommon/RCTInteropTurboModule.mm | 4 ++++ 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/react-native/React/Base/RCTBridge.mm b/packages/react-native/React/Base/RCTBridge.mm index d163cd49ccb..3f1cbeb7bb2 100644 --- a/packages/react-native/React/Base/RCTBridge.mm +++ b/packages/react-native/React/Base/RCTBridge.mm @@ -141,7 +141,8 @@ NSMutableArray *getModulesLoadedWithOldArch(void) void RCTRegisterModule(Class); void RCTRegisterModule(Class moduleClass) { - if (RCTIsNewArchEnabled() && ![getCoreModuleClasses() containsObject:[moduleClass description]]) { + if (RCTAreLegacyLogsEnabled() && RCTIsNewArchEnabled() && + ![getCoreModuleClasses() containsObject:[moduleClass description]]) { addModuleLoadedWithOldArch([moduleClass description]); } static dispatch_once_t onceToken; diff --git a/packages/react-native/React/Base/RCTUtils.h b/packages/react-native/React/Base/RCTUtils.h index 90939a8ea55..d4459badcea 100644 --- a/packages/react-native/React/Base/RCTUtils.h +++ b/packages/react-native/React/Base/RCTUtils.h @@ -22,6 +22,10 @@ RCT_EXTERN void RCTSetNewArchEnabled(BOOL enabled) __attribute__((deprecated( "This function is now no-op. You need to modify the Info.plist adding a RCTNewArchEnabled bool property to control whether the New Arch is enabled or not"))); ; +// Whether React native should output logs for modules and components used +// through the interop layers +RCT_EXTERN BOOL RCTAreLegacyLogsEnabled(void); + // JSON serialization/deserialization RCT_EXTERN NSString *__nullable RCTJSONStringify(id __nullable jsonObject, NSError **error); RCT_EXTERN id __nullable RCTJSONParse(NSString *__nullable jsonString, NSError **error); diff --git a/packages/react-native/React/Base/RCTUtils.mm b/packages/react-native/React/Base/RCTUtils.mm index 1223a1ef305..d1ac5cefae3 100644 --- a/packages/react-native/React/Base/RCTUtils.mm +++ b/packages/react-native/React/Base/RCTUtils.mm @@ -52,6 +52,18 @@ void RCTSetNewArchEnabled(BOOL enabled) // whether the New Arch is enabled or not. } +static BOOL _legacyWarningEnabled = true; +BOOL RCTAreLegacyLogsEnabled(void) +{ + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + NSNumber *rctNewArchEnabled = + (NSNumber *)[[NSBundle mainBundle] objectForInfoDictionaryKey:@"RCTLegacyWarningsEnabled"]; + _legacyWarningEnabled = rctNewArchEnabled == nil || rctNewArchEnabled.boolValue; + }); + return _legacyWarningEnabled; +} + static NSString *__nullable _RCTJSONStringifyNoRetry(id __nullable jsonObject, NSError **error) { if (!jsonObject) { diff --git a/packages/react-native/React/Fabric/Mounting/RCTComponentViewFactory.mm b/packages/react-native/React/Fabric/Mounting/RCTComponentViewFactory.mm index 6db166015fb..3db0181332b 100644 --- a/packages/react-native/React/Fabric/Mounting/RCTComponentViewFactory.mm +++ b/packages/react-native/React/Fabric/Mounting/RCTComponentViewFactory.mm @@ -141,7 +141,7 @@ static Class RCTComponentViewClassWithName(const char // TODO(T174674274): Implement lazy loading of legacy view managers in the new architecture. if (RCTFabricInteropLayerEnabled() && [RCTLegacyViewManagerInteropComponentView isSupported:componentNameString]) { RCTLogNewArchitectureValidation( - RCTNotAllowedInFabricWithoutLegacy, + RCTAreLegacyLogsEnabled() ? RCTNotAllowedInFabricWithoutLegacy : RCTNotAllowedInBridgeless, self, [NSString stringWithFormat: diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTInteropTurboModule.mm b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTInteropTurboModule.mm index 802e5a09e10..98df9165c41 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTInteropTurboModule.mm +++ b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTInteropTurboModule.mm @@ -336,6 +336,10 @@ jsi::Value ObjCInteropTurboModule::create(jsi::Runtime &runtime, const jsi::Prop void ObjCInteropTurboModule::_logLegacyArchitectureWarning(NSString *moduleName, const std::string &methodName) { + if (!RCTAreLegacyLogsEnabled()) { + return; + } + std::string separator = std::string("."); std::string moduleInvocation = [moduleName cStringUsingEncoding:NSUTF8StringEncoding] + separator + methodName;