Repository navigation
Description
Description
When opening Xcode not from the command line (via xed f.e.) but directly from Finder/Dock and the ccache option is enabled the build will fail because ccache can not be found even though it is installed. The issue exists because the PATH most certainly won't include the ccache binary (f.e. when installed via homebrew, it is located at /opt/homebrew/bin/ccache but Xcodes PATH does not include it). When setting the project up for ccache, the existence of ccache is checked. However this runs as part of pod install in a terminal environment with a potentially different PATH env variable. To fix this I think the ccache path found at pod install time should be passed to the ccache-clang and ccache-clang++.sh scripts.
Steps to reproduce
- Enable the ccache flag in your Podfile
react_native_post_install(
installer,
react_native_path,
:ccache_enabled => true
)- Install ccache
- Run pod install
- Open Xcode from Dock/Finder
- Open the projects xcworkspace
- Build the project
An error message will be visible stating:
[root_path]/node_modules/react-native/scripts/xcode/ccache-clang.sh: line 14: exec: ccache: not found
Command CompileC failed with a nonzero exit code
React Native Version
0.74.2
Affected Platforms
Build - MacOS
Output of npx react-native info
System:
OS: macOS 14.6.1
CPU: (8) arm64 Apple M1 Pro
Memory: 84.19 MB / 32.00 GB
Shell:
version: "5.9"
path: /bin/zsh
Binaries:
Node:
version: 20.11.0
path: ~/.asdf/installs/nodejs/20.11.0/bin/node
Yarn:
version: 1.19.0
path: ~/.asdf/shims/yarn
npm:
version: 10.2.4
path: ~/.asdf/plugins/nodejs/shims/npm
Watchman: Not Found
Managers:
CocoaPods:
version: 1.13.0
path: /Users/David.vonKnobelsdorff/.asdf/shims/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: Not Found
IDEs:
Android Studio: 2024.1 AI-241.15989.150.2411.11948838
Xcode:
version: 15.4/15F31d
path: /usr/bin/xcodebuild
Languages:
Java:
version: 17.0.7
path: /usr/bin/javac
Ruby:
version: 3.2.4
path: /Users/David.vonKnobelsdorff/.asdf/shims/ruby
npmPackages:
"@react-native-community/cli": Not Found
react: Not Found
react-native: Not Found
react-native-macos: Not Found
npmGlobalPackages:
"*react-native*": Not Found
Android:
hermesEnabled: true
newArchEnabled: false
iOS:
hermesEnabled: true
newArchEnabled: false
Stacktrace or Logs
[root_path]/node_modules/react-native/scripts/xcode/ccache-clang.sh: line 14: exec: ccache: not found
Command CompileC failed with a nonzero exit code
Reproducer
https://github.com/dehlen/rn-ccache-bug
Screenshots and Videos
No response
Activity
react-native-bot commentedon Aug 21, 2024
react-native-bot commentedon Aug 21, 2024
dehlen commentedon Aug 23, 2024
Checked with the latest version (0.75.2) and the issue persists.
cipolleschi commentedon Aug 28, 2024
Hi @dehlen thanks for opening the issue. This is the code that sets up ccache and the scripts
IIUC, you are in a situation where ccache is present but in a path that is not accessible by Xcode, correct?
Does it work if you build from command line using
yarn ios?dehlen commentedon Aug 28, 2024
Yes that works since its run from the terminal where my path is set. Ccache is installed via homebrew and the installation directory is part of my terminal path. However macOS applications do not inherit the path and therefore ccache can not be found. I think what would mitigate this problem would be to add a user defined setting like CCHACHE_BINARY go the targets from the ruby script as part of the post_install hook where the path is found and printed out. Then update the shell script to use this variable instead of calling exec ccache.
I recognize that you can build the project without modifications however to build it in Xcode itself and not from the Terminal is broken for me when enabling cchache and i think this could potentially be fixed.
cipolleschi commentedon Aug 29, 2024
@dehlen thanks for the additional information. The issue can surely be fixed. As of now, we are a bit busy with the rollout of the New Architecture which has the highest priority, so we need to wait a bit for this to happen on our side. However, if you had some spare cycle and you'd like to try and submit a PR, I can help out and make sure we merge it.
dehlen commentedon Aug 29, 2024
I'll give it a shot. I won't be able to have a look at this before next week but will comment back with a PR next week.
dehlen commentedon Sep 2, 2024
Sorry to comment again without having a PR ready for you. I just had a look at the problem. I think the .xcode.env (and .local) files would be the right place to input a new environment variable (CCACHE_BINARY). I can do this in scripts/cocoapods/utils.rb:create_xcode_env_if_missing. I would create a export for .xcode.env with
export CCACHE_BINARY=$(command -v ccache)and for the local variantecho 'export CCACHE_BINARY=#{ccache_path}' > #{file_path}.localif ccache is available. I then would assume calling the ccache scripts viawith-environment.sh ccache_scriptwould work.I would choose this behaviour for the following reasons:
command -v ccacheis used. If it is not in the PATH one has the ability to overwrite this in the .xcode.env.local. So no changes for existing users of this feature, unless you want to support this use case.However I am not quite sure why I can't make it run like that. Somehow when switching the CC, CXX, LD and LDPLUSPLUS variables to something like
/absolute/path/to/with-environment.sh "/absolute/path/to/clang/or/clang++-script.sh"errors our for me although I added the CCACHE_BINARY export to my environment. Executing the with-environment.sh. I then tried to call with-environment.sh directly from the clang scripts without changing the way CC, CXX, LD and LDPLUSPLUS are configured like this:However this does not work as well. The environment variables are not found f.e. PODS_ROOT is not found and therefore with-environment.sh does not find the xcode env files. I am willing to investigate a bit further but I feel like I am missing the kownledge regarding environment variables in Xcode and shell scripting to find a quick solution for now. I could pass the ccache path directly to the clang scripts however I am not sure whether this would be an acceptable solution?
cipolleschi commentedon Sep 2, 2024
Hi @dehlen, thanks for investigating the solution.
The
with-environment.shshould be effectively the best approach.I don't know why it is not working for the
CC, etc. settings... might it be because they expect a single script/executable with no parameters? It might be that Xcode uses that executable as starting point and passes extra parameters that do not work properly.Having the error might help.
dehlen commentedon Sep 2, 2024
Xcode passes parameters to ccache which is why I tried to call with-environment.sh from inside the ccache scripts. They pass all variables to ccache via $@. I will recreate my last approach and comment with the error messages.
EDIT: To make it even easier to debug I did the following things:
So I basically copy and pasted the relevant parts of with-environment.sh into the clang scripts to source the environment files. However the clang scripts do not received the PODS_ROOT environment, it is empty. Therefore sourcing does not work and the script fails. The same happens when running with-environment.sh instead of copy & pasting it directly into the script. PODS_ROOT is available as a user defined setting however the script does not receive these variables and I am not quite sure why. I tried changing the bang from
#!/bin/shto#!/bin/bashas with-environment.sh uses bash but this did not change anything.dehlen commentedon Sep 2, 2024
I added a printenv to the script to see what environment values are available inside the ccache script:
If I append the CCACHE_BINARY path like so for CC, LD, etc.:
this also fails with this error message:
At this point the only thing I could think of is adding this to the ccache scripts (which is obviously working for my case but most certainly will not work for everyone):
export PATH="$PATH:/usr/local/bin:/opt/homebrew/bin/"dehlen commentedon Sep 2, 2024
Another option might be to echo a warning if ccache can't be found and use clang or clang++ directly in that case. While this does not solve the issue at hand at least it would not result in a build issue. The user could then write some custom post_install hook or change the CC,LD environment variables to fix the problem locally. What do you think about this? I am not 100% sure which way would be preferred. Also it might be of course totally possible to find a better solution I haven't found yet. Open for any suggestions.
cipolleschi commentedon Sep 9, 2024
The warning can be a good alternative, but I'm afraid that people might not see it.
3 remaining items
krystofwoldrich commentedon Dec 3, 2024
Hello everyone, I would love to be able to configure CCACHE_PATH from
.xcode.env.local.I'm having the same ccache not found error when build my app directly from Xcode. When using RN CLI the build works as expected.
cipolleschi commentedon Dec 4, 2024
I understand that that's a problem, but I don't have time to work on this currently.
It would be great if some of you can prepare a PR to fix the behavior, we can then iterate on that one.
Use CCACHE_BINARY for ccache-clang and ccache-clang++ scripts
ste7en commentedon Dec 13, 2024
Hey everyone, I made a small contribution by fixing this error in #48257.
While it would be great to customise ccache's path in an .xcode.env(.local), I couldn't find a way to find and source this file in ccache scripts. Also I think this would be an improvement rather than a fix to the current issue.
cipolleschi commentedon Dec 16, 2024
Thanks for doing that @ste7en!
Add CCACHE_BINARY path to Xcode build settings and use it in ccache s…
cebrakadabra commentedon Dec 17, 2024
If one needs this urgently, patching is your friend.
Move in changes from #48257 in your
node_modules.Step 1: If you don't have patch-package:
add this to your package.json
Step 2:
test if it works for you & create patch file:
apply patch
elkinjosetm commentedon Jan 3, 2025
I found a solution that I think is a little bit less invasive and does not requires adding the CCACHE_BINARY path to the xcode project, but instead it pulls it all the time from the
.envfile, it's basically a combination of the solutions explained here.This allows you to customize the path on the
.localfile as you wishUpdate
node_modules/react-native/scripts/xcode/ccache-clang.shby replacing the lineexec ccache clang "$@"withexec $CCACHE_BINARY clang "$@"Update
node_modules/react-native/scripts/xcode/ccache-clang++.shby replacing the lineexec ccache clang++ "$@"withexec $CCACHE_BINARY clang++ "$@"Ultimately I patched react-native while an official solution is provided
bvoq commentedon Sep 5, 2025
@ste7en do you think this approach can be added to react?