【问题标题】:How to import a private Go library (as module) within another private Go project (as module)如何在另一个私有 Go 项目(作为模块)中导入私有 Go 库(作为模块)
【发布时间】:2019-10-11 17:45:23
【问题描述】:

我正在将一些私有 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 时,我发现一个空输出,即没有冲突的依赖关系。
  • 可能要避免第二次尝试,因为 URL 不以 .git 结尾,并且(出于某种我不明白的原因)URL 被剪切到 my-team 的级别。
  • GO111MODULE=off 似乎迫使 go get 签入 $GOPATH,我认为我应该避免这样做,但我只是想看看我是否能够以这种方式获取该依赖项。

【问题讨论】:

    标签: git go gitlab go-modules


    【解决方案1】:

    我绝对建议尝试使用 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.modmodule 行中声明自己的身份之间的不匹配。如果模块foo 正在导入模块bar,则foo 需要引用bar,就像barbarbar 文件的module 行上声明其身份一样.

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

    一般来说,关于如何将私有存储库与 Go 模块一起使用,您有几种选择。这两篇博文概述了几个问题并涵盖了一些方法,如果您还没有阅读它们,那么值得一读:

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

    • -v 标志到go get 要求打印更详细的详细信息,包括HTTPS requests,但请注意,根据远程存储库的运行情况,可能会出现某些“错误”,例如 404 错误已配置。

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

    【讨论】:

    • go.mod 文件与我尝试导入的文件的差异是我的问题!谷歌搜索太多,无法用那个金块找到这个答案......
    【解决方案2】:
    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
    )
    

    【讨论】:

      猜你喜欢
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-01
      相关资源
      最近更新 更多