Search code examples
iosswiftxcodebashswift-framework

Xcodebuild clean command fails in build script but works in terminal


I have a build script written in Bash that is used to create an XCFramework for iOS. The script includes several xcodebuild commands, including a clean command to clean the workspace cache. When I run the script via SSH or manually in the terminal, it works perfectly fine without any errors. However, when I integrate the script into the Xcode build process as a build phase, the xcodebuild clean command fails with the following error:

Reason: Could not open workspace file at ~/apps/PHCore/PHCore/PHCore.xcworkspace/contents.xcworkspacedata

I have verified that the path to the workspace file is correct and that the file exists. Additionally, I've confirmed that the file permissions should be set correctly . When I run the xcodebuild clean command manually in the terminal using the same path and options as in my script, it works without any issues.

Here is the relevant part of my build script:

rm -rf ./build &&\
rm -rf ./output &&\

xcodebuild \
    clean \
    -workspace PHCore.xcworkspace \
    -scheme PHCore

And here is the output of ls -lG in the directory where my workspace file is located:

-rwxrwxrwx@  1 username  staff  6148 15 Feb 13:11 .DS_Store
drwxrwxrwx  12 username  staff   384 14 Feb 18:12 .git
drwxrwxrwx  12 username  staff   384 15 Feb 13:13 PHCore
drwxrwxrwx@  5 username  staff   160 15 Feb 13:20 PHCore.xcodeproj
drwxrwxrwx@  5 username  staff   160 14 Feb 17:37 PHCore.xcworkspace
-rwxrwxrwx@  1 username  staff  1284 14 Feb 17:35 build.sh

I replaced my username with "username"

I'm struggling to understand why the command fails only when run as part of the Xcode build process. Any insights or suggestions on how to troubleshoot and resolve this issue would be greatly appreciated.

Thank you!


Solution

  • Encountered the same issue if xcodebuild scripts are inside Run Script Phase. Workaround was to move build scripts from Run Script Phase into separate bash script file and using Run Script Phase to call the script file.

    Run Script Phase

    $SRCROOT/Scripts/runBuild.sh
    

    runBuild.sh

    xcodebuild clean archive \
        -target "TestFramework" \
        -scheme "SDK" \
        -archivePath "./build/ios.xcarchive" \
        -sdk iphoneos \
        -derivedDataPath "./derived-data" \
        SKIP_INSTALL=NO \
        ONLY_ACTIVE_ARCH=NO \
        BUILD_LIBRARY_FOR_DISTRIBUTION=YES \
        COPY_PHASE_STRIP=YES \
        GCC_GENERATE_DEBUGGING_SYMBOLS=NO
    
    xcodebuild clean archive \
        -target "TestFramework" \
        -scheme "SDK" \
        -archivePath "./build/ios-simulator.xcarchive" \
        -sdk iphonesimulator \
        -arch x86_64 \
        -arch arm64 \
        -derivedDataPath "./derived-data" \
        SKIP_INSTALL=NO \
        ONLY_ACTIVE_ARCH=NO \
        BUILD_LIBRARY_FOR_DISTRIBUTION=YES \
        COPY_PHASE_STRIP=YES \
        GCC_GENERATE_DEBUGGING_SYMBOLS=NO
    
    xcodebuild -create-xcframework \
        -framework "./build/ios.xcarchive/Products/Library/Frameworks/TestFramework.framework" \
        -framework "./build/ios-simulator.xcarchive/Products/Library/Frameworks/TestFramework.framework" \
        -output "./build/TestFramework.xcframework"