Search code examples
swiftxcodetravis-ciswift-package-manager

Travis CI - Swift Package Manage - checkout already exists?


Attempting to setup a sample project within a project that is configured to use Swift Package Manager and getting a strange error.

Project Structure:

MyProject/
    - Package.swift
    MyProject/
        - etc...
    Samples/
        - MySampleProject/

Swift Package Repository Setup:

MySampleProject is set up to use a local Swift package that should exist in the travis job:

file:///Users/travis/build/MyProject/MyProject <- pointed to branch: HEAD

Travis build command:

The travis script cd's into Sample/MySampleProject and runs:

xcodebuild clean build -target MySampleProject -sdk iphonesimulator

Error:

During the run, travis is claiming the checkout of the package already exists.

From the logs:

Resolve Package Graph

Fetching /Users/travis/build/<namespace>/MyProject

Cloning /Users/travis/build/<namespace>/MyProject

xcodebuild: error: Could not resolve package dependencies:

  An unknown error occurred. '/Users/travis/Library/Developer/Xcode/DerivedData/MySampleProject-agdvdspgtwakvignsmkkrkoxijnm/SourcePackages/checkouts/MyProject' exists and is not an empty directory (-4)

This works locally. Why would the checkout already be present in derived data for the travis builds? I'm not running any special commands to modify anything regarding SPM.


Solution

  • Two things to realize:

    1. The error message is very misleading. You will get this error message if there is not actually a commit to pull.

    2. A coworker pointed out that Travis is actually using refs/pull/$TRAVIS_PULL_REQUEST/merge for the value of $TRAVIS_COMMIT

    Full solution:

    Find the object in the plist that corresponds to XCRemoteSwiftPackageReference You can find this by opening YourProject.xcodeproj/project.pbxproj in a text editor and searching for XCRemoteSwiftPackageReference. Grab the ID since you'll need to hardcode it in your build step.

    Note: You'll need to update this if you remove and re-add the package.

    Next use PlistBuddy to update the branch to be the merge ref for the pull request.

        echo "Updating project file to point to merge commit at: refs/pull/$TRAVIS_PULL_REQUEST/merge"
        /usr/libexec/PlistBuddy \
            -c "set :objects:F4CEA53E23C29C9E0086EB16:requirement:branch refs/pull/$TRAVIS_PULL_REQUEST/merge" \
            YourProject.xcodeproj/project.pbxproj
    
        # Redirecting to /dev/null because we only care about errors here and the full output drowns Travis
        xcodebuild build -scheme YourScheme \
          -sdk iphonesimulator > /dev/null
      }