Skip to content
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

codegen - 0.74: Int32 | null generates Nullable Integer on java side, but Ljava/lang/Double on c++ side. #44963

Closed
mfazekas opened this issue Jun 14, 2024 · 12 comments
Labels
Resolution: PR Submitted A pull request with a fix has been provided. Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules)

Comments

@mfazekas
Copy link
Contributor

Description

In RN 0.74 we started to see Error: Exception in HostFunction: no non-static method "Lcom/rnmapbox/rnmbx/components/mapview/NativeMapViewModule;.setHandledMapChangedEvents(Ljava/lang/Double;Lcom/facebook/react/bridge/ReadableArray;Lcom/facebook/react/bridge/Promise;)V

We implement the function as

   typealias ViewRefTag = Int

   ...

   override fun setHandledMapChangedEvents(
        viewRef: ViewRefTag?,
        events: ReadableArray,
        promise: Promise
    ) {

Codespec: Int32

Our maps/src/specs/NativeMapViewModule.ts has:

setHandledMapChangedEvents: ( viewRef: Int32 | null,  events: ReadonlyArray<string>) => Promise<Object>; 

specs-generated.cpp: java.lang.Double

Codegen generated cpp: android/build/generated/source/codegen/jni/rnmapbox_maps_specs-generated.cpp has Ljava/lang/Double

static facebook::jsi::Value __hostFunction_NativeMapViewModuleSpecJSI_setHandledMapChangedEvents(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
  static jmethodID cachedMethodId = nullptr;
  return static_cast<JavaTurboModule &>(turboModule).invokeJavaMethod(rt, PromiseKind, "setHandledMapChangedEvents", "(Ljava/lang/Double;Lcom/facebook/react/bridge/ReadableArray;Lcom/facebook/react/bridge/Promise;)V", args, count, cachedMethodId);
}

ModuleSpec.java: java.lang.Integer

Codegen generate-specs-cli.js generated maps/android/src/main/old-arch/com/rnmapbox/rnmbx/NativeMapViewModuleSpec.java has as:

public abstract void setHandledMapChangedEvents(@Nullable Integer viewRef, ReadableArray events, Promise promise); 

See rnmapbox/maps#3527

Steps to reproduce

1.) check out the main branch of the rnmapbox/maps repository
2.) install modules

yarn generate
cd example
yarn install

3.) change newArchEnabled to newArchEnabled=true in example/android/gradle.properties
4.) Build the project

```
cd example/android
./gradlew assembleDebug
```

Check the files mentioned in description

React Native Version

0.74.2

Affected Platforms

Runtime - Android

Areas

TurboModule - The New Native Module System, Codegen

Output of npx react-native info

info Fetching system and libraries information...
System:
  OS: macOS 14.5
  CPU: (12) arm64 Apple M2 Max
  Memory: 83.89 MB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 18.20.2
    path: ~/.nvm/versions/node/v18.20.2/bin/node
  Yarn:
    version: 1.22.22
    path: ~/.nvm/versions/node/v18.20.2/bin/yarn
  npm:
    version: 10.5.0
    path: ~/.nvm/versions/node/v18.20.2/bin/npm
  Watchman:
    version: 2024.05.06.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.14.2
    path: /Users/boga/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.5
      - iOS 17.5
      - macOS 14.5
      - tvOS 17.5
      - visionOS 1.2
      - watchOS 10.5
  Android SDK: Not Found
IDEs:
  Android Studio: 2024.1 AI-241.15989.150.2411.11948838
  Xcode:
    version: 15.4/15F31d
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 20.0.1
    path: /usr/bin/javac
  Ruby:
    version: 2.7.8
    path: /Users/boga/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.74.2
    wanted: 0.74.2
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: true
  newArchEnabled: true

Stacktrace or Logs

Error: Exception in HostFunction: no non-static method "Lcom/rnmapbox/rnmbx/components/mapview/NativeMapViewModule;.setHandledMapChangedEvents(Ljava/lang/Double;Lcom/facebook/react/bridge/ReadableArray;Lcom/facebook/react/bridge/Promise;)V"
                                                                                                    
This error is located at:
    in MapView (created by SetTintColor)
    in RCTView (created by View)
    in View (created by SetTintColor)
    in SetTintColor
    in RCTView (created by View)
    in View (created by Page)
    in Page
    in Unknown (created by Item)
    in Item (created by SceneView)
    in StaticContainer
    in EnsureSingleNavigator (created by SceneView)
    in SceneView (created by SceneView)
    in RCTView (created by View)
    in View (created by DebugContainer)
    in DebugContainer (created by MaybeNestedStack)
    in MaybeNestedStack (created by SceneView)
    in RCTView (created by View)
    in View (created by SceneView)
    in RNSScreen (created by Animated(Anonymous))
    in Animated(Anonymous) (created by InnerScreen)
    in Suspender (created by Freeze)
    in Suspense (created by Freeze)
    in Freeze (created by DelayedFreeze)
    in DelayedFreeze (created by InnerScreen)
    in InnerScreen (created by Screen)
    in Screen (created by SceneView)
    in SceneView (created by NativeStackViewInner)
    in Suspender (created by Freeze)
    in Suspense (created by Freeze)
    in Freeze (created by DelayedFreeze)
    in DelayedFreeze (created by ScreenStack)
    in RNSScreenStack (created by ScreenStack)
    in ScreenStack (created by NativeStackViewInner)
    in NativeStackViewInner (created by NativeStackView)
    in RNCSafeAreaProvider (created by SafeAreaProvider)
    in SafeAreaProvider (created by SafeAreaProviderCompat)
    in SafeAreaProviderCompat (created by NativeStackView)
    in NativeStackView (created by NativeStackNavigator)
    in PreventRemoveProvider (created by NavigationContent)
    in NavigationContent
    in Unknown (created by NativeStackNavigator)
    in NativeStackNavigator (created by AppStackNavigator)
    in AppStackNavigator (created by AppContainer)
    in EnsureSingleNavigator
    in BaseNavigationContainer
    in ThemeProvider
    in NavigationContainerInner (created by AppContainer)
    in AppContainer (created by App)
    in App
    in RCTView (created by View)
    in View (created by AppContainer)
    in RCTView (created by View)
    in View (created by AppContainer)
    in AppContainer
    in RNMapboxGLExample(RootComponent), js engine: hermes

Reproducer

https://github.com/rnmapbox/maps

Screenshots and Videos

No response

@mfazekas mfazekas added Needs: Triage 🔍 Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules) labels Jun 14, 2024
@github-actions github-actions bot added Needs: Author Feedback Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. labels Jun 14, 2024
Copy link

⚠️ Missing Reproducible Example
ℹ️ We could not detect a reproducible example in your issue report. Please provide either:
  • If your bug is UI related: a Snack
  • If your bug is build/update related: use our Reproducer Template. A reproducer needs to be in a GitHub repository under your username.

@cortinico
Copy link
Contributor

Is this a regression from 0.73 to 0.74 @mfazekas ?

@mfazekas
Copy link
Contributor Author

mfazekas commented Jun 17, 2024

@cortinico yes I believe so. At least the error - Error: Exception in HostFunction: no non-static method "Lcom/rnmapbox/rnmbx/components/mapview/NativeMapViewModule;.setHandledMapChangedEvents(Ljava/lang/Double;Lcom/facebook/react/bridge/ReadableArray;Lcom/facebook/react/bridge/Promise;)V we've not encountered with 0.73.

But looking at the sources this part was not touched in 4 years.
https://github.com/facebook/react-native/blame/9922628032ace4d87257c1b3d70c2dc6c38a60a6/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js#L262-L263

Also in 0.73.0 I also see double

static facebook::jsi::Value __hostFunction_NativeMapViewModuleSpecJSI_setHandledMapChangedEvents(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
  static jmethodID cachedMethodId = nullptr;
  return static_cast<JavaTurboModule &>(turboModule).invokeJavaMethod(rt, PromiseKind, "setHandledMapChangedEvents", "(Ljava/lang/Double;Lcom/facebook/react/bridge/ReadableArray;Lcom/facebook/react/bridge/Promise;)V", args, count, cachedMethodId);
}

So not sure why we started to face the issue now.

@github-actions github-actions bot added Needs: Attention Issues where the author has responded to feedback. and removed Needs: Author Feedback labels Jun 17, 2024
@cortinico
Copy link
Contributor

@mfazekas Could we get a small repro using https://github.com/react-native-community/reproducer-react-native ? That would make it way easier for me to understand what happened.

@mfazekas
Copy link
Contributor Author

@cortinico sure will try to do that

@mfazekas
Copy link
Contributor Author

@cortinico so the issue is that in RN 0.73
GenerateModuleJavaSpec.js has generated

public abstract void setHandledMapChangedEvents(@Nullable Double viewRef, ReadableArray events, Promise promise); 

while RN 0.74 it's changed to:

- public abstract void setHandledMapChangedEvents(@Nullable Double viewRef, ReadableArray events, Promise promise); 
+ public abstract void setHandledMapChangedEvents(@Nullable Integer viewRef, ReadableArray events, Promise promise);

see
ccd3b04
#42126

But rest of RN 0.74 has not made this change.

case 'Int32TypeAnnotation':
return !isRequired ? 'Ljava/lang/Double;' : 'D';

@mfazekas
Copy link
Contributor Author

As for if it's a breaking change? I'm not 100% sure many modules like react-native-svg are generating it once, and haven't rerun with the changed codegen in RN 0.74. We've reran with @rnmapbox/maps that's why we've faced the issue.

https://github.com/software-mansion/react-native-svg/blob/main/android/src/paper/java/com/horcrux/svg/NativeSvgRenderableModuleSpec.java

cortinico added a commit to cortinico/react-native that referenced this issue Jun 18, 2024
Summary:
Users reported that Int32 types in codegen are causing a crash in 0.74.
Fixes facebook#44963

This is because of D52420921 which partially implemented type change for Int32/Float.
Here I'm fixing it by adding propery return types and bytecode signatures for Int32/Float.

Changelog:
[Android] [Fixed] - Fix codegen support for Int32/Float

Reviewed By: dmytrorykun

Differential Revision: D58725243
@cortinico
Copy link
Contributor

This is indeed a bug. Fix for it is here:

@mfazekas if you could try it with patch-package and report back, would be gold. We could include it in the next 0.74 patch release.

cortinico added a commit to cortinico/react-native that referenced this issue Jun 18, 2024
Summary:
Pull Request resolved: facebook#45024

Users reported that Int32 types in codegen are causing a crash in 0.74.
Fixes facebook#44963

This is because of D52420921 which partially implemented type change for Int32/Float.
Here I'm fixing it by adding propery return types and bytecode signatures for Int32/Float.

Changelog:
[Android] [Fixed] - Fix codegen support for Int32/Float

Reviewed By: dmytrorykun

Differential Revision: D58725243
cortinico added a commit to cortinico/react-native that referenced this issue Jun 18, 2024
Summary:
Pull Request resolved: facebook#45024

Users reported that Int32 types in codegen are causing a crash in 0.74.
Fixes facebook#44963

This is because of D52420921 which partially implemented type change for Int32/Float.
Here I'm fixing it by adding propery return types and bytecode signatures for Int32/Float.

Changelog:
[Android] [Fixed] - Fix codegen support for Int32/Float

Differential Revision: D58725243
@mfazekas
Copy link
Contributor Author

@cortinico
I've tested it with patch-package, and it worked fine.
mfazekas/maps-1@dbcf2b1

Note that this fix, while looks great and is correct. Can actually cause issues in some case - like, the mentioned react-native-svg has the spec generated with 0.73 or even older, so it might work because of the current but, but with next 0.74.* release they'll need to update those generated java code: https://github.com/software-mansion/react-native-svg/blob/main/android/src/paper/java/com/horcrux/svg/NativeSvgRenderableModuleSpec.java

@cortinico
Copy link
Contributor

I've tested it with patch-package, and it worked fine.

Great thanks for the confirmation 👍

Note that this fix, while looks great and is correct. Can actually cause issues in some case

We're aware of this. That's why this change was marked as breaking + started from last version we added support for includesGeneratedCode:

"includesGeneratedCode": true,

That will allow libraries to ship the generated code so that mismatch between codegen versions like this one won't happen again

@cortinico cortinico added Resolution: PR Submitted A pull request with a fix has been provided. and removed Needs: Triage 🔍 Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. Needs: Attention Issues where the author has responded to feedback. labels Jun 18, 2024
@cortinico
Copy link
Contributor

Ok so for React Native SVG the fix would be here:

@mfazekas have you noticed any similar failures?

cortinico added a commit to cortinico/react-native that referenced this issue Jun 20, 2024
…ok#45024)

Summary:
Original commit changeset: 32b3bbdf5fd2

Fixes facebook#44963
Closes facebook#45024

Original Phabricator Diff: D52420921

Reviewed By: dmytrorykun

Differential Revision: D58820544
@cortinico
Copy link
Contributor

We ended up reverting the partial Int32/Float implementation, as attempting to fix it with the previous PR I linked would create more problems with libraries like -svg and others.

We'll get back to it in a future release.

cortinico added a commit that referenced this issue Jun 24, 2024
Summary:
Pull Request resolved: #45087

Original commit changeset: 32b3bbdf5fd2

Fixes #44963
Closes #45024

Original Phabricator Diff: D52420921

Changelog:
[Internal] [Changed] - Back out "[RN][Codegen]Add Float and Int type support for Android  modules"

Reviewed By: dmytrorykun

Differential Revision: D58820544

fbshipit-source-id: 59cd0e7cc17a681785c57b5ce1a9d50d28a348af
Titozzz pushed a commit that referenced this issue Jul 1, 2024
Summary:
Pull Request resolved: #45087

Original commit changeset: 32b3bbdf5fd2

Fixes #44963
Closes #45024

Original Phabricator Diff: D52420921

Changelog:
[Internal] [Changed] - Back out "[RN][Codegen]Add Float and Int type support for Android  modules"

Reviewed By: dmytrorykun

Differential Revision: D58820544

fbshipit-source-id: 59cd0e7cc17a681785c57b5ce1a9d50d28a348af
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: PR Submitted A pull request with a fix has been provided. Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules)
Projects
None yet
2 participants