Golang中`go get`获取私有模块时遇到的问题

Golang中go get获取私有模块时遇到的问题 在导入私有模块时遇到问题,列出了调试和详细日志,但无法找出原因…:

$ go get -x -v private-gitlab.com/orgname/subgroup/myproject-go
# get https://private-gitlab.com/orgname/subgroup/myproject-go?go-get=1
# get https://private-gitlab.com/orgname/subgroup/myproject-go?go-get=1: 200 OK (0.095s)
get "private-gitlab.com/orgname/subgroup/myproject-go": found meta tag vcs.metaImport{Prefix:"private-gitlab.com/orgname/subgroup", VCS:"git", RepoRoot:"https://private-gitlab.com/orgname/subgroup.git"} at //private-gitlab.com/orgname/subgroup/myproject-go?go-get=1
get "private-gitlab.com/orgname/subgroup/myproject-go": verifying non-authoritative meta tag
# get https://private-gitlab.com/orgname/subgroup?go-get=1
# get https://private-gitlab.com/orgname/subgroup?go-get=1: 200 OK (0.038s)
mkdir -p /home/username/go/pkg/mod/cache/vcs # git3 https://private-gitlab.com/orgname/subgroup.git
# lock /home/username/go/pkg/mod/cache/vcs/5992a5e5b7a53bbdd91113a9673fe0b4d486cf6d2db09a1dc466f05595aeb817.lock
# /home/username/go/pkg/mod/cache/vcs/5992a5e5b7a53bbdd91113a9673fe0b4d486cf6d2db09a1dc466f05595aeb817 for git3 https://private-gitlab.com/orgname/subgroup.git
cd /home/username/go/pkg/mod/cache/vcs/5992a5e5b7a53bbdd91113a9673fe0b4d486cf6d2db09a1dc466f05595aeb817; git ls-remote -q origin
1.919s # cd /home/username/go/pkg/mod/cache/vcs/5992a5e5b7a53bbdd91113a9673fe0b4d486cf6d2db09a1dc466f05595aeb817; git ls-remote -q origin
# get https://private-gitlab.com/orgname/subgroup.git
# get https://private-gitlab.com/orgname/subgroup.git: 200 OK (0.094s)
go: module private-gitlab.com/orgname/subgroup/myproject-go: git ls-remote -q origin in /home/username/go/pkg/mod/cache/vcs/5992a5e5b7a53bbdd91113a9673fe0b4d486cf6d2db09a1dc466f05595aeb817: exit status 128:
        remote: 
        remote: ========================================================================
        remote: 
        remote: The project you were looking for could not be found or you don't have permission to view it.
        remote: 
        remote: ========================================================================
        remote: 
        fatal: Could not read from remote repository.

        Please make sure you have the correct access rights
        and the repository exists.

以下两种方式都能正常工作:

git clone git@private-gitlab.com:orgname/subgroup/myproject-go.git
git clone https://private-gitlab.com/orgname/subgroup/myproject-go.git

我已经在全局的 .gitconfig 中启用了以下配置:

[url "git@private-gitlab.com:"]
	insteadOf = https://private-gitlab.com/

我没有设置 .netrc,因为我更希望它能通过 SSH 工作…


更多关于Golang中`go get`获取私有模块时遇到的问题的实战教程也可以访问 https://www.itying.com/category-94-b0.html

2 回复

我认为我刚刚发现这个问题可能出在GitLab那边……

编辑:在该问题解决之前,使用 .netrc 是解决方案,正如此处Github Gist的这部分所解释的

我自己通过将 ssh 设置替换为 .netrc 解决了这个问题。

更多关于Golang中`go get`获取私有模块时遇到的问题的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


从日志看,go get 尝试使用 HTTPS 协议访问 GitLab,但 GitLab 返回了权限错误。虽然你配置了 insteadOf 规则,但 Go 模块系统在元数据发现阶段可能没有应用这个配置。

问题在于 Go 工具链在解析模块路径时,从 ?go-get=1 的响应中获取的 RepoRoot 是 HTTPS URL(https://private-gitlab.com/orgname/subgroup.git),而不是 SSH URL。

需要配置 Go 使用 SSH 进行私有仓库的访问。在 ~/.gitconfig 中添加:

[url "git@private-gitlab.com:"]
    insteadOf = https://private-gitlab.com/

同时设置 GOPRIVATE 环境变量:

export GOPRIVATE=private-gitlab.com

如果问题仍然存在,检查 Git 配置是否生效:

git config --global --get-urlmatch.url.https://private-gitlab.com/.insteadof

另一种方法是使用 .netrc 文件进行认证,但既然你希望使用 SSH,可以强制 Go 使用 SSH 协议。在 go.mod 中使用 replace 指令:

module yourproject

go 1.21

replace private-gitlab.com/orgname/subgroup/myproject-go => git@private-gitlab.com:orgname/subgroup/myproject-go.git v0.0.0

require private-gitlab.com/orgname/subgroup/myproject-go v0.0.0

然后运行:

GOPRIVATE=private-gitlab.com go mod tidy

如果还是不行,可能是 Git 版本或 SSH 配置问题。检查 SSH 连接:

ssh -T git@private-gitlab.com

确保 SSH 密钥已添加到 GitLab 账户。

回到顶部