Skip to content

iOS Native Module Crash: Optional Methods in Spec Not Properly Generated #51485

Description

@dantrongamz

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: AttentionIssues where the author has responded to feedback.Platform: iOSiOS applications.StaleThere has been a lack of activity on this issue and it may be closed soon.Tech: CodegenRelated to react-native-codegenType: New ArchitectureIssues and PRs related to new architecture (Fabric/Turbo Modules)Type: Unsupported VersionIssues reported to a version of React Native that is no longer supported

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions