Clearing Swift Package Manager Cache in Xcode
When working with Swift Package Manager, it’s common to encounter situations where we need to clear the cache because Xcode doesn’t refresh directories with the latest versions of files. This often happens when our dependencies are packages pointed to using a git branch, for example:
Adding a dependency in our manifest:
.package(
url: "https://repo-url.com",
.branch("master")
)
Or directly in an Xcode project:
To resolve cache-related issues, we have two options:
- Use the Reset Package Caches option in Xcode.
- Manually delete the entire cache on our machine.
Zeroth Option
Before explaining the two options mentioned above, it’s worth noting the zeroth option. This involves closing and reopening Xcode. Many times, this solves the problem, so it’s worth a try.
Using the Reset Packages Caches Option in Xcode
This is achieved by right-clicking on Xcode and selecting the Reset Packages Caches option:
Manually Deleting the Entire Cache on Our Machine
This usually works, but not always. When we see that it doesn’t work, we can use the following script:
rm -rf ~/Library/org.swift.swiftpm
rm -rf ~/Library/Caches/org.swift.swiftpm
These two directories store data about our packages to help us get cached versions and speed up the compilation process. However, if the cached version is not the one we need, we may face a situation where Xcode never updates the files properly.
It’s important to note that running these scripts deletes all cached data. So, if we have other packages in other projects that were working correctly, this might cause the next time we open the project to be a bit slower because Swift Package Manager will need to resolve dependencies from scratch.
A Third Option
A third solution, if the nature of the project allows, is to start using versioning in our packages and establish a strategy where we generate new versions frequently. This is more related to how the team decides to work.
Notes
Clearing the cache can also help resolve other issues related to security credential failures without apparent reason or conflicts in dependency resolution that don’t make sense.
If you want to read the Spanish version of this article, you can find it here: https://asynclearn.com/blog/como-limpiar-la-cache-de-swift-package-manager/