Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save packages downloaded by SPM into project GIT using Xcode 11

I started using new Xcode 11 which integrates SPM.

I added first dependency to my project:

enter image description here

but detected that files are not fetched into my project folder but into Xcode's cache:

enter image description here

I would like to commit all my dependencies files into my main project repository so my question is:

Is it possible to change location of fetched packages via SPM using Xcode 11?

like image 483
Kamil Harasimowicz Avatar asked Sep 06 '19 09:09

Kamil Harasimowicz


People also ask

How do I add a Swift package to Xcode project?

Open your Xcode project, navigate the File tab within the macOS bar, and click on “Add Packages”. In the Add New Package window, you can select from recently used or Apple Swift Packages. Alternatively, you can search for a package via the name or the URL to the Github page.

Where are SPM packages stored?

In order to cache the dependencies, they should be located in the project directory, but by default, SPM downloads the dependencies into the system folder: ~/Library/Developer/Xcode/DerivedData.

What is SPM in Xcode?

Also known as SwiftPM or SPM, it's been included in Swift since version 3.0. From the official repository: The Swift Package Manager is a tool for managing distribution of source code, aimed at making it easy to share your code and reuse others' code.

How do I import dependencies into Xcode?

To add a package dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter its repository URL.


2 Answers

It's somewhat possible, although the solution isn't necessarily a good or great practice, so I can't recommend.

  1. Set the DerivedData in workspace settings to be relative to the workspace.
  2. Add gitignore rules such that the workspace/WORKSPACE_NAME_DIR/SourcePackages/checkouts and related files are includes. Maybe best to ensure repositories directory is not included.
  3. Add a Run Script phase to remove .git and .gitignore files in the checkouts directory.

Obviously, this is fragile largely through fighting the way SPM works. The workspace settings are per person so it's not great in teams.

like image 82
Scott McKenzie Avatar answered Oct 13 '22 01:10

Scott McKenzie


SwiftPM integration has been setup to prevent this. It clones the files into a DerivedData/ProjectName-[RandomStuff]. You should commit your Package.resolved into the repo to ensure that you get the same version of each dependency across clones of the project.

like image 37
bscothern Avatar answered Oct 13 '22 03:10

bscothern