facebook / react-native

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

Creating a module with New Architecture and including a .swift file introduces compiler errors #46212

Open gtokman opened 2 weeks ago

gtokman commented 2 weeks ago

Description

I'm upgrading one of our open-source libraries, which uses Swift for the logic. However, when I include swift in the podspec source_files, I get compiler errors.

Steps to reproduce

I'm running into compiler errors after updating the podspec for the module to include .swift files in the ios/ directory.

 s.source_files = "ios/**/*.{h,m,mm,cpp,swift}" <--- adding Swift

After building the development pod, I get this error:

Screenshot 2024-08-26 at 10 11 15 AM

I found a workaround by omitting the following: 1.

 s.source_files = "ios/**/*.{h,m,mm,cpp}" <--- remove swift
  1. run bundle exec pod install

  2. Add the .swift file manually

    Screenshot 2024-08-26 at 10 18 12 AM

After building the project, there is no compiler error. This is fine for testing, but I need to update the podspec to include the file in the Compile Sources, otherwise, other errors will occur.

React Native Version

0.74.0

Affected Platforms

Runtime - iOS

Areas

TurboModule - The New Native Module System

Output of npx react-native info

System:
  OS: macOS 14.4.1
  CPU: (8) arm64 Apple M1
  Memory: 175.06 MB / 16.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 18.18.0
    path: ~/.nvm/versions/node/v18.18.0/bin/node
  Yarn:
    version: 3.6.4
    path: /opt/homebrew/bin/yarn
  npm:
    version: 9.8.1
    path: ~/.nvm/versions/node/v18.18.0/bin/npm
  Watchman:
    version: 2024.04.15.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.15.2
    path: /Users/gary/.rvm/gems/ruby-2.7.5/bin/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:
    API Levels:
      - "29"
      - "30"
      - "31"
      - "32"
      - "33"
      - "34"
    Build Tools:
      - 28.0.3
      - 30.0.2
      - 30.0.3
      - 31.0.0
      - 32.0.0
      - 32.1.0
      - 33.0.0
      - 33.0.1
      - 34.0.0
    System Images:
      - android-32 | Google APIs ARM 64 v8a
      - android-32 | Google Play ARM 64 v8a
      - android-33 | Google APIs ARM 64 v8a
      - android-33 | Google Play ARM 64 v8a
      - android-Tiramisu | Google APIs ARM 64 v8a
    Android NDK: 22.1.7171670
IDEs:
  Android Studio: 2023.2 AI-232.10300.40.2321.11668458
  Xcode:
    version: 15.4/15F31d
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.12
    path: /usr/bin/javac
  Ruby:
    version: 2.7.5
    path: /Users/gary/.rvm/rubies/ruby-2.7.5/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.3.1
    wanted: 18.3.1
  react-native:
    installed: 0.75.2
    wanted: 0.75.2
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: true
  newArchEnabled: true

Stacktrace or Logs

n/a

Reproducer

https://github.com/candlefinance/oss/tree/feat-convert-send-na

Screenshots and Videos

No response

react-native-bot commented 2 weeks ago
:warning: Newer Version of React Native is Available!
:information_source: You are on a supported minor version, but it looks like there's a newer patch available - 0.74.5. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases.
react-native-bot commented 2 weeks ago
:warning: Missing Reproducible Example
:information_source: 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.
react-native-bot commented 2 weeks ago
:warning: Newer Version of React Native is Available!
:information_source: You are on a supported minor version, but it looks like there's a newer patch available - undefined. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases.
react-native-bot commented 2 weeks ago
:warning: Missing Reproducible Example
:information_source: We could not detect a reproducible example in your issue report. Please provide either:
Rexogamer commented 2 weeks ago

(do not download that file - the account seems to have been compromised (see pyg-team/pytorch-frame#441))

nullishamy commented 2 weeks ago

@gtokman Seen you add labels, do you have permission to delete the comment above? Almost certainly malware

Rexogamer commented 2 weeks ago

afaik those labels are added by GitHub whenever someone opens an issue - while the action is attributed to the author, this doesn't mean they have any further permissions

cipolleschi commented 2 weeks ago

@gtokman thanks for the issue... A few points:

  1. Swift is not officially supported by the New Architecture. I know it's something big that is missing, but we don't have the resources to work on that at the moment. I hope that this will come soon.
  2. Swift and C++ does not really work well together. Yes, we have the Swift/Cxx interoperability but it is not complete sadly and I don't think it is stable enough to be used
  3. It looks like that your pod is failing before some swift file is trying to import the <memory> C++ module. That could also happen for transitive dependencies. Swift can't import C++ modules, hence the failure.
gtokman commented 2 weeks ago

@cipolleschi, thanks for the context.

  1. It looks like that your pod is failing before some swift file is trying to import the <memory> C++ module. That could also happen for transitive dependencies. Swift can't import C++ modules, hence the failure.

I mentioned that I found a workaround. My thought was that a Build Setting was not being set correctly, which led to the pod failing to compile if I included Swift source_files. Do you have an idea of what would be different? I guess I can try to diff the .xcodeproj. of the generated vs. manually adding the file.

cipolleschi commented 2 weeks ago

when you add the swift file manually, Xcode usually creates some bridging headers and extra configuration files. We need to look into what is generated to see how Xcode resolve the issue and then "commit" those files into the library.

I can see if I can find some time to look into it, but it will likely happen in a couple of weeks as next week we have React Universe and I'll be in Poland for the Conference.

gtokman commented 2 weeks ago

when you add the swift file manually, Xcode usually creates some bridging headers and extra configuration files. We need to look into what is generated to see how Xcode resolve the issue and then "commit" those files into the library.

I can see if I can find some time to look into it, but it will likely happen in a couple of weeks as next week we have React Universe and I'll be in Poland for the Conference.

Appreciate it @cipolleschi!

I do see the alert in Xcode for the bridging header, but I don't create it. Bridging headers are for Obj-C -> Swift. To use Swift -> Obj-C you just need to import the generated Swift headers i.e Send.swift -> #import "candlefinance_send-Swift.h".