[英]How to import a private Go library (as module) within another private Go project (as module)
我正在将一些私有 Go 项目移至 GitLab,同时摆脱Godeps
、带有vendor
目录的 Go dep
以及所有这些,因为我只想使用 Go 模块。
我使用的是 Go 版本: go1.12.6 linux/amd64
。
gitlab.com/my-company/my-team/my-library
有一个私人“图书馆”项目/git 存储库。 这与go.mod
和go.sum
一起用作 Go 模块。my-library
作为另一个项目的依赖项,这个: gitlab.com/my-company/my-team/my-project
。URL 的结构是相同的,唯一改变的是存储库的名称。
在my-project
导入my-library
时遇到各种错误。
我知道 GitLab 令牌有效,因为go get
命令自己计算出my-library
的提交 ID。 无论如何,我已经完成了以下操作(参见https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html ):
git config \
--global \
url."https://token_name:token_val@gitlab.com".insteadOf \
"https://gitlab.com"
这些是错误消息:
$ go get -u gitlab.com/my-company/my-team/my-library.git
go: finding gitlab.com/my-company/my-team/my-library.git latest
go: gitlab.com/my-company/my-team/my-library.git@v0.0.0-20190802120216-712a10fb5fac: parsing go.mod: unexpected module path "gitlab.com/my-company/my-team/my-library"
go get: error loading module requirements
$
$ go get -u gitlab.com/my-company/my-team/my-library
go get gitlab.com/my-company/my-team/my-library: git ls-remote -q https://gitlab.com/my-company/my-team.git in /home/foo/Development/go-workspace/pkg/mod/cache/vcs/9be637426eac43b329899d57d9375d12246f2cc0f6ddd098446bc42ed1ca534d: exit status 128:
remote: The project you were looking for could not be found.
fatal: repository 'https://token_name:token_val@gitlab.com/my-company/my-team.git/' not found
$
$ GO111MODULE=off go get -u gitlab.com/my-company/my-team/my-library.git
package gitlab.com/my-company/my-team/my-library.git: no Go files in /home/foo/Development/go-workspace/src/gitlab.com/my-company/my-team/my-library.git
$
go mod graph | grep gitlab
go mod graph | grep gitlab
我发现一个空的输出,即没有冲突的依赖。.git
结尾,并且(出于某种我不明白的原因)URL 在my-team
级别被剪切。GO111MODULE=off
似乎强制go get
检查$GOPATH
我认为我应该完全避免,但我只是想看看我是否能够获取该依赖项。我绝对建议尝试使用Go 1.13 beta:
$ go get golang.org/dl/go1.13beta1
$ go1.13beta1 download
$ go1.13beta1 get foo
甚至更好,请尝试使用最新的Go on tip / master(鉴于Go 1.13 beta1版本在此之前已经存在了一个多月):
$ go get golang.org/dl/gotip
$ gotip download
$ gotip get foo
Go 1.13改进了使用私有存储库的几个方面(包括CL 170879和其他改进),并且与Go 1.12相比,它通常具有更好的错误消息。
对于您的第一个错误消息:
$ go get -u gitlab.com/my-company/my-team/my-library.git
go: finding gitlab.com/my-company/my-team/my-library.git latest
go: gitlab.com/my-company/my-team/my-library.git@v0.0.0-20190802120216-712a10fb5fac:
parsing go.mod: unexpected module path "gitlab.com/my-company/my-team/my-library"
go get: error loading module requirements
这是go
命令,抱怨模块的导入/ go.mod
方式与其在go.mod
的module
行中声明自己的身份之间的不匹配。 如果模块foo
被导入模块bar
,然后foo
需要参考bar
,同样的方式bar
声明了它的身份module
的线bar
的go.mod
文件。
换句话说,用于导入模块(或go get
模块)的导入路径需要从在导入模块go.mod
的module
行上声明的确切模块路径开始。 您可能需要更改导入程序以匹配go.mod
文件中module
行上go.mod
格式,或者您可能需要更改go.mod
的module
行以匹配导入程序所使用的格式,但是它们可以不同意。 如果他们不同意,您将得到第一个报告的错误。
通常,您可以选择如何将私有存储库与Go模块一起使用。 这两篇博客文章概述了几个问题,并介绍了一些方法,如果您还没有阅读它们,那么非常值得一读:
最后,如果仍然不清楚发生了什么,您可能应该尝试go get -v foo
或go get -v -x foo
:
go get
-v
标志要求打印更多详细信息,包括HTTPS请求 ,但请注意,根据远程存储库的配置,可能会期望某些“错误”(例如404错误)。
如果问题的性质仍然不清楚,您也可以尝试使用更详细的命令go get -v -x foo
,这也显示了git或其他VCS命令正在发出。 如果有必要,您通常可以在go
工具的上下文之外执行相同的git命令,以进行故障排除。
export GOPROXY=http://nexus.my-company.com/nexus/repository/goproxy/,direct
export GONOPROXY=gitlab.my-company.com
export GOSUMDB=off
export GO111MODULE=on
即使“gitlab.my-company.com”不支持https,也可以这样配置:
git config --global url."git@gitlab.my-company.com:".insteadOf "https://gitlab.my-company.com/"
require (
gitlab.my-company.com/my-team/my-library v0.0.0-00010101000000-000000000000
)
replace (
#use "go get -insecure gitlab.my-company.com/my-team/my-library" to get latest commit hash
gitlab.my-company.com/my-team/my-library => git.midea.com/my-team/my-library.git v0.0.0-XXXXXXXXXXXXXX-YYYYYYYYYYYY
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.