react-native: Redefinition of 'Native....SpecJSI' . Error when building with a custom turbo module

Description

Redefinition of ‘Native…SpecJSI’ . Error when building with a custom turbo module. I strictly followed the instructions for create turbo module https://reactnative.dev/docs/the-new-architecture/pillars-turbomodules

image

Steps to reproduce

React Native Version

0.73.2

Affected Platforms

Runtime - iOS

Areas

TurboModule - The New Native Module System

Output of npx react-native info

System:
  OS: macOS 14.2.1
  CPU: (12) arm64 Apple M3 Pro
  Memory: 99.55 MB / 18.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 18.19.0
    path: ~/.nvm/versions/node/v18.19.0/bin/node
  Yarn:
    version: 1.22.21
    path: /opt/homebrew/bin/yarn
  npm:
    version: 10.2.3
    path: ~/.nvm/versions/node/v18.19.0/bin/npm
  Watchman:
    version: 2023.12.04.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.14.3
    path: /Users/skinnynpale/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.2
      - iOS 17.2
      - macOS 14.2
      - tvOS 17.2
      - visionOS 1.0
      - watchOS 10.2
  Android SDK: Not Found
IDEs:
  Android Studio: 2023.1 AI-231.9392.1.2311.11076708
  Xcode:
    version: 15.2/15C500b
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.9
    path: /usr/bin/javac
  Ruby:
    version: 3.0.6
    path: /Users/skinnynpale/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.73.2
    wanted: 0.73.2
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: true
  newArchEnabled: true

Stacktrace or Logs

/Users/skinnynpale/Library/Developer/Xcode/DerivedData/a4ord-cnymkqjxmmqjnmfdnnckwsbdoigs/Build/Products/Debug-iphonesimulator/React-Codegen/React_Codegen.framework/Headers/RTNCustomAppBrowserSpec/RTNCustomAppBrowserSpec.h:34:22 Redefinition of 'NativeCustomAppBrowserSpecJSI'

About this issue

  • Original URL
  • State: open
  • Created 5 months ago
  • Reactions: 1
  • Comments: 21 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Yes, #pragma once doesn’t seem to work on iOS when using codegen.

I do agree to everything @eflashcards mentioned:

  • I fixed the docs, here, to make sure that there is only one import of those files.
  • I’m working on a Codegen change to add the #include guards here.

Hopefully, we will be able to fix this for good soon and systematically…

I managed to repro it locally! 😄

One think I noticed is that both RNTCalculator.h and RNTCalculator.mm both import the same file.

  • RNTCalculator.h
+#import <RNTCalculatorSpec/RNTCalculatorSpec.h>

NS_ASSUME_NONNULL_BEGIN

@interface RNTCalculator : NSObject <NativeCalculatorSpec>
  • RNTCalculator.mm
+#import "RNTCalculatorSpec.h"
#import "RNTCalculator.h"

@implementation RNTCalculator
// ...

So, what happens is that, when Xcode builds the RNTCalculator.mm, it takes the RNTCalculatorSpec once because it has been explicitly imported by line 1, and then it takes it another time because the #import "RNTCalculator.h" import the RNTCalculatorSpec transitively.

The first line can be removed:

-#import "RNTCalculatorSpec.h"
#import "RNTCalculator.h"

@implementation RNTCalculator
// ...

And everything will work fine ==> The error goes away.

That said, this is a bit brittle. I’d like to see if we can find a systematic approach to solve this.

cc. @eflashcards @dylancom @dumbravaandrei22 @skinnynpale @javache

We’ll probably end up applying the @eflashcards suggestions in codegen directly. For some reasons, #pragma once does not work in this case.

@eflashcards thanks for the reproducer steps and the heads up with the pragmas. It might be that we forgot to add #pragma once in the codegen? 🤔

I’m sorry, but I need a reproducer from you, guys. I build new empty apps with React Nativ and the New Architecture every day as that is my main focus, but I can’t reproduce this issue in any way. 😦

@eflashcards which commands are you using to create an empty project? @dumbravaandrei22 are you sure you followed the tutorial correctly? For example, here skinnynpale deleted a line inadvertently and it make everything fail… @dylancom can you provide a reproducer?

thanks, I’ll have a look at this tomorrow morning!