Like others have answered, the issue can be worked around by deleting Swift package caches both in the derived data directory of your project, as well as the global Swift Package Manager cache in ~/Library/Caches/org.swift.swiftpm
. Even better, by deleting only the affected remotes
directory, but it can be time consuming to find the package and the folder.
Here is a small script I quickly put together that will delete all directories named remotes
both in the global Swift Package Manager cache and in the derived data repositories directory of your project.
#!/bin/bash
if [[ $# -eq 0 ]] ; then
echo 'Please call the script with the name of your project as it appears in the derived data directory. Case-insensitive.'
echo 'For example: ./fix-spm-cache.sh myproject'
exit 0
fi
# Delete all directories named "remotes" from the global Swift Package Manager cache.
cd ~/Library/Caches/org.swift.swiftpm/repositories
for i in $(find . -name "remotes" -type d); do
echo "Deleting $i"
rm -rf $i
done
# Find derived data directories for all projects matching the script argument, and
# delete all directories named "remotes" from source package repositories cache for those projects.
cd ~/Library/Developer/Xcode/DerivedData/
for project in $(find . -iname "$1*" -type d -maxdepth 1); do
for i in $(find "$project/SourcePackages/repositories" -name "remotes" -type d); do
echo "Deleting $i"
rm -rf $i
done
done
If you save the code in a file named fix-spm-cache.sh
, then you can do chmod +x fix-spm-cache.sh
to make the file executable. Afterwards, just run the script with the project name, like ./fix-spm-cache.sh myproject
, when you encounter the error in Xcode.
You can keep Xcode open while running the script. Once the script finishes executing, try resolving or updating your packages again. It should work, and it should be relatively fast since we are not deleting the whole cache.
This should resolve both the error mentioned in this topic, as well as the infamous SwiftPM.SPMRepositoryError error 5
that happens in Xcode 13, which is probably the same error, just with a different message.