Golang中go mod tidy报错:"no matching versions for query "latest""如何解决

Golang中go mod tidy报错:"no matching versions for query “latest”"如何解决 go 版本

go version go1.19.4 linux/amd64

go 环境变量

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/ishmeets/.cache/go-build"
GOENV="/home/ishmeets/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS="-modcacherw"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/ishmeets/go/pkg/mod"
GONOPROXY="ssd-git.juniper.net"
GONOSUMDB="ssd-git.juniper.net"
GOOS="linux"
GOPATH="/home/ishmeets/go"
GOPRIVATE="ssd-git.juniper.net"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/ishmeets/.local"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/ishmeets/.local/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.4"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/ishmeets/go/pkg/mod/xxx/xxx/fabric-mgmt-api@v0.0.0-20230119053658-acd5a1e86128/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2990676511=/tmp/go-build -gno-record-gcc-switches"

当我在模块缓存中执行 go mod tidy 时,出现了这个错误。

xxx/xxx/fabric-mgmt-api/internal/app/network-mgmt/xxx/primitives/mock: no matching versions for query "latest"

我已经将模拟文件放在了包含 mock 包的对应位置。但错误仍然存在。

我尝试过以下方法:

  1. 将仓库复制到 GOMODCACHE 之外。然后 go mod tidy 可以正常工作。 cp -a ~/go/pkg/mod/xxx/xxx/fabric-mgmt-api@v0.0.0-20230119053658-acd5a1e86128 ~/workspace/ cd ~/workspace/fabric-mgmt-api@v0.0.0-20230119053658-acd5a1e86128 && go mod tidy
  2. 使用 go 1.18。这个方法也有效。
  3. 在 go 1.20.1 上尝试。它不起作用。

更多关于Golang中go mod tidy报错:"no matching versions for query "latest""如何解决的实战教程也可以访问 https://www.itying.com/category-94-b0.html

2 回复

你好,@Ishmeet,欢迎来到论坛!

在正常情况下,通常不需要在模块缓存中运行 Go 命令。你能解释一下你想实现什么吗?

更多关于Golang中go mod tidy报错:"no matching versions for query "latest""如何解决的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


这个错误通常是因为Go模块系统在GOMODCACHE目录中执行go mod tidy时,无法解析某些依赖的版本。从你的环境变量看,问题可能与私有仓库配置有关。

问题分析:

  1. GOPRIVATE="ssd-git.juniper.net" 设置了私有仓库
  2. GONOPROXY="ssd-git.juniper.net"GONOSUMDB="ssd-git.juniper.net" 表示这些仓库不使用代理和校验
  3. 错误发生在模块缓存目录中,这可能是因为模块系统在缓存目录中无法正确处理私有依赖

解决方案:

方案1:清除模块缓存并重新下载

# 清除模块缓存
go clean -modcache

# 重新下载依赖
go mod download

# 执行tidy
go mod tidy

方案2:检查并更新私有仓库配置

# 确保私有仓库配置正确
go env -w GOPRIVATE="ssd-git.juniper.net"
go env -w GONOPROXY="ssd-git.juniper.net"
go env -w GONOSUMDB="ssd-git.juniper.net"

# 如果需要,可以临时关闭代理
go env -w GOPROXY="direct"

# 然后执行
go mod tidy

方案3:检查go.mod文件中的replace指令 检查你的go.mod文件中是否有不正确的replace指令:

# 查看go.mod文件
cat go.mod

# 如果有问题,可以尝试移除replace指令
# go mod edit -dropreplace=xxx/xxx/mock

方案4:使用vendor模式

# 初始化vendor目录
go mod vendor

# 使用vendor模式执行tidy
go mod tidy -mod=vendor

方案5:检查网络和认证 确保可以访问私有仓库:

# 测试私有仓库访问
git ls-remote https://ssd-git.juniper.net/xxx/xxx/mock.git

# 如果需要认证,配置git凭证
git config --global credential.helper store

方案6:更新Go版本并设置正确的环境变量

# 更新到最新Go版本
# 然后设置环境变量
export GO111MODULE=on
export GOPROXY=https://proxy.golang.org,direct
export GOPRIVATE=ssd-git.juniper.net

# 执行tidy
go mod tidy

如果问题仍然存在,可以尝试在项目根目录外执行:

# 在临时目录中测试
cd /tmp
mkdir test-project && cd test-project
go mod init test
# 添加你的依赖进行测试

这个错误通常与模块缓存、私有仓库配置或网络访问有关。根据你的描述,在GOMODCACHE外可以正常工作,建议优先尝试方案1清除缓存。

回到顶部