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
This commit is contained in:
Riccardo Cipolleschi
2025-04-03 12:22:17 -07:00
committed by Facebook GitHub Bot
parent 8acc53da57
commit ce7a602edf
5 changed files with 23 additions and 2 deletions
@@ -141,7 +141,8 @@ NSMutableArray<NSString *> *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;
@@ -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);
@@ -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) {
@@ -141,7 +141,7 @@ static Class<RCTComponentViewProtocol> 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:
@@ -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;