[Fixed] Xcode Error PhaseScriptExecution failed with a nonzero exit code – Xcode

by
Ali Hasan
axios flutter flutter-build flutter-ios-build xcode

Quick Fix: Update the generated …-frameworks.sh files to add the -f flag to the call to readlink. Replace:
source="\((readlink "\)")"
with:
source="\((readlink -f "\)")"

The Problem:

While attempting to create an archive for app distribution using Xcode 14.3, the archive remains disabled and the build fails with the error message "PhaseScriptExecution failed with a nonzero exit code". This issue persists despite implementing common troubleshooting measures like pod installation, cleaning the build folder, and executing pod deintegrate followed by pod install

The Solutions:

Solution

The error “PhaseScriptExecution failed with a nonzero exit code” occurs when there’s an issue with executing a script during the Xcode build process. In this case, the issue is with a script that is part of the Flutter plugin for Xcode and is used to set up the Flutter build environment.

To fix the issue, you need to edit the script and add the -f flag to the call to readlink. This flag tells readlink to follow symbolic links, which is necessary in this case.

Here’s how to do it:

  1. Open the Xcode project folder in your Terminal app.

  2. Go to the "Pods" directory.

  3. Open the file named Pods-[your project name]-frameworks.sh.

  4. Find the line that contains source="$(readlink "${source}")".

  5. Change the line to source="$(readlink -f "${source}")".

  6. Save the file and close it.

  7. Re-open Xcode and try to build your project again. The error should now be fixed.

Additional notes:

  • The -f flag is short for --follow-symlinks.
  • You may need to repeat these steps for multiple Pods-[your project name]-frameworks.sh files if you have multiple targets in your project.
  • If you’re still having trouble, you can try deleting the DerivedData folder and rebuilding your project from scratch.

Solution 2: Update the Podfile and XCode Configuration

Step 1: Edit the Podfile
Locate the Pods-[your-project-name]-frameworks.sh file in your project and modify the following section:

if [ -L "${source}" ]; then
  echo "Symlinked..."
  source=$(readlink -f "${source}")
fi

Step 2: Update XCode Configuration
In XCode, select the Runner target from the PROJECT section (not TARGETS). Adjust the configuration settings as follows:

  • Set Debug to Archive.
  • Set Release to Archive.
  • Set Profile to Archive.

Solution 3: Resetting PhaseScriptExecution

The error “PhaseScriptExecution failed with a nonzero exit code” can occur when the PhaseScriptExecution script in Xcode returns a non-zero exit code. To resolve this issue, try resetting the PhaseScriptExecution script. Here’s how:

  1. Go to the Xcode project settings for your target.

  2. Select the "Build Phases" tab.

  3. Find the "PhaseScriptExecution" script under the "Custom Build" section.

  4. Delete the script contents and replace them with the following:

set +e
/bin/sh "$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" "$@"
  1. Click "Apply" and then "Run".

This should reset the PhaseScriptExecution script and allow the build to succeed.

Solution 4: Update the Node Path

If you encounter the “PhaseScriptExecution failed with a nonzero exit code” error, it’s possible that the `.xcode.env.local` file located under `ios/` contains an outdated Node installation path. To resolve this:

  1. Open the `.xcode.env.local` file in a text editor.
  2. Locate the line that sets the `NODE_BINARY` path.
  3. Update the path to point to the correct Node installation. For example, if Node is installed at `/usr/local/bin/node`, you would update the line to:
export NODE_BINARY="/usr/local/bin/node"

After updating the path, save the file and rebuild your project in Xcode. This should resolve the PhaseScriptExecution error.

Solution 5: Edit Podfile

Within the Podfile located in the ios/Pods/Target Support Files/Pods-Runner directory, append -f to the following code:
source="$(readlink -f "${source}")"

Q&A

How to enable Lock & unlock option in Keychain Access?

Head over to Keychain Access. Select Lock & unlock again from the login option is disabled. How to enable it?

Where is the file that needs to be edited

Pods-[your-project-name]-frameworks.sh (…-frameworks.sh)

How to update node_modules in a react-native project?

updating node_modules/react-native/scripts/find-node.sh @ L7 set -e to set +e

Video Explanation:

The following video, titled "Xcode Command PhaseScriptExecution failed with a nonzero exit ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

This video provides further insights and detailed explanations related to the content discussed in the article.