go get/ go mod tidy 问题汇总

fatal: could not read Username for ‘https://github.com‘: terminal prompts disabled


https://h1z3y3.me/posts/go-private-git-repository/

https://go.dev/doc/faq#git_https

解决办法之一: 如果用的是github.com, 可以编辑~/.gitconfig,新增:

1
2
[url "ssh://git@github.com/"]
insteadOf = https://github.com/

如果用的是gitlab,则:

1
2
[url "ssh://git@your.gitlab.com/"]
insteadOf = https://your.gitlab.com/

20240920补充:

在一台新机器对于taskon-download项目进行go mod tidy

1
2
3
4
5
6
go.uber.org/goleak: github.com/Taskon-xyz/taskon-server@v1.74.15-test.0.20240919095556-73cbedcea4a1: verifying go.mod: github.com/Taskon-xyz/taskon-server@v1.74.15-test.0.20240919095556-73cbedcea4a1/go.mod: reading https://sum.golang.org/lookup/github.com/!taskon-xyz/taskon-server@v1.74.15-test.0.20240919095556-73cbedcea4a1: 404 Not Found
server response:
not found: github.com/Taskon-xyz/taskon-server@v1.74.15-test.0.20240919095556-73cbedcea4a1: invalid version: git ls-remote -q origin in /tmp/gopath/pkg/mod/cache/vcs/60d9bb6aad3eb79e70dd9506cfc576f9877aa3a1545694236822feaab2aa7df1: exit status 128:
fatal: could not read Username for 'https://github.com': terminal prompts disabled
Confirm the import path was entered correctly.
If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.

https://go.dev/doc/faq#git_https

如果没有指定当前git用户的身份信息,

1
2
3
cat ~/.gitconfig
[url "ssh://git@github.com/"]
insteadOf = https://github.com/

上面这样操作是不行的~

不如直接用osoffice02上的配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[alias]
ls = status -sb
cm = commit
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --
co = checkout
[core]
ediotr = vim
autocrlf = input
fileMode = false
editor = vim -c 'startinsert'
#pager = delta --theme OneHalfLight
[user]
name = rongyi
email = rongyi@onchain.com
[color]
ui = true
[branch]
autosetuprebase = always
[pull]
rebase = true
[push]
default = upstream
[diff]
guitool = meld
tool = meld
[difftool]
prompt = false
[credential]
helper = cache --timeout=54000
[interactive]
#diffFilter = delta

[url "https://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:x-oauth-basic@github.com/"]
insteadOf = https://github.com/
[url "https://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:x-oauth-basic@github.com/"]
insteadOf = https://github.com/
[safe]
directory = /data/go/src/tools/
directory = /data/go/src/tools
directory = /root/.config/nvim

也不行..然后改回最初的办法,

1
2
[url "ssh://git@github.com/"]
insteadOf = https://github.com/

再换个schedule项目,go mod tidy就行了…应该download有缓存

!!!记得多执行 go clean -modcache 清除mod的缓存!!!

!!! GOPRIVATE=github.com/Taskon-xyz/taskon-server go mod tidy !!! 这样试试!

最后也换成了这样:

1
2
[url "https://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:x-oauth-basic@github.com/"]
insteadOf = https://github.com/

最后确实证明了,和 GOPRIVATE=github.com/Taskon-xyz/taskon-server 有关系!!!

出问题时记得执行这个~

(我写到了go env里面)

20250523 cuishuang 记录:
其实改 .gitconfig就行了,用有这个仓库读权限的github搞一个token,贴到上面,参考上面~

(1. 一个是改.ssh里面的公钥私钥, 然后应用)

(2. 改.gitconfig)



20250513 maobo 让用taskoncicd.

导致原来wing-test-cluster-01上面,jenkins就拉取不到啥依赖了…

需要改.gitconfig 里面的token

但改了以后,diucicd的依赖是不是就会有问题…. —> 是的

我有两个分开的项目diu和taskon, 其中github账号diucicd有diu组织下的全部仓库的写权限,taskoncicd有taskon组织下的全部仓库的写权限. 现在两个项目共用一个jenkins,本身clone是可以的,但是因为是go项目,在go moy tidy 拉取依赖时,如果当前.gitconfig里设置的是diucicd的信息,如[url “https://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:x-oauth-basic@github.com/"]
insteadOf = https://github.com/, 那么takson的项目在go mod tidy时就会报128的错误.如何解决?


其实发现佩文的oscore-server的这些服务没这些问题,他不依赖taskon-server,他的服务之间也只有api层面的调用依赖,没有golang package层面的调用/依赖关系, 所以其实现在就没问题了…

即 现在 wingcluster-test-01这台机器root账号根目录下, .gitconfig 里面配置的是taskoncicd的账号信息,

.gitconfig.old 是之前diucicd的信息

.gitconfig

1
2
3
4
5
6
7
8
9
[url "https://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:x-oauth-basic@github.com/"]
insteadOf = https://github.com/
[core]
editor = vim
[use]
name = rongyi
email = rongyi@ont.io
[url "https://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:x-oauth-basic@github.com/"]
insteadOf = https://github.com/

.gitconfig.old

1
2
3
4
5
6
7
8
9
10
[url "https://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:x-oauth-basic@github.com/"]
insteadOf = https://github.com/
[core]
editor = vim
[use]
name = rongyi
email = rongyi@ont.io
[url "https://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:x-oauth-basic@github.com/"]
insteadOf = https://github.com/

但如果佩文也依赖呢? 这个问题该如何解决…. 即上面准备问claude的问题:

下面是claude的回复:

您遇到的问题是关于在同一个Jenkins环境中处理两个不同GitHub组织的项目(diu和taskon),使用不同的访问凭证。当您在Go项目中运行go mod tidy时,由于全局.gitconfig只能配置一个GitHub凭证,导致访问另一个组织的代码库时出现权限错误。

这个问题可以通过几种方式解决:

1. 使用Jenkins凭证管理和环境变量

您可以在Jenkins构建任务中使用不同的凭证,并在运行前动态设置Git配置:

1
2
3
4
5
6
7
8
9
# 根据项目类型设置不同的凭证
if [[ "$JOB_NAME" == *"diu"* ]]; then
git config --global url."https://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:x-oauth-basic@github.com/".insteadOf https://github.com/
elif [[ "$JOB_NAME" == *"taskon"* ]]; then
git config --global url."https://ghp_TASKON_TOKEN_HERE:x-oauth-basic@github.com/".insteadOf https://github.com/
fi

# 执行Go模块操作
go mod tidy

2. 使用Git凭证存储

您可以配置.netrc文件来存储多个凭证:

1
2
3
4
5
6
# 在Jenkins作业开始时创建或修改.netrc文件
cat > ~/.netrc << EOF
machine github.com login diucicd password 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
machine github.com/taskon login taskoncicd password ghp_TASKON_TOKEN_HERE
EOF
chmod 600 ~/.netrc

3. 使用GOPRIVATE和替代配置

针对Go模块,您可以使用GOPRIVATE环境变量和对应的替代URL配置:

1
2
3
4
5
6
# 设置GOPRIVATE避免对私有模块使用代理
export GOPRIVATE=github.com/diu/*,github.com/taskon/*

# 为不同组织设置不同的替代URL
git config --global url."https://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:x-oauth-basic@github.com/diu/".insteadOf https://github.com/diu/
git config --global url."https://ghp_TASKON_TOKEN_HERE:x-oauth-basic@github.com/taskon/".insteadOf https://github.com/taskon/

4. 使用项目级别的Git配置

您可以为每个项目创建独立的Git配置:

1
2
3
4
5
# diu项目目录中
git config --local url."https://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:x-oauth-basic@github.com/".insteadOf https://github.com/

# taskon项目目录中
git config --local url."https://ghp_TASKON_TOKEN_HERE:x-oauth-basic@github.com/".insteadOf https://github.com/

建议最佳实践

综合考虑安全性和实用性,我建议使用Jenkins凭证管理结合项目级别的Git配置:

  1. 在Jenkins中存储两个不同的GitHub访问凭证(分别用于diu和taskon)
  2. 在构建脚本中,根据当前构建的项目类型,动态设置适当的Git配置
  3. 构建结束后清理Git配置,避免影响后续任务

这样可以确保每个项目在构建时都使用正确的凭证,并且不会互相干扰。


—分割线—

GOPRIVATE=github.com/Taskon-xyz/taskon-server 怎么设置go env

在 Go 中,GOPRIVATE 环境变量用于指定私有模块的域名或路径前缀,防止 Go 在拉取这些模块时使用公共代理或校验模块的校验和数据库(sum.golang.org)。这对于私有仓库模块的使用非常重要。

你可以通过以下几种方式设置 GOPRIVATE 环境变量。

1. 临时设置 GOPRIVATE

如果你只想在当前终端会话中临时设置 GOPRIVATE,可以通过以下命令直接在终端中设置:

1
go env -w GOPRIVATE=github.com/Taskon-xyz/taskon-server

这将使用 go env -w 命令永久写入 Go 的环境配置,适用于所有 Go 项目。你可以通过 go env 命令来验证它是否设置成功:

1
go env GOPRIVATE

输出应该会显示:

1
github.com/Taskon-xyz/taskon-server

2. 永久设置 GOPRIVATE(全局环境变量)

如果你希望永久设置 GOPRIVATE,使其在所有终端会话中生效,可以将其添加到你的 shell 配置文件中。

对于 Bash 用户(如 ~/.bashrc~/.bash_profile):

打开你的 ~/.bashrc~/.bash_profile 文件,添加以下行:

1
export GOPRIVATE=github.com/Taskon-xyz/taskon-server

然后,运行以下命令以使更改生效:

1
source ~/.bashrc

或者:

1
source ~/.bash_profile

对于 Zsh 用户(如 ~/.zshrc):

打开你的 ~/.zshrc 文件,添加以下行:

1
export GOPRIVATE=github.com/Taskon-xyz/taskon-server

然后,运行以下命令以使更改生效:

1
source ~/.zshrc

对于 Fish 用户(如 ~/.config/fish/config.fish):

打开你的 config.fish 文件,添加以下行:

1
set -x GOPRIVATE github.com/Taskon-xyz/taskon-server

然后,运行以下命令以使更改生效:

1
source ~/.config/fish/config.fish

3. 通过 Go 命令行设置

如果你不想手动编辑配置文件,你可以通过 go env -w 命令直接将 GOPRIVATE 设置为全局变量,并写入 Go 的环境配置:

1
go env -w GOPRIVATE=github.com/Taskon-xyz/taskon-server

这将会将 GOPRIVATE 设置为全局环境变量,不需要手动修改 shell 配置文件。

4. 查看当前 GOPRIVATE 设置

要查看当前的 GOPRIVATE 设置,你可以使用以下命令:

1
go env GOPRIVATE

如果正确设置了,会返回你指定的私有模块前缀,例如:

1
github.com/Taskon-xyz/taskon-server

5. 支持多个私有仓库

如果你有多个私有模块需要设置 GOPRIVATE,你可以用逗号(,)分隔它们:

1
go env -w GOPRIVATE=github.com/Taskon-xyz/taskon-server,gitlab.com/company/private-repo

或者通过编辑配置文件的方式:

1
export GOPRIVATE=github.com/Taskon-xyz/taskon-server,gitlab.com/company/private-repo

总结

  • 临时设置:你可以在当前终端会话中通过 go env -w 命令来设置 GOPRIVATE
  • 永久设置:如果希望在所有终端会话中生效,可以将 export GOPRIVATE=... 添加到你的 shell 配置文件中。
  • 查看设置:使用 go env GOPRIVATE 可以查看当前的 GOPRIVATE 设置。

这样你就成功配置了 Go 的 GOPRIVATE 环境变量,Go 会使用特定的策略处理私有仓库中的模块,不会通过公共代理或校验和数据库来访问这些模块。

—分割线—



ambiguous import: found package xxxxx in multiple modules


https://www.google.com/search?q=ambiguous+import%3A+found+package+xxxxx+in+multiple+modules&oq=ambiguous+import%3A+found+package+xxxxx+in+multiple+modules&aqs=chrome..69i57.4502j0j1&sourceid=chrome&ie=UTF-8

https://blog.csdn.net/DisMisPres/article/details/120075283

果然,https://github.com/btcsuite/btcd/tree/master/chaincfg/chainhash 目录下也有一个go.mod

我们的解决办法:

在go mod tidy执行之前,预先go get -v github.com/btcsuite/btcd/chaincfg/chainhash


后面发现又不行了。。参考这篇,https://juejin.cn/post/6844903927012392967

报错信息为:

1
2
3
4
5
6
7
8
9
 > git rev-list --no-walk 93f1295331863d8b5188d58d1c45187047d71df9 # timeout=10
[taskon-server] $ /bin/sh -xe /tmp/jenkins5934943817064139911.sh
+ cd /data/.jenkins/workspace/taskon-server
+ go get -v github.com/btcsuite/btcd/chaincfg/chainhash
go: github.com/btcsuite/btcd/chaincfg/chainhash: ambiguous import: found package github.com/btcsuite/btcd/chaincfg/chainhash in multiple modules:
github.com/btcsuite/btcd v0.0.0-20171128150713-2e60448ffcc6 (/data/go/pkg/mod/github.com/btcsuite/btcd@v0.0.0-20171128150713-2e60448ffcc6/chaincfg/chainhash)
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 (/data/go/pkg/mod/github.com/btcsuite/btcd/chaincfg/chainhash@v1.0.2)
Build step 'Execute shell' marked build as failure
Finished: FAILURE

在go.mod里加上 replace github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 => github.com/btcsuite/btcd v0.0.0-20171128150713-2e60448ffcc6


go get时出现terminal prompts disabled


go get时出现terminal prompts disabled

https://all.dashen.tech/2020/11/23/go-get%E6%97%B6%E5%87%BA%E7%8E%B0terminal-prompts-disabled/




go install github.com/xxx/xxxx/xxxxx@latest 安装的目录


Go 1.16及以后版本可以用install的方式,

执行完命令后,二进制文件被安装在 $GOPATH/bin目录下。如果没有设置$GOPATH,则被默认安装在$HOME/go/bin目录下。

Go 1.16 中关于 go get 和 go install 需要注意的地方

Go 1.16 中 go install 和 go get 有些不兼容的变更,但1.16 中模块更简洁,减少了使用时的心智负担





20240826

修改go.mod中的某个依赖的版本为老的,go mod tidy后又会变为新的~

需要用exclude 或者 replace

https://blog.csdn.net/weixin_42988781/article/details/129668867

https://blog.csdn.net/love666666shen/article/details/117445968