SPM unknown error reference not found when changing branch

Viewed 580

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?

4 Answers

Something that seemed to work for me today:

  • Temporarily remove the dependency that causes the issue
  • Close Xcode
  • Checkout the Package.resolved to cancel its changes
  • Reopen Xcode

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 git@github.com: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.

  1. Remove package from project
  2. Close Xcode
  3. Clean SPM - swift package purge-cache
  4. remove DerivedData - rm -rf ~/Library/Developer/Xcode/DerivedData
  5. Open project and add package again.

P.S. purge-cache w/o removing DerivedData not worked, but it could be that only removing DerivedData 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 git@library-project.git: An unknown error occurred. reference 'refs/remotes/origin/main' not found (-1), you

  1. go to your DerivedData folder (~/Library/Developer/Xcode/DerivedData/) (for AppCode, it's located in ~/Library/Caches/JetBrains/AppCode2022.2/DerivedData)
  2. go to the following directory, matching your project name, of course: <YourProject>-<RandomCharacters>/SourcePackages/repositories/<FailingLibraryProject>-<RandomCharacters>/refs/
  3. delete the remotes folder
  4. In XCode, click File -> Packages -> Resolve Package Versions
  5. In AppCode, click Tools -> Swift Package Manager -> Resolve Dependencies.
  6. Close and Re-open XCode

That'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 :)

Related