解决 Sourcetree 升级导致 Xcode 无法解析 SPM 包的问题

543 阅读1分钟

解决 Sourcetree 升级导致 Xcode 无法解析 SPM 包的问题

最近在升级了 Sourcetree 后,Xcode 无法解析 Swift 包管理器(SPM)包,并且在构建时出现了如下错误:

fatal: cannot use bare repository /T/ResultBundle_2024-14-11_12-03-0031.xcresult
xcodebuild: error: Could not resolve package dependencies: Couldn’t get the list of tags:

问题分析

经过一番排查和清理缓存(包括删除了以下文件夹)后,问题依然没有解决:

rm -rf ~/Library/Developer/Xcode/DerivedData/
rm -rf ~/Library/Caches/org.swift.swiftpm
rm -rf ~/Library/Developer/Xcode/DerivedData
rm -rf ~/Library/org.swift.swiftpm
rm -rf ~/.swiftpm/Xcode/

通过搜索错误信息,终于找到了问题的根源,发现问题和 Sourcetree 的设置有关。具体来说,Sourcetree 在升级到 4.2.9 版本后,会在 ~/.gitconfig 中添加以下配置:

[safe]
    bareRepository = explicit

这个设置导致 Xcode 无法正常解析 SPM 包。

解决方法

  1. 打开 Sourcetree,进入 Settings... 设置页面,取消勾选 "Allow Sourcetree to modify your global Mercurial and Git configuration files"。这样可以防止 Sourcetree 在每次启动时自动修改 Git 配置。

  2. 打开 ~/.gitconfig 文件,找到以下部分并删除:

    [safe]
        bareRepository = explicit
    
  3. 保存 ~/.gitconfig 文件后,重新打开 Xcode,问题应该就解决了。

参考链接

总结

这个问题源自 Sourcetree 在升级后不当的配置推送,导致了 Xcode 在解析 SPM 包时的失败。通过取消 Sourcetree 修改全局配置的权限,并手动删除配置文件中的 bareRepository = explicit,可以有效解决该问题。希望这个记录能帮助遇到同样问题的开发者。