Skip to content

Generate RCTThirdPartyComponents.mm with platform-specific macros #50637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

cgoldsby
Copy link

Summary:

iOS crashes in the New Architecture because Codegen includes all installed packages — even ones that don’t support iOS. The Legacy Architecture uses a different code generator that doesn’t have this issue. In both architectures, CocoaPods correctly excludes platform-specific native code — but Codegen doesn’t respect those exclusions in the New Architecture.

To prevent this, a macro was originally introduced to guard Objective-C code in generated components:
#42047

However, that macro usage was inadvertently lost during a later refactor that moved the Codegen provider to a static template:
#47518

This PR restores those platform guard macros in RCTThirdPartyComponentsProvider, ensuring that unsupported packages do not cause runtime crashes on iOS:

		@"RNSVGTextPath": NSClassFromString(@"RNSVGTextPath"), // react-native-svg
+ #if !TARGET_OS_IOS && !TARGET_OS_TV && !TARGET_OS_VISION
		@"MacOSClass": NSClassFromString(@"MacOSClass"), // test-library-macos
+ #endif
+ #if !TARGET_OS_IOS && !TARGET_OS_OSX && !TARGET_OS_VISION
		@"TvOSClass": NSClassFromString(@"TvOSClass"), // test-library-tvos
+ #endif
#if !TARGET_OS_IOS && !TARGET_OS_OSX && !TARGET_OS_TV
		@"VisionOSClass": NSClassFromString(@"VisionOSClass"), // test-library-visionos
+ #endif
+ #if !TARGET_OS_TV
		@"RNCWebView": NSClassFromString(@"RNCWebView"), // react-native-webview
+ #endif
    };
  });

  return thirdPartyComponents;
}

This issue was initially reported in react-native-tvos#889, where a developer's iOS app crashed after including a tvOS-only package. CocoaPods correctly excluded the code, but Codegen still attempted to register the component at runtime.

Changelog:

[iOS] [Fixed] - Reintroduce platform guard macros in RCTThirdPartyComponentsProvider to prevent crashes when including platform-specific packages in OOT apps.

Test Plan:

MyApp contains several React Native packages that do not support iOS. When running on an iOS device, the app crashes.

Reproduce the steps for crash:

gh repo clone cgoldsby/RN-889
yarn
yarn ios -i

❌ MyApp launches and crashes immediately.

Apply patch to fix:

git apply patches/*
yarn ios -i

✅ MyApp launches and does not crash.

Related Issues:

#42047 (Macro introduced)

#47518 (Regression)

react-native-tvos/react-native-tvos#889 (Original crash report)

https://github.com/cgoldsby/RNTV-889 (RVTV example)

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Apr 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants