繁体   English   中英

如何在另一个私有 Go 项目(作为模块)中导入私有 Go 库(作为模块)

[英]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.modgo.sum一起用作 Go 模块。
  • 我想使用my-library作为另一个项目的依赖项,这个: gitlab.com/my-company/my-team/my-project

URL 的结构是相同的,唯一改变的是存储库的名称。

my-project导入my-library时遇到各种错误。

  • 如何将 Go 模块作为私有存储库导入其他 Go 模块作为私有存储库?
  • 下面的命令输出有什么问题?

我知道 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我发现一个空的输出,即没有冲突的依赖。
  • 可能要避免第二次尝试,因为 URL 不以.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.modmodule行中声明自己的身份之间的不匹配。 如果模块foo被导入模块bar ,然后foo需要参考bar ,同样的方式bar声明了它的身份module的线bargo.mod文件。

换句话说,用于导入模块(或go get模块)的导入路径需要从在导入模块go.modmodule行上声明的确切模块路径开始。 您可能需要更改导入程序以匹配go.mod文件中module行上go.mod格式,或者您可能需要更改go.modmodule行以匹配导入程序所使用的格式,但是它们可以不同意。 如果他们不同意,您将得到第一个报告的错误。

通常,您可以选择如何将私有存储库与Go模块一起使用。 这两篇博客文章概述了几个问题,并介绍了一些方法,如果您还没有阅读它们,那么非常值得一读:

最后,如果仍然不清楚发生了什么,您可能应该尝试go get -v foogo get -v -x foo

  • go get -v标志要求打印更多详细信息,包括HTTPS请求 ,但请注意,根据远程存储库的配置,可能会期望某些“错误”(例如404错误)。

  • 如果问题的性质仍然不清楚,您也可以尝试使用更详细的命令go get -v -x foo ,这也显示了git或其他VCS命令正在发出。 如果有必要,您通常可以在go工具的上下文之外执行相同的git命令,以进行故障排除。

  1. 如果楼宇服务器无法访问互联网,我们需要一个私有的“nexus3 goproxy”。
export GOPROXY=http://nexus.my-company.com/nexus/repository/goproxy/,direct
export GONOPROXY=gitlab.my-company.com
export GOSUMDB=off
export GO111MODULE=on
  1. 禁止尝试通过 http(s) 协议下载

即使“gitlab.my-company.com”不支持https,也可以这样配置:

git config --global url."git@gitlab.my-company.com:".insteadOf "https://gitlab.my-company.com/"
  1. 由于gitlab的缺陷,我们需要替换go.mod config中的模块下载路径
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.

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