Fix PhaseScriptExecution Failed with Nonzero Exit Code in Xcode
Problem Statement
When building or archiving a Flutter app in Xcode, you may encounter the error PhaseScriptExecution failed with a nonzero exit code
. This typically occurs when running a shell script during build phases and prevents successful archive creation, sometimes even disabling the Archive menu option. The problem commonly appears in iOS projects using CocoaPods, especially those with Flutter or React Native dependencies.
Solutions
1. Modify Pods Scripts to Use readlink -f
Best solution for CocoaPods-related issues
Locate and modify the Pods-Runner-frameworks.sh
script:
- Open your project in Finder
- Navigate to
ios/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh
- Find the line
source="$(readlink "${source}")"
- Add the
-f
flag to make itsource="$(readlink -f "${source}")"
- Save the file
This resolves symlink evaluation issues common with CocoaPods:
if [ -L "${source}" ]; then
echo "Symlinked..."
source="$(readlink -f "${source}")" # Added -f flag here
fi
WARNING
This file may be regenerated when running pod install
. Always reapply the fix after dependency updates.
2. Verify Node.js Path in Environment Variables
Useful if Node.js paths are misconfigured
- Open
ios/.xcode.env.local
in a text editor - Ensure the Node path is correct and points to an existing installation:
export NODE_BINARY="$(command -v node)" # Recommended dynamic lookup
# OR explicit path for custom installations:
# export NODE_BINARY="/usr/local/bin/node"
3. Other Common Fixes
Disable User Script Sandboxing
In Xcode:
- Go to your target's Build Settings
- Search for "User Script Sandbox"
- Set to NO
Clean and Reinstall Dependencies
# Flutter projects:
flutter clean
rm -rf ios/Pods ios/Podfile.lock
cd ios
pod deintegrate
pod install
# React Native projects:
npx react-native-clean-project
cd ios && pod deintegrate && pod install
Path and Permission Checks
- Ensure no whitespace exists in your project path
- Verify script execution permissions:bash
chmod +x ios/Pods/Target\ Support\ Files/Pods-Runner/Pods-Runner-frameworks.sh
- Delete problematic files and regenerate:bash
rm ios/.xcode.env.local # Then re-run flutter pub get and pod install
Configure Build Settings
In Xcode:
- Select your project under PROJECT (not TARGETS)
- Go to Info > Configurations
- Double-check configurations match across all settings
Underlying Causes
The error typically originates from:
- Path resolution failures in CocoaPods scripts
- Missing dependencies or broken Node.js paths
- Incorrect environment variables in
.xcode.env.local
- File permission issues with build scripts
- Configuration mismatches in Xcode settings
Best Practices for Prevention
- Avoid spaces in project paths
- Keep consistent CocoaPods versions across your team
- Verify node installation paths after system updates
- Regularly clean build artifacts (
flutter clean
/npx react-native clean-project
)
Additional Considerations
Flutter Projects
- Run
flutter doctor --verbose
to verify environment health - Ensure CocoaPods is updated:
sudo gem install cocoapods
React Native Projects
For Expo projects:
- Delete
/ios
folder - Rebuild using:
eas build --platform ios
For M-series Macs using Rosetta:
- Enable Open using Rosetta for Xcode in Finder