jhugman / uniffi-bindgen-react-native

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

Doesn't work anymore with create-react-native-library@0.41.2 & react-native@0.76.1 #161

Open Johennes opened 1 week ago

Johennes commented 1 week ago

The codegenConfig block now seems to include output dirs for the generated code.

  "codegenConfig": {
    "name": "RNReactNativeMatrixSdkSpec",
    "type": "all",
    "jsSrcsDir": "src",
    "outputDir": {
      "ios": "ios/generated",
      "android": "android/generated"
    },
    "android": {
      "javaPackageName": "com.unomed.reactnativematrixsdk"
    },
    "includesGeneratedCode": true
  },

UBRN doesn't appear to use these for Android and instead tries to include the generated code from android/build/generated/source/codegen/java in android/build.gradle.

          java.srcDirs += [
            // This is needed to build Kotlin project with NewArch enabled
            "${project.buildDir}/generated/source/codegen/java"
          ]

I tried changing the output dir for Android in package.json to comply with the value UBRN expects but this gave me another error. The only thing I found working so far is to manually patch build.gradle.

          java.srcDirs += [
            // This is needed to build Kotlin project with NewArch enabled
-            "${project.buildDir}/generated/source/codegen/java"
+            "${project.buildDir}/../generated/java"
          ]

UBRN should read the values from package.json and use them accordingly.

jhugman commented 1 week ago

@Johennes I'm not sure if you want to fix this yourself, or not. If you don't then, no worries, but it will take a week or two for me to get to it. If you do, great! This is what I'd suggest:

Reading the outputDir from package.json should be a case of adding a struct to npm.rs. I think that file has some #[serde(default ="…")]` functions.

You may need to add something to AndroidConfig and IosConfig so it's accessible from the templates. The javaPackageName property is a reasonable template for how all this ties together.

I'm not sure if you need to add something to build.gradle too but you'll likely want to add to build.kt.gradle. I haven't looked at the current build.gradle from CRNL, so it may be better just to add some lines, or to completely re-do it.

Looking up a relative path has a utility method on RenderedFile; here it is used in the templates:

  {%- let root = self.project_root() %}
  {%- let dir = self.config.project.ios.framework_path(root) %}
  {%- let framework = self.relative_to(root, dir) %}

I think you have a good handle on what's going on with the script/test-turbo-modules.sh.

What do you think?

Johennes commented 1 week ago

Thanks for the detailed steps. Let me try and take a stab at this.

I wanted to first set up the bob / rn test matrix we spoke about but was surprised when the issue didn't reproduce there. It turns out that build.kt.gradle doesn't have this issue as long as you don't change the default paths that crnl puts into package.json because of this:

https://github.com/jhugman/uniffi-bindgen-react-native/blob/33fb8c63c7165b49f9726dda42f36225c484f549/crates/ubrn_cli/src/codegen/templates/build.kt.gradle#L119-L121

When I first generated my project, however, crnl wasn't yet using Kotlin so this check always failed for me:

https://github.com/jhugman/uniffi-bindgen-react-native/blob/33fb8c63c7165b49f9726dda42f36225c484f549/crates/ubrn_cli/src/codegen/mod.rs#L177

That in turn meant that ubrn would always generate from the build.gradle template which expects the generated code in another folder:

https://github.com/jhugman/uniffi-bindgen-react-native/blob/33fb8c63c7165b49f9726dda42f36225c484f549/crates/ubrn_cli/src/codegen/templates/build.gradle#L107-L109

Johennes commented 1 week ago

Given that as of writing the lowest version of crnl that is not broken[^1] is 0.42.2 and given that this version uses Kotlin rather than Java, maybe we can ditch the build.gradle template and always use build.kt.gradle?

[^1]: 0.42.1 is missing @react-native-community/cli and all lower versions have the unknown option '--npm' issue.