React Native - Xcode Build Using Bad Environment Variabled - ios

I am building a react-native project using "sudo react-native run-ios" command. The build environment variables are acting weird, not returning the expected values such as /Users/userName for $HOME. Instead, I'm getting the following values:
for $USER_LIBRARY_DIR - will result: /var/root/Library
for $HOME - will result /var/root
this is happening ONLY when I build the react project. The Xcode project is compiling smoothly.
Why don't the environment variables return the correct value? I am using a secondary (admin) user on my macbook pro. Might that be related?

Using sudo will make the command run as root, skip the sudo part, delete any build intermediaries and you should be fine.

Related

Xcode build rules script "command not found" on M1

I have a project that runs some utility I've installed using homebrew on one of its build rules.
On my M1 mac, homebrew is installed on /opt/homebrew/bin.
I have eval "$(/opt/homebrew/bin/brew shellenv)" present in my ~/.zprofile file, but it seems like Xcode doesn't respect that (or maybe overrides it?).
When trying to evaluate the PATH variable during Xcode run script command I get the following:
PATH=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/bin:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/libexec:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/usr/bin:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/usr/local/bin:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/local/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
For clearance, this is the phase I was running, under Build rules.
I've managed to find two workarounds that work with this issue, the first is to symlink the program you want from /opt/homebrew/bin onto /usr/local/bin (protoc in my case)
sudo ln -s /opt/homebrew/bin/protoc /usr/local/bin/protoc
The second is to add the following line to the build rule script:
eval "$(/opt/homebrew/bin/brew shellenv)"
This has the disadvantage of not working on non M1 Macs.
There is a possible, very simple solution provided by this answer.
Basically, freshly upgraded versions of XCode (for example, 13.4.1) running on Apple Silicon-powered Macs (like my M1 MacBook Pro), have components that cannot properly use/output the appropriate object files in all the output platform combinations you need.
This leads to some "interesting" issues and error message cross-overs that find "almost correct" answers on StackOverflow. Hence the many suggestions to exclude "arm64" platform and similar from the build options.
The simple answer to all of that is... to just run XCode with Rosetta enabled. Rosetta will engage with the components that miss the needed cross-platform capabilities.
Here is an example of how to enable an application to use Rosetta. You select the app icon, then go to the File menu and select "Get Info". Then click the "Open using Rosetta" checkbox.

Flutter fastlane ios how to pass --dart-define

I can't find answer for this.
My app needs parameters defined --dart-define=ENVIRONMENT="$APP_ENV"
There is no problem building Android, but how to pass those while build ad-hoc in fastlane?
I've prepared build scripts that run:
flutter pub get
flutter build ios --config-only \
--flavor prod \
--dart-define=ENVIRONMENT="$APP_ENV"
cd ios
bundle exec fastlane build_app_prod_ad_hoc
And my lane for fastlane is:
lane :build_app_prod_ad_hoc do
cocoapods
gym(
configuration: "AdHoc-prod",
export_method: "ad-hoc",
scheme: "prod",
export_options: {
provisioningProfiles: {
...
},
},
)
end
But I can see that my result doesn't have ENVIRONMENT set correctly.
Any ideas?
Solution:
Encoding and replacing your dart variable in flutter_export_environment.sh and Generated.xcconfig then running the app from Xcode/Fastlane directly will work fine.
------ Details -------
The issue: The variables passing via --dart-define won't reflect if you run the app from Xcode/ Fastlane directly without first running the flutter run/build command.
Reason: The following generated files are involved but not intended to update manually, but in our case running from Xcode or Fastlane to build the app, the dart variables used won't get updated. When you run the flutter run or build command, these files get updated with the values from --dart-define as Base64.
/ios/Flutter/flutter_export_environment.sh
ios/Flutter/Generated.xcconfig
When you directly build the app from Xcode or use Fastlane, the Generated.xcconfig from the ios folder inside the Flutter project code is being used to run/build the app.
Example:
In my case, we pass the ENV variable using --dart-define, but if you run directly from XCode without running the flutter build or run command first, these arguments won't update.
flutter run/build --flavor dev --dart-define ENV=dev
Dart define variable will store in the flutter_export_environment.sh and Generated.xcconfig in Base64 encoding.
DART_DEFINES=RU5WX1UEU9chZ2luZw==
Note: This is a workaround to avoid running the flutter run/build command followed by a Xcode/Fastlane run to solve the issue. Not the best solution, but I hope it may help someone.

xcode - command line build - required content for platform watchOS is missing IBBuildSupport

I am trying to build my project (iPad app) from go agent. While building from command line, I am getting below error.
Error log:
Requested but did not find extension point with identifier Xcode.IBBuildSupport.AssetCatalog.AssetTagScanner.TypeExtension
go-agent: error: Initialization failed.
Reason: Required content for platform watchOS is missing
I am not using/referring to any of watchOS related libraries. Any one faced similar kind of error?
This seems to be a problem in GoCD to do with an environment variable that is being set by the GoCD agent.
A Github issue has been opened to fix this.
Meantime, if you have a look at the chat there you'll see that the original reporter has found a workaround, which is to call unset CFProcessPath before invoking the xcodebuild command.
In my case, I changed the custom command I was using to:
unset CFProcessPath; bundle exec fastlane build_for_test
and the build is passing
(I tried using a separate command for the unset CFProcessPath, but the environment variables appear to revert between them - so I was forced to combine the two).

Command `bundle` unrecognized. Did you mean to run this inside a react-native project?

I am currently upgrading to the most recent version of react from 0.11.0 branch.
I have upgraded successfully through each step with minimal issues but when I got to 0.14.0 I hit some issues with:
node_modules/react-native/packager/react-native-xcode.sh
No matter what I do all my machines seem to get this same error.
Command `bundle` unrecognized. Did you mean to run this inside a react-native project?
I also run this same command that script runs from my terminal in the same location and it works fine, I have also set the path as well and before the script runs no mas, set path inside the script still nothing.
I am hoping someone else has saw this and has found a solution or is there a work around so we dont need this script.
I started this project with react-native ~0.6.0 maybe even a little earlier.
I have even removed nvm to combat this issue and using single install of node which is on PATH in both conditions.
Thanks!
Have you installed the latest version of react-native-cli globally?
npm uninstall -g react-native-cli
npm install -g react-native-cli
I personally preferred to install it as a dev dependency.
yarn add -D react-native-cli

Setup of React Native using ndenv on OS X

I'm running into PATH difficulty setting up a new React Native iOS project in Xcode 7.1. I'm trying to use ndenv to manage node versions, which means ~/.ndenv/shims needs to be on PATH. To that end, I have PATH set as a user-defined project setting as $PATH:~/.ndenv/shims. The Xcode settings UI shows the correct expanded PATH value.
Yet the project, generated by react-native init, still fails to build. The custom build script fails to find react-native:
../node_modules/react-native/packager/react-native-xcode.sh: line 36: react-native: command not found
react-native is installed correctly, but the environment variables dump in the build output makes clear that ~/.ndenv/shims isn't on PATH. My PATH settings don't appear in the build environment. However, if I set a random user-defined setting (e.g. FOOBAR=baz), that does correctly show up in the build environment.
That result plus further inspection of PATH in the build environment makes clear that Xcode is overriding PATH somewhere along the line.
So in order of preference:
How do I correctly append to PATH for a custom build script in Xcode 7?
Alternatively, what's the best (least hacky?) way of getting ndenv-managed npm modules on PATH for the build script?
Modifying react-native-xcode.sh is out of the question, since that's a part of the external react-native npm module.
UPDATE 1
As a temporary workaround, I've bailed on ndenv and installed node via homebrew. This puts react-native on the (apparently-hardcoded) Xcode path at /usr/local/bin, and works to get the build running. I'm not going to submit or accept this as a solution, since it doesn't allow the repeatable control of node versions that ndenv does.
The problem seems to be that the react-native-xcode.sh script depends on the react-native-cli package to be in PATH. This issue was reported to the project in these two issues on Github:
https://github.com/facebook/react-native/issues/3974
https://github.com/facebook/react-native/issues/3948
I expect a fix to be implemented quite soon, because this is a major problem for anyone using the react-native cli to create new projects.
There are a few work arounds for this problem, but none of them are perfect. Which one would work for you depends on your node / npm setup:
I myself installed node via nvm and just needed to add export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.shbefore running the react-native-xcode.sh.
For this to work you need to have react-native-cli installed globally.
If you need help finding a solution for your specific setup, please give me some more information about your node setup. ( or you can try one of the other solutions pointed out in the issues I linked above. )
Had the same issue. The work around called out in the link below is working for me. Haven't found a permanent solution.
The XCode $PATH differs from terminal $PATH and it doesn't include path of nvm nodes (~/.nvm/versions/...). This can be temporarily fixed by adding source ~/.bash_profile to the beginig of react-native-xcode.sh
https://github.com/facebook/react-native/issues/3948
This issue was effectively fixed when facebook/react-native#5518 landed. react-native-xcode.sh now respects a NODE_BINARY environment variable that tells it where to find node on the build system. Under your app's target, see Build Phases > Bundle React Native code and images and set the shell script code to:
export NODE_BINARY=$HOME/.ndenv/shims/node
../node_modules/react-native/packager/react-native-xcode.sh
If your node is in a different location, change the value of NODE_BINARY to suit your installation. Here's a screencap from Xcode 7.1 to aid navigation:
It looks like a fix for this was just published as release 0.14.2
https://github.com/facebook/react-native/releases/tag/v0.14.2

Resources