jhugman / uniffi-bindgen-react-native

A uniffi bindings generator for calling Rust from react-native
https://jhugman.github.io/uniffi-bindgen-react-native/
Other
52 stars 6 forks source link

Generated code is being prefixed (e.g. with "ReactNative") but some occurrences appear to be missing #109

Closed Johennes closed 1 month ago

Johennes commented 1 month ago

I have the following in package.json:

"codegenConfig": {
  "name": "RNMatrixSdkSpec",
  "type": "modules",
  "jsSrcsDir": "src"
}

When generating the turbo module with 46f81224e4aae9a05dc7c8e209b4750c014f4d02 (main branch from an hour ago), the iOS module comes out as

// Generated by uniffi-bindgen-react-native
#ifdef __cplusplus
#import "react-native-matrix-sdk.h"
#endif

#ifdef RCT_NEW_ARCH_ENABLED
#import "RNMatrixSdkSpec.h"

@interface ReactNativeMatrixSdk : NSObject <RNMatrixSdkSpec>
#else
#import <React/RCTBridgeModule.h>

@interface ReactNativeMatrixSdk : NSObject <RCTBridgeModule>
#endif

@end

Notice how it imports RNMatrixSdkSpec.h and tries to implement the eponymous protocol <RNMatrixSdkSpec>. However, the generated file in example/ios/build/generated/ios/RNMatrixSdkSpec/RNMatrixSdkSpec.h defines the protocol as @protocol NativeReactNativeMatrixSdkSpec. As a result, the build on iOS fails.

When I change the name in package.json to NativeReactNativeMatrixSdkSpec

"codegenConfig": {
  "name": "NativeReactNativeMatrixSdkSpec",
  "type": "modules",
  "jsSrcsDir": "src"
}

both the generated file and the protocol come out as NativeReactNativeMatrixSdkSpec and the build succeeds.

Johennes commented 1 month ago

Possibly related: The Android build fails, too, after switching to 46f81224e4aae9a05dc7c8e209b4750c014f4d02. The problem there is that my android/src/main/AndroidManifest.xml used com.matrixsdk as the package name (which is what create-react-native-library had generated for my react-native-matrix-sdk repository).

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.matrixsdk">
</manifest>

The UBRN-generated code now uses com.reactnativematrixsdk, however.

Changing the package name in AndroidManifest.xml fixes things for me. Maybe UBRN should generate this file, too?

jhugman commented 1 month ago

Thanks for your analysis!

We have had some difficulty working out what the rules are between Codegen and create-react-native-library, with #44 being the first attempt.

Unfortunately, I have found this part of the code is very difficult to test!

My latest attempt at has just landed in #72 .

Changing the package name in AndroidManifest.xml fixes things for me. Maybe UBRN should generate this file, too?

Yes, I think this should be regenerated by UBRN.

FWIW: All the templates are in the codegen templates directory. The configuration for those templates, and most of the defaulting rules in the config/mod.rs file.

Johennes commented 1 month ago

Unfortunately, I have found this part of the code is very difficult to test!

I also find the name mangling in React Native pretty confusing tbh. As for testing, maybe as an integration test it'd be possible to script the creation of a turbo module using create-react-native-library and UBRN using a dummy Rust crate and then simply run the build? That wouldn't catch runtime errors of course.