react-native
react-native copied to clipboard
iOS Native Module Crash: Optional Methods in Spec Not Properly Generated
Description
The TypeScript spec file defines optional methods (marked with ?), but the generated iOS implementation files don't respect these optional markers, causing potential crashes on iOS. In Android, it is not a problem since the generated Android abstract class has a default implementation.
Steps to reproduce
Define a spec file NativeExampleSpec.ts
// Spec definition (simplified)
export interface Spec extends TurboModule {
// Optional method marked with ?
someOptionalMethod?: () => Promise<Object>
}
Here is the generated code in header file
// Generated iOS header (simplified)
@protocol NativeExampleSpec <RCTBridgeModule, RCTTurboModule>
// Method is not marked as optional in Objective-C
- (void)someOptionalMethod:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject;
@end
Expected Behavior
-
Methods marked as optional in TypeScript spec should generate corresponding optional methods in Objective-C -
The generated iOS code should handle optional methods appropriately to prevent crashes
Current Behavior
-
Optional methods in TypeScript spec are generated as required methods in iOS -
This mismatch can cause runtime crashes when optional methods are not implemented
React Native Version
0.79.2
Affected Platforms
Runtime - iOS
Areas
Codegen
Output of npx @react-native-community/cli info
System:
OS: macOS 15.4.1
CPU: (12) arm64 Apple M2 Pro
Memory: 77.44 MB / 32.00 GB
Shell:
version: 4.0.0
path: /usr/local/bin/fish
Binaries:
Node:
version: 20.16.0
path: ~/.nvm/versions/node/v20.16.0/bin/node
Yarn:
version: 1.22.22
path: ~/.nvm/versions/node/v20.16.0/bin/yarn
npm:
version: 8.19.4
path: ~/.nvm/versions/node/v20.16.0/bin/npm
Watchman:
version: 2024.12.02.00
path: /opt/homebrew/bin/watchman
Managers:
CocoaPods:
version: 1.15.2
path: /Users/dantrong/.rbenv/shims/pod
SDKs:
iOS SDK:
Platforms:
- DriverKit 24.2
- iOS 18.2
- macOS 15.2
- tvOS 18.2
- visionOS 2.2
- watchOS 11.2
Android SDK:
API Levels:
- "31"
- "33"
- "34"
- "35"
Build Tools:
- 30.0.3
- 31.0.0
- 33.0.0
- 33.0.1
- 34.0.0
- 35.0.0
System Images:
- android-28 | Google APIs ARM 64 v8a
- android-29 | Google APIs Intel x86 Atom
- android-31 | Android TV ARM 64 v8a
- android-31 | Android TV Intel x86 Atom
- android-31 | Google APIs ARM 64 v8a
- android-34 | Google APIs ARM 64 v8a
- android-34 | Google Play ARM 64 v8a
- android-35 | Google Play ARM 64 v8a
Android NDK: 22.1.7171670
IDEs:
Android Studio: 2024.3 AI-243.25659.59.2432.13423653
Xcode:
version: 16.2/16C5032a
path: /usr/bin/xcodebuild
Languages:
Java:
version: 17.0.13
path: /usr/bin/javac
Ruby:
version: 2.7.6
path: /Users/dantrong/.rbenv/shims/ruby
npmPackages:
"@react-native-community/cli": Not Found
react: Not Found
react-native: Not Found
react-native-macos: Not Found
npmGlobalPackages:
"*react-native*": Not Found
Android:
hermesEnabled: Not found
newArchEnabled: Not found
iOS:
hermesEnabled: Not found
newArchEnabled: Not found
Stacktrace or Logs
Potential crash since method is not implemented in IOS
MANDATORY Reproducer
https://github.com/dantrongamz/codegen-example
Screenshots and Videos
No response
[!WARNING] Unsupported version: It looks like your issue or the example you provided uses an unsupported version of React Native.
Due to the number of issues we receive, we're currently only accepting new issues against one of the supported versions. Please upgrade to latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If you cannot upgrade, please open your issue on StackOverflow to get further community support.
[!WARNING] Unsupported version: It looks like your issue or the example you provided uses an unsupported version of React Native.
Due to the number of issues we receive, we're currently only accepting new issues against one of the supported versions. Please upgrade to latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If you cannot upgrade, please open your issue on StackOverflow to get further community support.
Update reproducible repo with ReactNative version 0.79.2, I still see the same issue. Please remove the label "Type: Unsupported version"
Added a fix in https://github.com/facebook/react-native/pull/51632
Official fix: https://github.com/facebook/react-native/pull/51636 (previous PR is closed)