Go mod tidy在私有仓库上失败,但git clone可以正常工作。

huangapple go评论84阅读模式
英文:

Go mod tidy fails on private repository when git clone works

问题

我将我的SSH密钥放在GitHub上,以便git clone可以用于某个私有仓库;然而,当尝试访问同一仓库时,go mod tidy失败,并显示下面的错误消息。我需要帮助解决这个问题。go mod tidy的错误消息如下(隐藏了私有仓库的名称,其他内容完全一样):

github.com/[私有仓库路径]:无法找到提供包github.com/[私有仓库路径]的模块:模块github.com/[私有仓库路径]:在/mnt/dependencies/gopath/pkg/mod/cache/vcs/a1f499df6a9855aecdc77bda31504008583a3268fdd403799aade71bb47df7d2中的git ls-remote -q origin失败:退出状态128:
fatal: 'origin'不似乎是一个git仓库
fatal: 无法从远程仓库读取

请确保您具有正确的访问权限并且仓库存在。

成功的git clone命令(同样隐藏了私有仓库)是:

git clone git@github.com:[私有仓库路径].git

我的.gitconfig文件如下(隐藏了我的姓名和电子邮件):

[user]
    name = [我的姓名]
    email = [我的电子邮件]
insteadOf = https://github.com/ [core] excludesfile = /workdir/.gitignore

其他可能有影响的因素:

  • 我在Docker容器中以root身份运行命令。
  • 我尝试在主机上使用与Docker容器中的密钥相同的~/.ssh中的密钥运行go mod tidy,并且我得到了登录提示,但GitHub不再支持此方式(他们要求使用SSH密钥或个人访问令牌)。
  • 我的gitconfig中的电子邮件与我放在github.com上的公钥中给出的电子邮件匹配。

第二个相关问题:我能够运行git clone是否排除了密钥错误上传到GitHub并忘记在GitHub上启用SSO的可能性?

这是一个类似的问题,但涉及个人访问令牌而不是SSH密钥:https://stackoverflow.com/questions/71851732/go-mod-tidy-fails-to-download-private-github-repository

英文:

I put my ssh key in github so git clone works for a certain private repository; yet go mod tidy fails when trying to access the same repository, with the error message shown below. I could use help troubleshooting this problem. The error message from go mod tidy (redacting the name of the private repository, but otherwise verbatim) is:

    github.com/[private repository path]: cannot find module providing package github.com/[private repository path]: module github.com/[private repository path]: git ls-remote -q origin in /mnt/dependencies/gopath/pkg/mod/cache/vcs/a1f499df6a9855aecdc77bda31504008583a3268fdd403799aade71bb47df7d2: exit status 128:
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository

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

The successful git clone command (again redacting the private repository) is:

git clone git@github.com:[private repository path].git

My .gitconfig file is (with my name and email redacted):

[user]
    name = [my name]
    email = [my email]
insteadOf = https://github.com/ [core] excludesfile = /workdir/.gitignore

Other things that might make a difference:

  • I am running commands as root in a Docker container.
  • I tried to run go mod tidy on my host with the same key in ~/.ssh as I have on the Docker container, and I got login prompts, which github no longer supports (they want SSH keys or personal access tokens).
  • The email in my gitconfig matches email given in the public key that I put on github.com.

A second, related question: Does the fact that I can run git clone rule out keys being uploaded wrong to github, and forgetting to enable SSO for the key on github?

Here is a similar question but it involves personal access tokens rather than ssh keys: https://stackoverflow.com/questions/71851732/go-mod-tidy-fails-to-download-private-github-repository

答案1

得分: 4

我正在以root用户在Docker容器中运行命令。

请确保它访问的是与您常规帐户使用的密钥(在/root/.ssh中)相同的密钥。

并检查Git全局配置设置是否实际包含您的insteadOf指令。

我尝试使用与Docker容器中相同的密钥(在~/.ssh中)在主机上运行go mod tidy,但是我收到了登录提示。

这意味着公钥没有正确注册到GitHub用户配置文件中,或者该用户没有访问该存储库的权限。

英文:

> I am running commands as root in a Docker container.

Make sure then it does access the same keys (in /root/.ssh) as the ones you are using with your regular account.

And check if the Git global config settings actually include your insteadOf directive.

> tried to run go mod tidy on my host with the same key in ~/.ssh as I have on the Docker container, and I got login prompts.

That means the public key is not properly registered to the GitHub user profile, or said user does not have the right to access the repository.

答案2

得分: 3

我找到了解决我的问题的答案,但事实证明我在问题中忽略了一个事实。

直到第二天早上我才意识到我的Docker镜像中的golang版本比用于填充$GOPATH的版本要高(或者至少我没有意识到这一点的重要性)。为了解决这个问题,我删除了$GOPATH下的所有内容,并重新运行go mod tidy,没有出现任何问题。

VonC的回答中的清单对于问题的陈述非常有用和切题,所以我接受了这个答案。但我会为任何遇到这个帖子的人添加一些内容:

“用于go mod tidy的golang版本与填充$GOPATH的版本是否不同?”

英文:

I found the answer to my problem, but it turns on a fact that I omitted in the question.

I didn't realize until the next morning that my Docker image had a higher version of golang than was used to populate $GOPATH (or at least I didn't realize how important this was). To fix this, I deleted everything under $GOPATH and re-ran go mod tidy with no issues.

The checklist in VonC's answer is useful and more to the point of the question as stated, so I accepted it. But I'll add to it for anyone that comes across this post:

"Is the version of golang used for go mod tidy different from what populated $GOPATH?"

huangapple
  • 本文由 发表于 2022年6月11日 13:38:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/72581998.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定