How to add local Swift Package as dependency?

If you're looking for a "development pods" type of experience, where you've got a local copy of the package source and you want to edit that source from inside the host project without having to commit and push your changes there's not currently a way to do that. However... I have found a hack that gets you halfway there. I'm using this approach in a sample project I include in one of my package's repositories.

Before proceeding, the following assumptions are made:

• you have your package repo cloned to your machine
• create a sample Xcode project in your Package repo

The root directory of your package repo should look like this, more or less:

Package.swift
Sources/
Tests/
SampleProject/

Then perform the following steps:

  1. Use the Xcode GUI to add your package to the sample project. You'll need to use the actual HTTPS URL to the repo origin. Specify a branch name to a branch you're comfortable working from regularly, e.g. a "develop" branch or something.

  2. Open the sample project's project.pbxproj file in a text editor.

  3. Find the XCRemoteSwiftPackageReference section.

  4. Edit the repositoryURL value so the line reads: repositoryURL = "../";

Now you should be able to build your sample project and run it normally.

The big caveat here is that it won't see any changes you haven't committed to the specified branch on your local clone. The good news is you don't have to push those changes to remote. Xcode resolves the ../ path to a local file URL and clones your local clone, not the remote branch.

3 Likes