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?
6
Answers
Something that seemed to work for me today:
After a few seconds, everything seemed to be normal again.
I think I may have found the issue here! After a ton of digging it seems Xcode and Swift PM has a bug with repos using git@ rather than https://
Using ssh we are getting a hanging ref to remote/origin/main in our caches and derived data. When using https this is not the case. This makes sense as the only dep in our project using ssh are our internal deps.
I tested this by adding a brand new 3rd party dependency [email protected]:jpsim/Yams.git into our project and saw the cache in org.swift.swiftpm update incorrectly.
UPDATE: 10 days later and it seems this was the issue all along. However even after changing the references it took a full DerivedData/SPM cache/Package.resolved wipe before Xcode no longer had any references to the
git@
repos. I’ve filed a feedback with Apple for this bug.Had same issue, even created new repo with same content to see that it is working. In the end I found a solution that helped me.
swift package purge-cache
rm -rf ~/Library/Developer/Xcode/DerivedData
P.S.
purge-cache
w/o removingDerivedData
not worked, but it could be that only removingDerivedData
was required to solve this issue. Can not re-check as I can not reproduce this issue anymore.UPD: Step #3 not required.
After having this problem in XCode and AppCode, which does not have a "Reset Package Cache" button and takes forever re-indexing the whole iOS Framework when you delete the DerivedData folder, i tried to minimize the folders i needed to delete.
I ended up with the following:
Once you get the error message
[email protected]: An unknown error occurred. reference 'refs/remotes/origin/main' not found (-1)
, you~/Library/Developer/Xcode/DerivedData/
) (for AppCode, it’s located in~/Library/Caches/JetBrains/AppCode2022.2/DerivedData
)<YourProject>-<RandomCharacters>/SourcePackages/repositories/<FailingLibraryProject>-<RandomCharacters>/refs/
remotes
folderThat’s all it takes – at least for me. I guess you need to repeat the steps for every library project that’s failing for you.
And the best thing: I never need to do this again, no matter how many times i switch branches or dependency versions 🙂
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 affectedremotes
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.If you save the code in a file named
fix-spm-cache.sh
, then you can dochmod +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.I made a python script to fix the issue.
The idea is to get the latest commit hash and write it to Package.resolved, then resolvePackageDependencies for you.
You can get the script below, or download from here
Put the script in your xcode project, along with your
*.xcodeproj
or*.xcworkspace
.Then run
or
If you don’t have python: