我遇到了一个问题,我可以使用XCode成功构建我的SWIFT应用程序,但当我try 在终端中使用"xcodeBuild"命令构建它时,我面临构建错误.在解析包依赖项时,它似乎总是失败.我想从命令行构建项目,但我不知道为什么会出现这个问题,也不知道如何解决它……

重现此问题的步骤

Failure case

  1. 从GitHub克隆我的应用程序
git clone git@github.com:fujidaiti/xcode-build-test.git
  1. 解析包依赖关系
xcodebuild -resolvePackageDependencies -project xcode-build-test/xcode-build-test.xcodeproj/ -scheme xcode-build-test

则应出现错误:

2023-05-31 15:55:11.778 xcodebuild[80462:1951627] DVTCoreDeviceEnabledState: DVTCoreDeviceEnabledState_Disabled set via user default (DVTEnableCoreDevice=disabled)
Command line invocation:
    /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -resolvePackageDependencies -project xcode-build-test/xcode-build-test.xcodeproj/ -scheme xcode-build-test

User defaults from command line:
    IDEPackageSupportUseBuiltinSCM = YES

Resolve Package Graph

Fetching from https://github.com/kareman/SwiftShell#others (cached)
skipping cache due to an error: Couldn’t fetch updates from remote repositories:
    fatal: https://github.com/kareman/SwiftShell#others/info/refs not valid: could not determine hash algorithm; is this a git repository?
Fetching from https://github.com/kareman/SwiftShell#others (cached)

skipping cache due to an error: Couldn’t fetch updates from remote repositories:
    fatal: https://github.com/kareman/SwiftShell#others/info/refs not valid: could not determine hash algorithm; is this a git repository?2023-05-31 15:55:14.411 xcodebuild[80462:1951627] Writing error result bundle to /var/folders/qb/wfmhbh3d6rdbp3z8lb8vl8zm0000gn/T/ResultBundle_2023-31-05_15-55-0014.xcresult
xcodebuild: error: Could not resolve package dependencies:
  Failed to clone repository https://github.com/kareman/SwiftShell#others:
    Cloning into bare repository '/Users/daichi/Library/Developer/Xcode/DerivedData/xcode-build-test-fvydfaqhyyxivzdsrdbbaxsykdxf/SourcePackages/repositories/SwiftShell-0eb504d5'...
    fatal: https://github.com/kareman/SwiftShell#others/info/refs not valid: could not determine hash algorithm; is this a git repository?

当然,建设该项目将失败:

$ xcodebuild -project xcode-build-test/xcode-build-test.xcodeproj -scheme xcode-build-test

2023-05-31 16:31:08.693 xcodebuild[82256:1980717] DVTCoreDeviceEnabledState: DVTCoreDeviceEnabledState_Disabled set via user default (DVTEnableCoreDevice=disabled)
Command line invocation:
    /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -project xcode-build-test/xcode-build-test.xcodeproj -scheme xcode-build-test

User defaults from command line:
    IDEPackageSupportUseBuiltinSCM = YES

Resolve Package Graph

Fetching from https://github.com/kareman/SwiftShell#others (cached)
skipping cache due to an error: Couldn’t fetch updates from remote repositories:
    fatal: https://github.com/kareman/SwiftShell#others/info/refs not valid: could not determine hash algorithm; is this a git repository?
Fetching from https://github.com/kareman/SwiftShell#others (cached)

skipping cache due to an error: Couldn’t fetch updates from remote repositories:
    fatal: https://github.com/kareman/SwiftShell#others/info/refs not valid: could not determine hash algorithm; is this a git repository?2023-05-31 16:31:09.245 xcodebuild[82256:1980717] Writing error result bundle to /var/folders/qb/wfmhbh3d6rdbp3z8lb8vl8zm0000gn/T/ResultBundle_2023-31-05_16-31-0009.xcresult
xcodebuild: error: Could not resolve package dependencies:
  Failed to clone repository https://github.com/kareman/SwiftShell#others:
    Cloning into bare repository '/Users/daichi/Library/Developer/Xcode/DerivedData/xcode-build-test-fvydfaqhyyxivzdsrdbbaxsykdxf/SourcePackages/repositories/SwiftShell-0eb504d5'...
    fatal: https://github.com/kareman/SwiftShell#others/info/refs not valid: could not determine hash algorithm; is this a git repository?

Success case

在Xcode中打开该项目应该会成功.

  1. 克隆,再一次
git clone git@github.com:fujidaiti/xcode-build-test.git
  1. 使用Xcode打开项目
open xcode-build-test/xcode-build-test.xcodeproj/

它将自动开始解析包依赖关系,并且应该会成功.

enter image description here

在此之后,我们可以使用xcodeBuild成功构建项目:

$ xcodebuild -project xcode-build-test/xcode-build-test.xcodeproj -scheme xcode-build-test

2023-05-31 16:12:45.885 xcodebuild[81087:1965576] DVTCoreDeviceEnabledState: DVTCoreDeviceEnabledState_Disabled set via user default (DVTEnableCoreDevice=disabled)
Command line invocation:
    /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -project xcode-build-test/xcode-build-test.xcodeproj -scheme xcode-build-test

...

RegisterWithLaunchServices /Users/daichi/Library/Developer/Xcode/DerivedData/xcode-build-test-fvydfaqhyyxivzdsrdbbaxsykdxf/Build/Products/Debug/xcode-build-test.app (in target 'xcode-build-test' from project 'xcode-build-test')
    cd /Users/daichi/Dev/tmp/xcode-build-test
    /System/Library/Frameworks/CoreServices.framework/Versions/Current/Frameworks/LaunchServices.framework/Versions/Current/Support/lsregister -f -R -trusted /Users/daichi/Library/Developer/Xcode/DerivedData/xcode-build-test-fvydfaqhyyxivzdsrdbbaxsykdxf/Build/Products/Debug/xcode-build-test.app

** BUILD SUCCEEDED **

环境

  • MacBook Air(M1,2020)
  • 操作系统:Ventura 13.4
  • Xcode 14.3
$ xcodebuild -version
Xcode 14.3
Build version 14E222b

$xcode-select --version
xcode-select version 2397.

$ git --version
git version 2.39.0

推荐答案

问题是由于项目中给出的包URL为

https://github.com/kareman/SwiftShell#others

结尾的#others位是问题所在,因为xcodebuild不理解这一点.FWIW #others是指向自述文件中某个部分的页面内链接.

我能够通过修改项目中的两个文件*来修复它.

xcode-build-test/xcode-build-test.xcodeproj/project.pbxproj
xcode-build-test/xcode-build-test.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

如果您只在这两个文件中找到#others,并将其作为存储库URL的一部分删除,您就会发现xcodebuild将成功地解析依赖关系.

将链接粘贴到Xcode项目中的人可能在单击自述文件内容中的链接后,从地址栏复制了该链接.我发现总是使用code按钮提供的链接来克隆Repo是一种很好的做法.

*严格来说,只需修正project.pbxproj即可.您仍然会得到关于#others事件的诊断,但这不会阻止包被正确解析.

Swift相关问答推荐

对多个项目的数组进行排序

我正在try 通过代码设置节中页眉和页脚的高度,但它不起作用

查找数组中 ** 元素 ** 的属性的最小值和最大值

如何在DATE()中进行比较

可选,类似于自定义类型的初始化

更改 SwiftUI 中按钮矩阵的填充 colored颜色

使用索引来访问 libc 定义的元组

在 RealityKit 中查找特定模型的 findEntity(named:) 方法的替代方法?

当字符串包含 \r\n 时,NSRegularExpression 不起作用

如何在闭包中使用构造 await sync

ConfirmationDialog取消swiftui中的错误

try 制作一个 Swift 扩展来替换字符串中的多次出现

`IndexSet` 永远不会是空的?

从 Finder 打开文件时,SwiftUI 的应用程序(_ openFile:) 从未调用过

为什么 Swift Tuples 只能在元素个数小于等于 6 的情况下进行比较?

如何在 Xcode 中的 UITests 下以通用方式访问后退栏按钮项?

快速的 AES 加密

Swift 覆盖实例变量

try 在解除分配时加载视图控制器的视图... UIAlertController

Xcode6 中的 Swift 类与 Cocoa Touch 类