facebook / react-native

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

Apple Silicon (M1) troubleshooting guide (RN 0.64/0.65/0.66) #31941

Closed kelset closed 1 year ago

kelset commented 3 years ago

Hey folks 👋

This issue wants to help everyone working from an M1 powered machine successfully be able to build their RN apps.

The points below have been written by @mikehardy (and only lightly edited for readability), so huge props to him for his work on this problem space 👏👏

All testing is done with this script and variations on it https://github.com/mikehardy/rnfbdemo/blob/master/make-demo.sh - so that testing is reproducible.


Main fix

For Apple Silicon to work at all you need to address a linker problem with react-native and Swift libraries.

You know you have this problem if you see something like

ld: warning: Could not find or use auto-linked library 'swiftCoreFoundation'

...as described in https://github.com/facebookarchive/react-native-fbsdk/issues/755

Many people recommend adding an empty Swift file with a bridging header, but it's not ideal: it is a manual thing that requires GUI manipulation in Xcode = hard to document, not automatic and it is only done for the side effect it has of changing up library paths.

This solution directly has the desired effect, added in your local Podfile's post_install section (aka "stanza"):

    installer.aggregate_targets.each do |aggregate_target|
      aggregate_target.user_project.native_targets.each do |target|
        target.build_configurations.each do |config|
          config.build_settings['LIBRARY_SEARCH_PATHS'] = ['$(SDKROOT)/usr/lib/swift', '$(inherited)']
        end
      end
      aggregate_target.user_project.save
    end

Hermes + iOS

For Apple Silicon to successfully use Hermes for iOS you should use react-native 0.65. It is maybe possible with react-native 0.64 but it requires the updated Hermes dependency used in react-native 0.65 (v0.8.1), and trying to qualify that on react-native 0.64 is not something generally supportable. Hermes + iOS + Apple Silicon? Please update to react-native 0.65.

3rd party packages PSA

Many React Native third party libraries, like the ones @mikehardy is involved in react-native-google-signin and react-native-fbsdk-next are not ready for Apple Silicon yet. They both need forward ports to wrap the updated breaking-change versions of the underlying native SDKs, and that's work in need of your effort.

Check the issue section of those repositories that error out on M1s, there's probably already an ongoing effort to add support - help out if you can.

Anecdotally, Mike says that

I currently check in my podfile to edit out those dependencies for my Apple Silicon colleagues and prohibit release builds there. The arch command is useful in these cases, you may use it either in package.json scripts or your Podfile to detect if you are on x86_64 or arm64 and behave programmatically (removing dependencies perhaps) as needed

VSCode Terminal issues

tl;dr: do not run install / compile commands from a Terminal in VS Code unless your VS Code is up to date

If you program using VSCode and use Terminal inside VSCode for commands, like run pod install, you should be aware that as of this typing VSCode Terminal runs under Rosetta 2, and your pod install will do things you do not want based on mis-diagnosing your arch as x86_64 (via Rosetta 2), resulting in undefined behavior and pod install / compile errors. See https://github.com/microsoft/vscode/issues/116763 for more details.

[edited: vscode current versions work great on arm64, even if it's a remote-ssh connection]


If none of the above helped you, and you can reproduce your issue consistently - even on a freshly init'd project, then post a comment below with how to repro so that this can be investigated further.

mikehardy commented 3 years ago

@kelset, I cannot edit the issue, so I will post a comment here, then please / with my blessing take anything useful and chop it up / paste it whereever seems good


1- all testing is done with this script and variations on it https://github.com/mikehardy/rnfbdemo/blob/master/make-demo.sh - so that testing is reproducible

2- For Apple Silicon to work at all you need to work around a linker problem with react-native and Swift libraries.

You know you have this problem if you see something like

ld: warning: Could not find or use auto-linked library 'swiftCoreFoundation'

...as described in https://github.com/facebookarchive/react-native-fbsdk/issues/755

Many people recommend adding an empty Swift file with a bridging header, but I find this distasteful personally as:

The stanza looks like this inside post_install

    installer.aggregate_targets.each do |aggregate_target|
      aggregate_target.user_project.native_targets.each do |target|
        target.build_configurations.each do |config|
          config.build_settings['LIBRARY_SEARCH_PATHS'] = ['$(SDKROOT)/usr/lib/swift', '$(inherited)']
        end
      end
      aggregate_target.user_project.save
    end
  1. Hermes + iOS: For Apple Silicon to successfully use Hermes for iOS you should use react-native 0.65. It is maybe possible with react-native 0.64 but it requires the updated hermes dependency used in react-native 0.65, and trying to qualify that on react-native 0.64 is not something generally supportable. Hermes + iOS + Apple Silicon? Use react-native 0.65.

  2. 3rd party package notes: I am involved in other popular repositories like react-native-google-signin and react-native-fbsdk-next and those are not ready for Apple Silicon yet. They both need forward ports to wrap the updated breaking-change versions of the underlying native SDKs, and that's work in need of your effort. See those repositories to help out. I currently use check in my podfile to edit out those dependencies for my Apple Silicon colleagues and prohibit release builds there. The arch command is useful in these cases, you may use it either in package.json scripts or your Podfile to detect if you are on x86_64 or arm64 and behave programmatically (removing dependencies perhaps) as needed

  3. VSCode Terminal issues: If you program using VS Code and use Terminal inside VS Code to do things, like run pod install, you should be aware that as of this typing VS Code Terminal runs under Rosetta 2, and your pod install will do things you do not want based on mis-diagnosing your arch as x86_64 (via Rosetta 2), resulting in undefined behavior and pod install / compile errors. https://github.com/microsoft/vscode/issues/116763 - do not run install / compile commands from a Terminal in VS Code.

azeezat commented 3 years ago

This is great! My app now builds. I have tried several fixes. Thanks and great work @kelset

sadikyalcin commented 3 years ago

Yup, this solution works on M1's.

mikehardy commented 3 years ago

...and it is so much tidier than an empty swift file and bridging header isn't it? Discovering the one stack overflow comment on one of the posts about this that held the clue about putting the swift path in first and having that pass testing was a very happy moment, I had a really hard time solving this one

AntouanK commented 3 years ago

both on my work project, the react-native "AwesomeProject", and on that "make-demo.sh" project, I get the same errors. looks like this

Screenshot 2021-08-10 at 14 27 06

I can then go in /ios and do arch -x86_64 pod install and it installs the right (??) packages I guess. but running npx react-native run-ios shows a similar error

Screenshot 2021-08-10 at 14 31 06

any ideas on how to fix this?

mikehardy commented 3 years ago

Please post text, enclosed in triple-backticks if possible, of the actual errors. For instance the first image you posted is incredibly difficult to read on mobile, and does not contain the actual error. So I'm not sure what is going on there.

No arch commands should be necessary to use react-native 0.65.0-rc.3 on Apple Silicon.

Your second error about swift libraries not being found appears to related to the error that this exact post_install hook above is supposed to fix, so that is surprising. I have that workaround in use on both intel and apple silicon macs and do not see your error.

https://stackoverflow.com/help/minimal-reproducible-example is critical here, but my make-demo.sh script does not reproduce it for me, so I'm not sure what's going on in your environment.

AntouanK commented 3 years ago

@mikehardy you're right. I'll prepare a better representation of the problem shortly so you can have the full picture.

AntouanK commented 3 years ago

the steps I take

am I supposed to have that GoogleService-Info.plist file? or should I run pod install first?

I've never set up a react-native app on a macbook before, so no idea what's the right steps.

Thank you.

mikehardy commented 3 years ago

My script (make-demo.sh) was originally intended for a "How do I integrate react-native-firebase" demonstration so it requires you to have firebase project files (the two files it fails on - GoogleService-Info.plist, google-services.json) in order to work successfully.

Using it for a "how do I get react-native to work on Apple Silicon" requires a slightly different script that removes the react-native-firebase parts.

I've done that on a branch, you may have more success with this: https://github.com/mikehardy/rnfbdemo/blob/rn65demo/make-demo.sh - you should retry with that

As an aside, it is basically always safe to run pod install on apple machines, and it is strictly required after doing any sort of Podfile modification or installing anything that touches native code

AntouanK commented 3 years ago

@mikehardy ah ok, I see.

I got your script, and tried again. the output is here : https://pastebin.com/raw/80ReM6M4 Any idea why it fails?

mikehardy commented 3 years ago

Quite odd, it appears that everything should be working and yet you are apparently suffering from exactly the error that the Podfile post_install library path tweak is supposed to fix. I can't explain it, sorry

AntouanK commented 3 years ago

not very hopeful 😂 any way we can debug it? Any ideas at all?

mikehardy commented 3 years ago

trial and error with adding empty swift file + bridging header approach documented elsewhere, with other swift items in the library path, on other machines perhaps. All pretty vague but it's really just methodical testing of one approach after another until you find something that works, then continued trial and error examining why that solution worked, with the goal to reduce it to it's simplest possible application (and ideally to something that may be done in a post_install hook

which is what I thought I had accomplished :-), but clearly not.

losheredos commented 3 years ago

I couldnt build react-native stripe package because of these swift errors.

After adding 'post_install' part, it resolved! Now I can build finally :))

Thanks for your efforts and sharing it!

mikehardy commented 3 years ago

If anyone has time to test the post_install with the two entries reversed - that is inherited first, then the swift libraries, I would really really really like to hear a report on if it still works after pod install / rebuild.

Why? There is an upstream patch going into react-native that is implementing something similar, but they have the entries reversed. Does it work? We need to know!

Thanks in advance

Mr0cket commented 3 years ago

I am getting the following issue while running pod install. The error occurs while installing the glog (0.3.5) pod. As you can see, I am emulating the x86_64 architecture in my terminal:

➜  ios git:(develop) arch
i386

And have also tried pod install with and without prefixing arch -x86_64. This problem occurs on several different RN projects, with react native versions: 0.64.2 & 0.64.5.

...
Installing boost-for-react-native (1.63.0)
Installing glog (0.3.5)
[!] /bin/bash -c 
set -e
#!/bin/bash
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

set -e

PLATFORM_NAME="${PLATFORM_NAME:-iphoneos}"
CURRENT_ARCH="${CURRENT_ARCH}"

if [ -z "$CURRENT_ARCH" ] || [ "$CURRENT_ARCH" == "undefined_arch" ]; then
    # Xcode 10 beta sets CURRENT_ARCH to "undefined_arch", this leads to incorrect linker arg.
    # it's better to rely on platform name as fallback because architecture differs between simulator and device

    if [[ "$PLATFORM_NAME" == *"simulator"* ]]; then
        CURRENT_ARCH="x86_64"
    else
        CURRENT_ARCH="armv7"
    fi
fi

export CC="$(xcrun -find -sdk $PLATFORM_NAME cc) -arch $CURRENT_ARCH -isysroot $(xcrun -sdk $PLATFORM_NAME --show-sdk-path)"
export CXX="$CC"

# Remove automake symlink if it exists
if [ -h "test-driver" ]; then
    rm test-driver
fi

./configure --host arm-apple-darwin

cat << EOF >> src/config.h
/* Add in so we have Apple Target Conditionals */
#ifdef __APPLE__
#include <TargetConditionals.h>
#include <Availability.h>
#endif

/* Special configuration for ucontext */
#undef HAVE_UCONTEXT_H
#undef PC_FROM_UCONTEXT
#if defined(__x86_64__)
#define PC_FROM_UCONTEXT uc_mcontext->__ss.__rip
#elif defined(__i386__)
#define PC_FROM_UCONTEXT uc_mcontext->__ss.__eip
#endif
EOF

# Prepare exported header include
EXPORTED_INCLUDE_DIR="exported/glog"
mkdir -p exported/glog
cp -f src/glog/log_severity.h "$EXPORTED_INCLUDE_DIR/"
cp -f src/glog/logging.h "$EXPORTED_INCLUDE_DIR/"
cp -f src/glog/raw_logging.h "$EXPORTED_INCLUDE_DIR/"
cp -f src/glog/stl_logging.h "$EXPORTED_INCLUDE_DIR/"
cp -f src/glog/vlog_is_on.h "$EXPORTED_INCLUDE_DIR/"

checking for a BSD-compatible install... /opt/homebrew/opt/coreutils/libexec/gnubin/install -c
checking whether build environment is sane... yes
checking for arm-apple-darwin-strip... no
checking for strip... strip
checking for a thread-safe mkdir -p... /opt/homebrew/opt/coreutils/libexec/gnubin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for arm-apple-darwin-gcc... /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.5.sdk
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... yes
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.5.sdk accepts -g... yes
checking for /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.5.sdk option to accept ISO C89... none needed
checking whether /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.5.sdk understands -c and -o together... yes
checking for style of include used by make... GNU
checking dependency style of /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.5.sdk... gcc3
checking how to run the C preprocessor... /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.5.sdk -E
checking whether we are using the GNU C++ compiler... yes
checking whether /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.5.sdk accepts -g... yes
checking dependency style of /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.5.sdk... gcc3
checking build system type... 
/Users/milly/Library/Caches/CocoaPods/Pods/External/glog/2263bd123499e5b93b5efe24871be317-73c24/missing: Unknown `--is-lightweight' option
Try `/Users/milly/Library/Caches/CocoaPods/Pods/External/glog/2263bd123499e5b93b5efe24871be317-73c24/missing --help' for more information
configure: WARNING: 'missing' script is too old or missing
Invalid configuration `arm64-apple-darwin20.3.0': machine `arm64-apple' not recognized
configure: error: /bin/sh ./config.sub arm64-apple-darwin20.3.0 failed

Stack

   CocoaPods : 1.10.2
        Ruby : ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20]
    RubyGems : 3.0.3
        Host : macOS 11.2.3 (20D91)
       Xcode : 12.5.1 (12E507)
         Git : git version 2.31.1
Ruby lib dir : /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib
Repositories : trunk - CDN - https://cdn.cocoapods.org/

Installation Source

Executable Path: /usr/local/bin/pod
Mr0cket commented 3 years ago

As for initialising a react native project with the latest react native version I fail at the following step:

> $ npx react-native init rnfbdemo
✔ Downloading template
✔ Copying template
✔ Processing template
✖ Installing CocoaPods dependencies (this may take a few minutes)
✖ Installing CocoaPods dependencies (this may take a few minutes)
error Error: Failed to install CocoaPods dependencies for iOS project, which is required by this template.
Please try again manually: "cd ./rnfbdemo/ios && pod install".
CocoaPods documentation: https://cocoapods.org/

🤷‍♂️

mikehardy commented 3 years ago

Looks like a network issue. If your doing Rosetta2 you need none of this. This issue is for Apple silicon / arch am64 native.

James-Wilkinson-git commented 3 years ago

I tried all these any many others, still getting errors. On a brand new machine MacOS Big Sur 11.5.2 Node 15.9 using Bash so Xcode finds node at 15.9 which was told is needed to compile too tried npx, tried npm, did the pod wipe and install with and without the post installer script, tried with and without rosetta, tried the exclude arm64 arch in code build settings, all just move the error somewhere else down the line.

Does no one have a contact at appl to just go wtf mang is with this architecture make it work apple is suppose to just work.

ld: building for iOS Simulator, but linking in dylib built for iOS, file '/Users/jameserwilk/Sites/hm-mobile-react-native/HiMama/ios/Pods/hermes-engine/destroot/Library/Frameworks/iphoneos/hermes.framework/hermes' for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I tried to upgrade react native but that broke the rest of NPM with dependency tree shaking :/

I run npm i react-native to get 0.65.1 but package json still says ^0.64.2

mikehardy commented 3 years ago

@jerw-git why are you using an unstable version of node? An LTS might be safer.

All my testing versions are converged via a script that pegs these versions at the moment - tested + working, on macOS 11.5.2

XCODE_WANTED_VERSION="Xcode 12"
XCODE_WANTED_VERSION_COMPLETE="${XCODE_WANTED_VERSION}.5.1"
BREW_WANTED_VERSION="3.2.9" # https://github.com/Homebrew/brew/releases
NVM_VERSION="0.38.0" # https://github.com/nvm-sh/nvm/releases
NODE_CURRENT_VERSION="v14.17.5" # https://nodejs.org/download/release/latest-v14.x/
YARN_VERSION="1.22.11" # https://github.com/yarnpkg/yarn/releases
JDK_VERSION_WANTED="11" # https://adoptopenjdk.net/archive.html
RVM_VERSION_WANTED="1.29.12" # https://github.com/rvm/rvm/releases
RUBY_VERSION_WANTED="ruby-3.0.2" # https://www.ruby-lang.org/en/downloads/
FASTLANE_VERSION_WANTED="2.191.0" # https://github.com/fastlane/fastlane/releases
COCOAPODS_VERSION_WANTED="1.10.2" # https://github.com/CocoaPods/CocoaPods/releases

I run npm i react-native to get 0.65.1 but package json still says ^0.64.2

So bump it in package.json to make sure and/or check package-lock.json ?

react-native 0.65 brings in it's own set of subtle compile issues but is worth it (to me) to get iOS+Hermes working on Apple Silicon

Here's my full set of workarounds there

 post_install do |installer|
    react_native_post_install(installer)

    # Apple Silicon builds require a library path tweak for Swift library discovery or "symbol not found" for swift things
    installer.aggregate_targets.each do |aggregate_target| 
      aggregate_target.user_project.native_targets.each do |target|
        target.build_configurations.each do |config|
          config.build_settings['LIBRARY_SEARCH_PATHS'] = ['$(SDKROOT)/usr/lib/swift', '$(inherited)']
        end
      end
      aggregate_target.user_project.save
    end

     # Flipper requires a crude patch to bump up iOS deployment target, or "error: thread-local storage is not supported for the current target"
    # I'm not aware of any other way to fix this one other than bumping iOS deployment target to match react-native (iOS 11 now)
    installer.pods_project.targets.each do |target|
      target.build_configurations.each do |config|
        config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '11.0'
       end
    end

    # ...but if you bump iOS deployment target, Flipper barfs again "Time.h:52:17: error: typedef redefinition with different types"
    # We need to make one crude patch to RCT-Folly - set `__IPHONE_10_0` to our iOS target + 1
    # https://github.com/facebook/flipper/issues/834 - 84 comments and still going...
    `sed -i -e  $'s/__IPHONE_10_0/__IPHONE_12_0/' Pods/RCT-Folly/folly/portability/Time.h`
  end

Does no one have a contact at appl to just go wtf mang is with this architecture make it work apple is suppose to just work.

I don't even know what this means. If you want it just work use nothing but Rosetta, they wrote a translation layer. This is simply not a constructive comment though, when it's really down to all the (non-Apple / not-apple-controlled) tooling working through compatibility :shrug:

Using the versions above, this test script seems to work for me (it integrates all the above hacks) and it's a pretty involved setup... https://github.com/mikehardy/rnfbdemo/blob/master/make-demo.sh

Mr0cket commented 3 years ago

Looks like a network issue. If your doing Rosetta2 you need none of this. This issue is for Apple silicon / arch am64 native.

My issue was actually caused due to homeBrew coreUtils which replaces some of the native libExecs with a GNU-based version causing strange behaviour while running configure on unknown archs (such as apple silicon).

The fix was to uninstall coreUtils from homeBrew.

Icasso commented 3 years ago

Finally got the rn-tester package running on my m1 mac mini! Nice! 💯

mikehardy commented 3 years ago

Just an update at least for the packages mentioned above:

Many React Native third party libraries, like the ones @mikehardy is involved in react-native-google-signin and react-native-fbsdk-next are not ready for Apple Silicon yet.

These work! I have my app successfully through a TestFlight build with react-native 0.65.1, the post_install hacks I posted above, Hermes enabled, Flutter enabled (although obviously not for release), google sign in via the current alpha version, and facebook sign in via the current stable version.

Other than the post_install section noted above and needing a google sign in alpha (for a moment, while it's testing), the M1 story is getting pretty solid for react-native.

mikehardy commented 3 years ago

More good news - this library lookup ordering thing is solved on react-native now, and should be in 0.66.0 https://github.com/react-native-community/releases/issues/246#issuecomment-910406770 - really excited, I think 0.66 will work out of the box on all apple machines without any workarounds needed

fkgozali commented 3 years ago

FYI, I and @mikehardy have been verifying v0.66.0-rc. in M1 machine on Xcode 12.5. and Xcode 13. So far, it seems like all M1 related issues have been resolved in 0.66 release candidates.

lubomyr commented 2 years ago

All problems with ios release build compilation via xcode on M1 which was fixed in stable versions 0.66.x, returned back in 0.67.0-rc.0 and rc.1. Maybe not all but problem with "error: thread-local storage is not supported for the current target" returned back for me :(

lunaleaps commented 2 years ago

@lubomyr We'll be picking this into the 0.67-rc.2: https://github.com/facebook/react-native/commit/03a0907868171253691d595fe09bee87caa5e2b1

You can follow progress here: https://github.com/reactwg/react-native-releases/discussions/1

mikehardy commented 2 years ago

Oh no! I was hoping the hacks could go away for good with the real fix. That thing is a :zombie: - as long as it's in and people can build though :crossed_fingers:

I actually tried to reproduce the M1 build in response to your post @lubomyr and got completely stuck on something not-react-native-related I think the old YogaKit.modulemap not found thing. Apple development is so touchy.

lubomyr commented 2 years ago

@lunaleaps, This problem resolved in 0.67-rc.2. Thanks

darrylyoung commented 2 years ago

I actually tried to reproduce the M1 build in response to your post @lubomyr and got completely stuck on something not-react-native-related I think the old YogaKit.modulemap not found thing. Apple development is so touchy.

@mikehardy Ah, yeah. I've seen that YogaKit.modulemap not found issue come back recently. It seems pretty random when it does, too. Like you said, all this is so touchy!

Does anyone know definitively what the current state is regarding these common M1-related issues? I see some people say they're gone in 0.66, some say they're back in 0.67, and others saying they're resolved in 0.67.

I'm running an ejected/bare Expo project and recently upgraded to SDK 43 which sets React Native to 0.64.2. My problem there is that I want Hermes but I'm on an M1 Mac so, as far as I can tell, I can't build a Hermes app for the Simulator unless I upgrade to React Native 0.65. Doing that, though, I get the following error, which I see many others are having:

Typedef redefinition with different types ('uint8_t' (aka 'unsigned char') vs 'enum clockid_t')

According to other threads, this is fixed with React Native 0.66 so I figured I'm already off the recommended version for Expo so why not go to the latest and get the supposed M1-related fixes. I did that and I'm still getting the issue. Now, though, I see people say it's related to iOS deployment version and to set that to 11 (I have 12 and have had that for a year now). Doing that, though, breaks Expo as something related to the SDK 43 changes requires a minimum version of 12. That completes the loop and leaves me stuck.

I thought the changes brought with __apply_Xcode_12_5_M1_post_install_workaround(installer) fixed the issue but I'm still seeing it both with Flipper enabled (with no versions pinned or with versions pinned) and with it removed completely (I've never actually used it so tried just removing it from the Podfile). Same issue.

For context, here's my Podfile after upgrading to React Native 0.66:

require File.join(File.dirname(`node --print "require.resolve('expo/package.json')"`), "scripts/autolinking")
require File.join(File.dirname(`node --print "require.resolve('react-native/package.json')"`), "scripts/react_native_pods")
require File.join(File.dirname(`node --print "require.resolve('@react-native-community/cli-platform-ios/package.json')"`), "native_modules")

platform :ios, '12.0'

require 'json'
podfile_properties = JSON.parse(File.read('./Podfile.properties.json')) rescue {}

target 'xxx' do
  use_expo_modules!
  config = use_native_modules!

  use_react_native!(
    :path => config[:reactNativePath],
    :hermes_enabled => podfile_properties['expo.jsEngine'] == 'hermes'
  )

  post_install do |installer|
    react_native_post_install(installer)
    __apply_Xcode_12_5_M1_post_install_workaround(installer)
  end
end

I know it's a complicated issue and that people are working to fix things like this but it's particularly frustrating when things seem to be so temperamental. Builds seem to work one minute and not then next. There are a bunch of hacks in various threads that supposedly fix these common issues but looking at responses, they work for some and not for others. It'd be great to have an official M1 Mac setup guide or something like that. I started gathering the initial development environment setup information from people so I could piece together a how-to but even with that stuff set up, I can't find a way around these other M1-related issues.

Thanks to everyone who's working to figure out all this stuff, though. It's very much appreciated.

mikehardy commented 2 years ago

M1 issues were mostly fixed in 0.66.x with my workaround baked into that podfile method call. They came back briefly in 0.67rc0 because there was an underlying real fix we had high hopes for. It did not work completely so the workaround was cherry picked back in for 0.67rc1+

M1 should be working for non Expo out of the box from template

For Expo if you need iOS deployment version 12 take special note if the code inside the workaround. Part of it is a hack that depends on ios deployment version and you may need to modify it, as it blindly assumes with no verification that 11 is the goal version to match the template just like an awful hack should

darrylyoung commented 2 years ago

@mikehardy Thank you, Mike. Since changing the version in the hack, I no longer see that error. Much appreciated.

mikehardy commented 2 years ago

Perhaps the time-redefinition-avoidance part of the Podfile compile hack - since it appears it will live longer then I/we thought it would could use a little refresh where it either takes a parameter of your ios deployment version, or attempts to dynamically detect it so that it is not quite so brittle. The assumption that target deployment version is 11 does fit most people, but if Expo is 12 there will be a huge percentage of users like yourself that are bitten by this

mikehardy commented 2 years ago

I have added this to the RN67 release planning discussion - PRs welcome as ever :raised_hands: - it takes all of us in order for this to work on the M1 :) https://github.com/reactwg/react-native-releases/discussions/1#discussioncomment-1619523

Yonom commented 2 years ago

I'm a bit confused about the Android JDK version to use I cannot find any arm64 builds on homebrew for the recommended JDK (adoptopenjdk8)

mikehardy commented 2 years ago

Hey @Yonom :wave: - I'm getting close to merging+releasing your react-native-firebase patch, thanks again for that - I would definitely use JDK11 at this point, the android default toolchain has moved to JDK11 as their standard with the latest release of Android Studio a few months back (so compatibility issues in the ecosystem here should be ironed out)

Also JDK11 required to avoid compilation issues when you start targetting sdkVersion 31 (see https://issuetracker.google.com/issues/190734097 and https://github.com/facebook/react-native/pull/32540#issuecomment-961326721)

This is not official guidance yet per react-native docs but it really should be at this point. I'll see about doing a PR for that to the docs site unless you beat me to it

Also not sure if there is an arm64 build of JDK11 (as discussed on a separate comment - I think without Rosetta2 installed life will be difficult), but this at least answers your JDK version question. (Edited to add: looks like the CI infrastructure just got moved to JDK11 so this is getting close to being officially recognized - it already works for regular app but it also has to work for Buck, CI etc etc before it is officially sanctioned https://github.com/facebook/react-native/pull/32186))

yurakruhlyk commented 2 years ago

@Yonom Hi I use Azul’s Zulu JDK and it works perfect on m1, they support m1(arm).

onlyling commented 2 years ago

Hello, there are some questions. Use @react-native-community/cli to create a project and it runs normally, but if you add jpush-react-native, you will get an error. There is no problem with the Inter platform, but Apple’s M1 will have problems, only iOS has problems, and the Android development model is good. What's the problem? More information -> https://github.com/jpush/jpush-react-native/issues/860

mikehardy commented 2 years ago

@onlyling they're packaging their library incorrectly and it doesn't have the necessary slices. Nothing to do with the build here at all. I posted this link there https://onesignal.com/blog/xcframeworks-guide/ - it's the solution for that library.

pixelknitter commented 2 years ago

I've tried the above steps and even tried some others, but still get Command PhaseScriptExecution failed with a nonzero exit code for FBReactNativeSpec:

These two seem to be where it fails consistently:

CompileSwiftSources normal x86_64 com.apple.xcode.tools.swift.compiler (in target 'EXLinearGradient' from project 'Pods')
CompileSwiftSources normal arm64 com.apple.xcode.tools.swift.compiler (in target 'EXLinearGradient' from project 'Pods')

Attempts to resolve

I've tried the following and perhaps a bit more:

I've considered wiping the data transfer for the 2021 MacBook I got and rebuilding my dev environment as my colleagues (w/ M1 macs) aren't having the same issue with the project. However, I'm certainly open to suggestions on how I might debug this effectively. I keep hitting dead ends.

Env

OS: OSX Monterey 12.0.1 Chip: Apple M1 XCode: 13.1 (13A1030d)

Project

standalone - with some EXPO modules baked in use_expo_modules! Build Target: iOS 12.0 React Native: 0.66.5

daniel-keen commented 2 years ago

@darrylyoung

I know it's a complicated issue and that people are working to fix things like this but it's particularly frustrating when things seem to be so temperamental. Builds seem to work one minute and not then next. react-native: 0.66.4

Have got "YogaKit.modulemap not found" error on M1 chip. Running with rosetta solves the issue, but it's been quite annoying...

mikehardy commented 2 years ago

The modulemap thing is usually that podfile min iOS deployment version and xcode app info iOS minimum deployment don't agree

mikehardy commented 2 years ago

@thexdd just had a report from the react-native-fbsdk-next repo that YogaKit.modulemap may also happen if you have an old .pbxproj without current settings, and bitcode is disabled. User reported successful M1 build after enabling bitcode.

gio-moros commented 2 years ago

We were running into many issues with the M1 version. We resolved the majority by first making sure we were running the latest Xcode Version 13.2 (13C90) Followed by this 1.npx react-native-clean-project.

  1. Go inside the ios folder and make sure the Podfile.lock and Pods/are removed.

If the problem persists, go into the ios folder and run pods using the old architecture

arch -x86_64 pod install --repo-update

Hope this helps!

Eafy commented 2 years ago

Yarn version: 1.22.17

Node version: 14.8.0

Platform: darwin x64

System: macOS 12.1

Trace: Error: https://registry.npmmirror.com/node-notifier/download/node-notifier-5.4.5.tgz: incorrect data check at Zlib.zlibOnError [as onerror] (zlib.js:180:17)

npm manifest: { "name": "react-native-baidu-map-zj", "version": "1.4.3", "description": "Baidu Map SDK modules and view for React Native(Android & IOS), support react native 0.57+. 百度地图 React Native 模块,支持 react native 0.57+,已更新到最新的百度地图SDK版本。", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url": "git+https://github.com/eafy/react-native-baidu-map-zj.git" }, "keywords": [ "Baidu Map", "React Native" ], "author": "Eafy", "license": "MIT", "bugs": { "url": "https://github.com/eafy/react-native-baidu-map-zj/issues" }, "homepage": "https://github.com/eafy/react-native-baidu-map-zj#readme", "devDependencies": { "react": "16.13.1", "react-native": "^0.63.2" } }

yarn manifest: No manifest

Lockfile: No lockfile

Error: error An unexpected error occurred: "https://registry.yarnpkg.com/node-notifier/-/node-notifier-5.4.5.tgz: incorrect data check". info If you think this is a bug, please open a bug report with the information provided in "/Users/lzj/Desktop/Git/react-native-baidu-map-zj/yarn-error.log". info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.

Eafy commented 2 years ago

Yarn version: 1.22.17

Node version: 14.8.0

Platform: darwin x64

System: macOS 12.1

Trace: Error: https://registry.npmmirror.com/node-notifier/download/node-notifier-5.4.5.tgz: incorrect data check at Zlib.zlibOnError [as onerror] (zlib.js:180:17)

npm manifest: { "name": "react-native-baidu-map-zj", "version": "1.4.3", "description": "Baidu Map SDK modules and view for React Native(Android & IOS), support react native 0.57+. 百度地图 React Native 模块,支持 react native 0.57+,已更新到最新的百度地图SDK版本。", "main": "index.js", "scripts": { "test": "echo "Error: no test specified" && exit 1" }, "repository": { "type": "git", "url": "git+https://github.com/eafy/react-native-baidu-map-zj.git" }, "keywords": [ "Baidu Map", "React Native" ], "author": "Eafy", "license": "MIT", "bugs": { "url": "https://github.com/eafy/react-native-baidu-map-zj/issues" }, "homepage": "https://github.com/eafy/react-native-baidu-map-zj#readme", "devDependencies": { "react": "16.13.1", "react-native": "^0.63.2" } }

yarn manifest: No manifest

Lockfile: No lockfile

Error: error An unexpected error occurred: "https://registry.yarnpkg.com/node-notifier/-/node-notifier-5.4.5.tgz: incorrect data check". info If you think this is a bug, please open a bug report with the information provided in "/Users/lzj/Desktop/Git/react-native-baidu-map-zj/yarn-error.log". info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.

Node.js upgrade from v14 to v16 has solved this problem.

gio-moros commented 2 years ago

We were running into many issues with the M1 version. We resolved the majority by first making sure we were running the latest Xcode Version 13.2 (13C90) Followed by this 1.npx react-native-clean-project. 2. Go inside the ios folder and make sure the Podfile.lock and Pods/are removed.

  • If you need a sample Podfile. Feel free to use this SampleFile
  1. Then go back to the main folder and run `npx pod-install'

If the problem persists, go into the ios folder and run pods using the old architecture arch -x86_64 pod install --repo-update

Hope this helps!

Eronred commented 2 years ago

When I run my ios simulator, it's directly shooting this error to my face but if I open Xcode with Rossetta and re-build/open the simulator again, it is working with zero error and fast. Really what is wrong with this M1 chip? React web is so good but there are so many errors for the mobile side..

Screen Shot 2022-02-02 at 1 51 27 AM
gio-moros commented 2 years ago

@Eronred start by cleaning the ios folder doing cd ios && xcodebuild clean && rm -rf ~/Library/Caches/CocoaPods && rm -rf Pods && rm -rf ~/Library/Developer/Xcode/DerivedData/* && pod cache clean --all && pod deintegrate

Additionally, there are some dependencies in your project that require an x86 to build after cleaning the ios package run arch -x86_64 pod install --repo-update

Hope this helps.

saifmattias commented 2 years ago

@Eronred start by cleaning the ios folder doing cd ios && xcodebuild clean && rm -rf ~/Library/Caches/CocoaPods && rm -rf Pods && rm -rf ~/Library/Developer/Xcode/DerivedData/* && pod cache clean --all && pod deintegrate

Additionally, there are some dependencies in your project that require an x86 to build after cleaning the ios package run arch -x86_64 pod install --repo-update

Hope this helps.

This helped me a looooot, could not run a simple app on physical iphone. But this command solved it :)