简体   繁体   English

如何使用 Go Mod 下载与未发布的伪版本

[英]How to Use Go Mod Download with Unreleased Pseudo-Versions

Since golang 1.13, I've been having this issue where our docker build scripts, which fetch our dependencies using go mod download , fail during a git fetch if the given dependency is an unreleased pseudo-version that is on a branch.自 golang 1.13 以来,我一直遇到这个问题,我们的 docker 构建脚本,它使用go mod download获取我们的依赖项,在 ZBA9F11ECC3497D9993B933FDC2BD69A6BZ 中失败I'm currently using golang 1.15.我目前正在使用 golang 1.15。

I'm trying to integration test some changes to the referenced go package.我正在尝试集成测试对引用的 go package 的一些更改。 I'll call the package example-go-sdk .我将调用 package example-go-sdk In the git repo for example-go-sdk , I've created a branch and pushed some changes.example-go-sdk的 git 存储库中,我创建了一个分支并推送了一些更改。 Then in the golang project that uses example-go-sdk , I've executed:然后在使用example-go-sdk的 golang 项目中,我执行了:

go get local.url.that.utilizes.goprivate/path/example-go-sdk@824eebca783c68c5ef3cf6db35ad688ad30b58b0

where that hash is the hash of the head commit on my branch, which I've double-checked is correct and does have my changes.其中 hash 是我的分支上的头部提交的 hash,我已经仔细检查过它是正确的并且确实有我的更改。 The project compiles locally with this just fine.该项目在本地编译就好了。 Even the go mod download works for me.甚至 go mod 下载也适合我。

However, when doing the go mod download from a golang:1.15-alpine docker container (after ensuring GOPRIVATE is set correctly), I get this error:但是,当从 golang:1.15-alpine docker 容器go mod download时(确保正确设置 GOPRIVATE 后),我收到此错误:

go: local.url.that.utilizes.goprivate/path/example-go-sdk@v1.1.2-0.20210329043657-824eebca783c: invalid pseudo-version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /go/pkg/mod/cache/vcs/369a4408a70eeb73ab52db67cb0c1ba8eb165df174d65c9ec995526a9545424b: exit status 128:
    fatal: unresolved deltas left after unpacking
    fatal: unpack-objects failed

Is there a way to fix this?有没有办法来解决这个问题? Due to some other requirements, I can't merge this branch in example-go-sdk to master first before trying to reference it.由于其他一些要求,在尝试引用它之前,我无法先将example-go-sdk中的这个分支合并到 master 中。 I've made sure the docker build is using the right version of the referencing project with the updated go.mod and go.sum , too.我已经确保 docker 构建使用正确版本的引用项目以及更新的go.modgo.sum

Even though this all works locally for me, a couple other team members also have build issues when referencing the pseudo-version, too, so I'm not sure what the deal is.尽管这一切对我来说都在本地工作,但其他几个团队成员在引用伪版本时也遇到了构建问题,所以我不确定这是什么问题。

The go command does and will continue to support pseudo-versions for commits for the foreseeable future. go命令在可预见的将来会继续支持提交的伪版本。 This looks like either data corruption within your repo, or a bug in your git server and/or client.这看起来像是您的存储库中的数据损坏,或者您的git服务器和/或客户端中的错误。

Since the failing command is the git fetch that attempts to resolve the existing branches and tags in the repo, my guess would be that the issue affects some non-default branch or some existing tag in the repo.由于失败的命令是git fetch ,它试图解析 repo 中的现有分支和标签,我猜这个问题会影响 repo 中的一些非默认分支或一些现有标签。 That would explain why your pre-release tag workaround works: if the go command can find a specific matching tag, then it doesn't need to fall back to fetching “all branches and tags”, and presumably doesn't trigger the underlying git issue.这可以解释为什么您的预发布标签解决方法有效:如果go命令可以找到特定的匹配标签,那么它不需要回退到获取“所有分支和标签”,并且可能不会触发底层git问题。

You could perhaps start to narrow it down by trying the failing git command in a fresh clone of your repo:您也许可以通过在您的 repo 的新克隆中尝试失败的git命令来开始缩小范围:

git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/*

Note that you can pass the -x flag to the go command to cause it to (very verbosely!) print all of the git commands it executes.请注意,您可以将-x标志传递给go命令,以使其(非常冗长!)打印它执行的所有git命令。 That may help you reproduce an exact repository configuration that triggers the underlying bug.这可能会帮助您重现触发底层错误的确切存储库配置。

I was not able to make this work via pseudo-versions.我无法通过伪版本完成这项工作。 It seems like since golang 1.13 this functionality of using pseudo-versions instead of semantic versions no longer works unless your module still has a major version of v0.从 golang 1.13 开始,这种使用伪版本而不是语义版本的功能似乎不再起作用,除非您的模块仍然具有 v0 的主要版本。 It may or may not be related to the use of GOPRIVATE -- I haven't verified that.它可能与 GOPRIVATE 的使用有关,也可能无关——我还没有证实这一点。

However, a workaround is to use pre-release versions as described in the official golang blog .但是,一种解决方法是使用官方golang 博客中描述的预发布版本 Basically adding a new semantic version but appending a string to the end after a dash: v2.2.2-example.pre.release基本上添加一个新的语义版本,但在破折号后附加一个字符串: v2.2.2-example.pre.release

By doing this, go mod download was able to find the tag and install the version.通过这样做, go mod download能够找到标签并安装版本。 Additionally, pre-release tags are not assumed backwards compatible, so users will not automatically get upgraded to that version.此外,预发布标签不假定向后兼容,因此用户不会自动升级到该版本。 They need to explicitly request the pre-release tag.他们需要明确请求预发布标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM