facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
118.79k stars 24.29k forks source link

[iOS] Codegen: Generates broken code for event emitter with nested objects in arrays #47113

Open hannojg opened 3 days ago

hannojg commented 3 days ago

Description

When having a fabric view component spec and using a direct event emitter which returns an object, which has an array field, which is of a custom object type the generates C++ code for the EventEmitter is broken.

This is the spec for example:

export interface NativeProps extends ViewProps {
  onCodeScanned?: DirectEventHandler<
    Readonly<{
      codes: Readonly<
        {
          type: string
          frame?: Readonly<{ x: Double; y: Double; width: Double; height: Double }>
          corners?: Readonly<{ x: Double; y: Double }[]>
        }[]
      >
      frame: Readonly<{ width: Int32; height: Int32 }[]>
    }>
  >
}

export default codegenNativeComponent<NativeProps>('CameraView')

And this is code generated by Codegen:

Screenshot 2024-10-18 at 08 55 04

To me it looks like it inserted a , where it should have used a ..

Steps to reproduce

  1. Clone the reproducer repo: https://github.com/hannojg/reproducer-react-native
  2. cd ReproducerApp
  3. yarn
  4. npx react-native codegen
  5. open ios/build/generates/ios/react/RNCameraSpec/EventEmitters.cpp, or try to build iOS

React Native Version

0.75.4

Affected Platforms

Build - MacOS, Build - Windows, Build - Linux

Output of npx react-native info

System:
  OS: macOS 15.0.1
  CPU: (12) arm64 Apple M2 Pro
  Memory: 85.53 MB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 20.17.0
    path: ~/.nvm/versions/node/v20.17.0/bin/node
  Yarn:
    version: 1.22.22
    path: ~/.nvm/versions/node/v20.17.0/bin/yarn
  npm:
    version: 10.8.2
    path: ~/.nvm/versions/node/v20.17.0/bin/npm
  Watchman: Not Found
Managers:
  CocoaPods:
    version: 1.15.2
    path: /Users/hannomargelo/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 24.0
      - iOS 18.0
      - macOS 15.0
      - tvOS 18.0
      - visionOS 2.0
      - watchOS 11.0
  Android SDK:
    API Levels:
      - "28"
      - "30"
      - "31"
      - "32"
      - "33"
      - "33"
      - "34"
    Build Tools:
      - 28.0.3
      - 30.0.2
      - 30.0.3
      - 31.0.0
      - 33.0.0
      - 33.0.1
      - 33.0.2
      - 34.0.0
      - 35.0.0
    System Images:
      - android-32 | Google APIs ARM 64 v8a
      - android-33 | Wear OS 4 ARM 64 v8a
      - android-33 | Google APIs ARM 64 v8a
      - android-33 | Google Play ARM 64 v8a
      - android-33 | Google APIs ATD ARM 64 v8a
      - android-34 | Google APIs ARM 64 v8a
      - android-34 | Google APIs ATD ARM 64
    Android NDK: 26.1.10909125
IDEs:
  Android Studio: 2024.2 AI-242.23339.11.2421.12483815
  Xcode:
    version: 16.0/16A242d
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.7
    path: /Users/hannomargelo/.jenv/shims/javac
  Ruby:
    version: 2.7.6
    path: /Users/hannomargelo/.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: true
  newArchEnabled: true
iOS:
  hermesEnabled: true
  newArchEnabled: true

Stacktrace or Logs

There are no relevant logs, the codegen command executes fine.

Reproducer

https://github.com/hannojg/reproducer-react-native

Screenshots and Videos

No response

hannojg commented 2 days ago

Update: I was able to circumvent this by removing the ReadOnly

Update: not sure what it was even w/o readonly its broken 😅

cortinico commented 2 days ago

Just tested on Android and this does not reproduce (most likely because we don't use the event emitter generated by codegen in Android)