Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPM unknown error reference not found when changing branch

Some of my project's dependencies are hosted on a private repository. It works most of the time, but sometimes when I change the current branch with git, I get this error :

❌ git@my_private_repo.git: An unknown error occurred. reference 'refs/remotes/origin/main' not found (-1)

And from that point, it's impossible to compile and my only option is to reset the SPM cache which takes a lot of time.

Any idea on what causes this and how to solve it?

like image 819
Xys Avatar asked Aug 30 '25 15:08

Xys


1 Answers

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.

like image 73
Srđan Avatar answered Sep 05 '25 16:09

Srđan