Joe Susnick
Joe Susnick

Reputation: 6772

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.

Upvotes: 4

Views: 2796

Answers (2)

ByteByByte
ByteByByte

Reputation: 1

Thanks, the above did help me. Here is what I did.

I removed the package dependency in the using project and then deleted the ../Xcode/DerivedData/usingProject directory. Then, I opened the swift package in Xcode and did a git checkout on the lates version (tagged 1.0.11). Lastly, in Xcode, I selected the using project -> Swift Packages and added (+) the local URL to the package's folder:

file:///Users/me/repository/mySwiftPackageFolder

The build finally worked on the using project.

Note, simply deleting the derived data or the mentioned checkouts directory doesn't fix the error. I needed a clean derived data, a good package checkout, and a package folder URL.

Upvotes: 1

Joe Susnick
Joe Susnick

Reputation: 6772

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
  }

Upvotes: 1

Related Questions