SPM未知错误参考在更改分支时找不到

发布于 2025-02-08 05:56:35 字数 314 浏览 2 评论 0 原文

我项目的某些依赖关系托管在私人存储库上。它在大多数情况下都可以工作,但是有时候,当我用git更改当前分支时,我会遇到此错误:

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

从那时起,这是不可能编译的,我唯一的选择是重置SPM CACHE需要大量时间。

关于原因是什么以及如何解决它的任何想法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

你的他你的她 2025-02-15 05:56:36

通过运行这些命令

默认来解决问题删除com.apple.dt.xcode dvtsourcecontrolaccountdefaultskeykekey

defaults delete com.apple.pode idesourcececontrolnownonknownonkoyssshhostsshhostssshhostsdefaultsdefaultskey

sudo sudo sudo sudo sudo killall ssh-agg

ssh-gabent.com: /forums/thread/662690“ rel =“ nofollow noreferrer”> https://forums.developer.apple.com/forums/thread/662690

Solve problem by running these commands

defaults delete com.apple.dt.xcode DVTSourceControlAccountDefaultsKey

defaults delete com.apple.dt.xcode IDESourceControlKnownSSHHostsDefaultsKey

sudo killall ssh-agent

More info: https://forums.developer.apple.com/forums/thread/662690

把昨日还给我 2025-02-15 05:56:36

我不必删除任何派生的数据或缓存。

我要做的就是:

  1. 更新到最新的软件包版本
  2. 重新启动xcode。

I didn't have to delete any derived data or caches.

All I had to do was:

  1. Update to Latest Package Versions
  2. Restart Xcode.

enter image description here

调妓 2025-02-15 05:56:35

就像其他人所回答的那样,可以通过在您的项目的派生数据目录中删除Swift软件包以及〜/library/caches/org.swift.swiftpm中的全球swift软件包管理器中的swift package caches来解决问题。 。更好的是,仅删除受影响的遥控器目录,但是找到包装和文件夹可能会很耗时。

这是我快速组合在一起的一个小脚本,它将在全局Swift软件包管理器缓存和您项目的派生数据存储库中删除所有名为远程>的目录

#!/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

如果将代码保存在名为 fix fix-cache.sh.sh 的文件中,则可以执行 chmod +x fix spm-cache.sh 使文件可执行文件。之后,只需使用项目名称运行脚本,例如 ./ fix-cache.sh myproject ,当您遇到Xcode中的错误时。

运行脚本时,您可以保持Xcode打开。脚本完成执行后,尝试再次解决或更新软件包。它应该起作用,并且由于我们没有删除整个缓存,因此应该相对较快。

这应该解决本主题中提到的错误,以及臭名昭著的 swiftpm.spmrepositoryerror错误5 在Xcode 13中发生的错误,这可能是相同的错误,只需带有不同的消息。

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.

淡笑忘祈一世凡恋 2025-02-15 05:56:35

我想我可能在这里找到了这个问题!经过大量的挖掘后,Xcode和Swift PM似乎使用GIT@而不是https://

使用SSH的错误,我们在我们的caches和派生的数据中获得了悬挂式ref c。使用HTTPS时,情况并非如此。这是有道理的,因为我们项目中使用SSH的唯一的DEP是我们的内部二;

我通过添加全新的第三方依赖性 jpsim/yams.git进入我们的项目,并在org.swift.swiftpm中看到了缓存。

更新:10天后,这似乎一直都是问题。但是,即使更改了参考文献,它也进行了完整的deveriveddata/spm缓存/软件包。在Xcode不再对 git@ repos的引用之前解决。我已经向Apple提交了此错误的反馈。

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.

韵柒 2025-02-15 05:56:35

在没有“重置软件包缓存”按钮的Xcode和AppCode中遇到此问题后,当您删除derivedData文件夹时,我将永远重新索引整个iOS框架,我尝试最大程度地减少删除所需的文件夹。

我最终得到了以下内容:
一旦获得错误消息 ,您

  1. 转到deriveddata文件夹(〜/library/developer/xcode/xcode/deriveddata/code>)(对于AppCode,for AppCode,它位于〜/library/caches/jetbrains/appcode2022.2/deriveddata 中)
  2. 转到以下目录,当然可以匹配您的项目名称,当然:< yourProject> - > - &lt randych; randomch; randych; gt; gt; gt; gt; /sourcepackages/repositories/< failinglibraryProject> - < randancharacters>/refs/
  3. 删除远程>遥控器 folder
  4. 文件夹,单击文件 - >包裹 - > 在AppCode中解析包装版本
  5. ,单击工具 - > Swift Package Manager->解决依赖关系。
  6. 关闭并重新打开Xcode

这就是全部需要的 - 至少对我而言。我想您需要重复每个为您失败的图书馆项目的步骤。
最好的事情是:无论我切换分支机构或依赖性版本多少次,我都不需要再做一次:)

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

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

毅然前行 2025-02-15 05:56:35

也有同样的问题,甚至创建了具有相同内容的新存储库,以确保其正常工作。最后,我找到了一种帮助我的解决方案。

  1. 从项目关闭
  2. XCode
  3. Clean SPM - SWIFT软件包净化库 - CACHE
  4. 删除deriveddata- rm -rf〜/library/developer/xcode/deriveddata
  5. 打开项目并再次添加软件包。

ps 吹扫 - cache w/o删除 deriveddata 无法使用,但可能只需要删除 deriveddata 才能解决此问题。无法重新检查,因为我再也无法再现此问题了。

upd:不需要步骤#3。

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.

不必你懂 2025-02-15 05:56:35

今天似乎对我有用的东西,只需几秒钟,并且不会删除派生的数据文件夹:

  • 中引起问题的依赖
  • 暂时删除swift package close xcode xcode close xcode
  • coble xcode nockout the软件包
  • 。 几秒钟后, Xcode

似乎再次正常。

Something that seemed to work for me today, that only takes a few seconds and that doesn't delete the derived data folder :

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

After a few seconds, everything seemed to be normal again.

暖树树初阳… 2025-02-15 05:56:35

我遇到了相同的错误,但是通过HTTPS添加了回购。我刚刚删除了〜/library/caches/org.swift.swiftpm 的缓存,并且似乎有效。重置软件包,清晰的派生数据无济于事。

I had the same error but with repo added via https. I just removed cache at ~/Library/Caches/org.swift.swiftpm and seems like it works. Reset packages, clear derived data didn't help.

携余温的黄昏 2025-02-15 05:56:35

在上面可以找到多个功能的解决方法。
我使用了这种方法:

  • 退出Xcode
  • deleting 〜/library/caches/org.swift.swiftpm
  • 删除〜/library/developer/xcode/xcode/deriveddata/yourprojectName
  • bainting xcode xcode xcode xcode

不幸,RE,RE - 下载软件包可能需要很长时间 - 当只有单个存储库失败时,请再次对它们进行编译。

我不确定为什么原因Xcode的初始获取成功及其后续获取失败 - Xcode也不会:“未知错误”。弄清楚原因是什么。我的机器上的某些设置似乎是错误的,因为我的同事没有这个问题。不幸的是,我无法弄清楚。

因此,我最终构建了这个解决方法,这只是做什么,Xcode不做的事情:取回库,通常会失败。它通过进入 deriveddata 文件夹并获取全部或指定的存储库来做到这一点。然后,Xcode将能够解析包装。

我将此功能放入我的 .profile (对不起,我仍在使用bash)。

function updateXcodeSPM() {
    local current_dir="$PWD"
    local target_dir="$HOME/Library/Developer/Xcode/DerivedData"
    local specific_libraries=("$@")

    find "$target_dir" -type d -name "SourcePackages" | while read spm_dir; do
        if [[ ! "$spm_dir" =~ \.noindex ]]; then
            find "$spm_dir/repositories" -type d -mindepth 1 -maxdepth 1 | while read repo_dir; do
                if [ ${#specific_libraries[@]} -eq 0 ]; then
                    # No specific libraries provided, update all
                    echo "Fetching in $repo_dir"
                    (cd "$repo_dir" && git fetch) &
                    sleep 0.1
                else
                    # Update only specific libraries
                    for lib in "${specific_libraries[@]}"; do
                        if [[ "$repo_dir" == *"repositories/$lib-"* ]]; then
                            echo "Fetching in $repo_dir"
                            (cd "$repo_dir" && git fetch) &
                            sleep 0.1
                        fi
                    done
                fi
            done
        fi
    done

    cd "$current_dir"
}

您可以在没有参数的情况下调用此函数,也可以使用未下载的软件包的名称来调用此功能:

> updateXcodeSPM FailingPackage1 FailingPackage2

或者

> updateXcodeSPM

我建议列出失败的软件包,因为它更快。

希望,它对您有帮助。

Multiple functioning workarounds can be found above.
I used this approach:

  • Quitting Xcode
  • Deleting ~/Library/Caches/org.swift.swiftpm
  • Deleting ~/Library/Developer/Xcode/DerivedData/YourProjectName
  • Launching Xcode

Unfortunately, re-downloading the packages could take a very long time - as well as compiling them all again, when only a single repo fails.

I am not sure why reason Xcode's initial fetch succeeds and its subsequent fetches fail - neither does Xcode: "unknown error". It would be good to figure out, what the cause is. Some setup on my machine seems to be wrong, as my colleagues don't have this problem. Unfortunately, I could not figure it out.

So I ended up building this workaround, that just does what, that Xcode does not do: fetching the repos, that usually fail. It does so by going into the DerivedData folder and fetching either all or the specified repos. Xcode will then be able to resolve the packages.

I put this function into my .profile (sorry, I am still using bash).

function updateXcodeSPM() {
    local current_dir="$PWD"
    local target_dir="$HOME/Library/Developer/Xcode/DerivedData"
    local specific_libraries=("$@")

    find "$target_dir" -type d -name "SourcePackages" | while read spm_dir; do
        if [[ ! "$spm_dir" =~ \.noindex ]]; then
            find "$spm_dir/repositories" -type d -mindepth 1 -maxdepth 1 | while read repo_dir; do
                if [ ${#specific_libraries[@]} -eq 0 ]; then
                    # No specific libraries provided, update all
                    echo "Fetching in $repo_dir"
                    (cd "$repo_dir" && git fetch) &
                    sleep 0.1
                else
                    # Update only specific libraries
                    for lib in "${specific_libraries[@]}"; do
                        if [[ "$repo_dir" == *"repositories/$lib-"* ]]; then
                            echo "Fetching in $repo_dir"
                            (cd "$repo_dir" && git fetch) &
                            sleep 0.1
                        fi
                    done
                fi
            done
        fi
    done

    cd "$current_dir"
}

You can call this function without parameters or with the names of the packages failing to download:

> updateXcodeSPM FailingPackage1 FailingPackage2

or

> updateXcodeSPM

I recommend listing the failing package(s) because it is quicker.

Hope, it helps you.

森罗 2025-02-15 05:56:35

我制作了一个Python脚本来解决这个问题。
这个想法是获取最新的提交哈希并将其写入包装。分辨,然后为您解决packaggations。

您可以在下面获取脚本,也可以从“ noreflowl noreferrer”>中

# Sometimes Xcode cannot resolve SPM(File -> Packages -> Resolve Package versions) if the dependency url is ssh
# This script is a workaround to resolve package versions.
# Usage:
# python spmResolve.py
# or
# python3 spmResolve.py
import os.path
import subprocess
import glob
import json

def main():
package_file = "xcshareddata/swiftpm/Package.resolved"
xcodeproj = glob.glob('*.xcodeproj')
xcworkspace = glob.glob('*.xcworkspace')
spmproj = glob.glob('Package.resolved')
package_resolved = ""
if xcodeproj:
package_resolved = xcodeproj[0] + f"/project.xcworkspace/{package_file}"
elif xcworkspace:
package_resolved = xcworkspace[0] + f"/{package_file}"
elif spmproj:
package_resolved = spmproj[0]
else:
print(f"

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

# Sometimes Xcode cannot resolve SPM(File -> Packages -> Resolve Package versions) if the dependency url is ssh
# This script is a workaround to resolve package versions.
# Usage:
#     python spmResolve.py
# or
#     python3 spmResolve.py
import os.path
import subprocess
import glob
import json


def main():
    package_file = "xcshareddata/swiftpm/Package.resolved"
    xcodeproj = glob.glob('*.xcodeproj')
    xcworkspace = glob.glob('*.xcworkspace')
    spmproj = glob.glob('Package.resolved')
    package_resolved = ""
    if xcodeproj:
        package_resolved = xcodeproj[0] + f"/project.xcworkspace/{package_file}"
    elif xcworkspace:
        package_resolved = xcworkspace[0] + f"/{package_file}"
    elif spmproj:
        package_resolved = spmproj[0]
    else:
        print(f"???? Cannot find *.xcodeproj, *.xcworkspace or Package.resolved file")
        exit(-1)

    update_package_resolved(package_resolved)


def update_package_resolved(package_resolved):
    if not os.path.exists(package_resolved):
        print(f"???? Package.resolved file doesn't exit: {package_resolved}")
        exit(-1)

    print(f"Found: {package_resolved}")
    f = open(package_resolved)
    content = json.load(f)
    f.close()
    for pin in content["pins"]:
        url = pin["location"]
        if "branch" in pin["state"]:
            branch = pin["state"]["branch"]
            commit_hash = get_git_revision_hash(url, branch)
            print(f"{url}, {branch}, {commit_hash}")
            pin["state"]["revision"] = commit_hash
        elif "version" in pin["state"]:
            version = pin["state"]["version"]
            commit_hash = get_git_revision_by_tag(url, version)
            print(f"{url}, {version}, {commit_hash}")
            pin["state"]["revision"] = commit_hash

    with open(package_resolved, "w") as output:
        json.dump(content,  output, indent=4)

    # resolve SPM
    subprocess.run(['xcodebuild', '-resolvePackageDependencies'])
    print('???? Well done')


def get_git_revision_hash(url, branch) -> str:
    command = f'git ls-remote {url} refs/heads/{branch} | cut -f 1'
    return get_git_command_output(command)


def get_git_revision_by_tag(url, version) -> str:
    command = f'git ls-remote {url} -t {version} | cut -f 1'
    return get_git_command_output(command)


def get_git_command_output(command) -> str:
    return subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True).decode('ascii').rstrip()


if __name__ == '__main__':
    main()


Put the script in your xcode project, along with your *.xcodeproj or *.xcworkspace.
Then run

python spmResolve.py

or

python3 spmResolve.py

If you don't have python:

brew install python
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文