Skip to content

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.

Xcode Error Screenshot

Solutions

Best solution for CocoaPods-related issues

Locate and modify the Pods-Runner-frameworks.sh script:

  1. Open your project in Finder
  2. Navigate to ios/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh
  3. Find the line source="$(readlink "${source}")"
  4. Add the -f flag to make it source="$(readlink -f "${source}")"
  5. Save the file

This resolves symlink evaluation issues common with CocoaPods:

bash
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

  1. Open ios/.xcode.env.local in a text editor
  2. Ensure the Node path is correct and points to an existing installation:
bash
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:

  1. Go to your target's Build Settings
  2. Search for "User Script Sandbox"
  3. Set to NO

Clean and Reinstall Dependencies

bash
# Flutter projects:
flutter clean
rm -rf ios/Pods ios/Podfile.lock
cd ios
pod deintegrate
pod install
bash
# 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:

  1. Select your project under PROJECT (not TARGETS)
  2. Go to Info > Configurations
  3. Double-check configurations match across all settings

Build Configuration Screenshot

Underlying Causes

The error typically originates from:

  1. Path resolution failures in CocoaPods scripts
  2. Missing dependencies or broken Node.js paths
  3. Incorrect environment variables in .xcode.env.local
  4. File permission issues with build scripts
  5. 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:

  1. Delete /ios folder
  2. Rebuild using: eas build --platform ios

For M-series Macs using Rosetta:

  • Enable Open using Rosetta for Xcode in Finder

See Also