Skip to content

Commit ce7a602

Browse files
cipolleschifacebook-github-bot
authored andcommitted
Add flag to turn off legacy warning (#50249)
Summary: Pull Request resolved: #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
1 parent 8acc53d commit ce7a602

5 files changed

Lines changed: 23 additions & 2 deletions

File tree

packages/react-native/React/Base/RCTBridge.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ void addModuleLoadedWithOldArch(NSString *moduleName)
141141
void RCTRegisterModule(Class);
142142
void RCTRegisterModule(Class moduleClass)
143143
{
144-
if (RCTIsNewArchEnabled() && ![getCoreModuleClasses() containsObject:[moduleClass description]]) {
144+
if (RCTAreLegacyLogsEnabled() && RCTIsNewArchEnabled() &&
145+
![getCoreModuleClasses() containsObject:[moduleClass description]]) {
145146
addModuleLoadedWithOldArch([moduleClass description]);
146147
}
147148
static dispatch_once_t onceToken;

packages/react-native/React/Base/RCTUtils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ RCT_EXTERN void RCTSetNewArchEnabled(BOOL enabled) __attribute__((deprecated(
2222
"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")));
2323
;
2424

25+
// Whether React native should output logs for modules and components used
26+
// through the interop layers
27+
RCT_EXTERN BOOL RCTAreLegacyLogsEnabled(void);
28+
2529
// JSON serialization/deserialization
2630
RCT_EXTERN NSString *__nullable RCTJSONStringify(id __nullable jsonObject, NSError **error);
2731
RCT_EXTERN id __nullable RCTJSONParse(NSString *__nullable jsonString, NSError **error);

packages/react-native/React/Base/RCTUtils.mm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ void RCTSetNewArchEnabled(BOOL enabled)
5252
// whether the New Arch is enabled or not.
5353
}
5454

55+
static BOOL _legacyWarningEnabled = true;
56+
BOOL RCTAreLegacyLogsEnabled(void)
57+
{
58+
static dispatch_once_t onceToken;
59+
dispatch_once(&onceToken, ^{
60+
NSNumber *rctNewArchEnabled =
61+
(NSNumber *)[[NSBundle mainBundle] objectForInfoDictionaryKey:@"RCTLegacyWarningsEnabled"];
62+
_legacyWarningEnabled = rctNewArchEnabled == nil || rctNewArchEnabled.boolValue;
63+
});
64+
return _legacyWarningEnabled;
65+
}
66+
5567
static NSString *__nullable _RCTJSONStringifyNoRetry(id __nullable jsonObject, NSError **error)
5668
{
5769
if (!jsonObject) {

packages/react-native/React/Fabric/Mounting/RCTComponentViewFactory.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ - (BOOL)registerComponentIfPossible:(const std::string &)name
141141
// TODO(T174674274): Implement lazy loading of legacy view managers in the new architecture.
142142
if (RCTFabricInteropLayerEnabled() && [RCTLegacyViewManagerInteropComponentView isSupported:componentNameString]) {
143143
RCTLogNewArchitectureValidation(
144-
RCTNotAllowedInFabricWithoutLegacy,
144+
RCTAreLegacyLogsEnabled() ? RCTNotAllowedInFabricWithoutLegacy : RCTNotAllowedInBridgeless,
145145
self,
146146
[NSString
147147
stringWithFormat:

packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTInteropTurboModule.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,10 @@ T RCTConvertTo(SEL selector, id json)
336336

337337
void ObjCInteropTurboModule::_logLegacyArchitectureWarning(NSString *moduleName, const std::string &methodName)
338338
{
339+
if (!RCTAreLegacyLogsEnabled()) {
340+
return;
341+
}
342+
339343
std::string separator = std::string(".");
340344

341345
std::string moduleInvocation = [moduleName cStringUsingEncoding:NSUTF8StringEncoding] + separator + methodName;

0 commit comments

Comments
 (0)