英文:
How to prevent go mod tidy from looking up a replaced module path
问题
考虑以下设置:
go.mod
module example.com/main
require example.com/sub dummy
replace example.com/sub => ./sub
sub/go.mod
module example.com/sub
如果我在主目录中运行 go mod tidy
,它会输出:
go: errors parsing go.mod:
[…]/go.mod:3: 无法识别的导入路径 "example.com/sub":读取 https://example.com/sub?go-get=1 时出错:404 Not Found
即使该URL存在,根据我的理解,由于replace
指令,go mod
与原始源没有任何关系,因为我已经替换了它。那么为什么它还要查询源呢?我该如何防止这种情况发生?
我已经尝试设置GOPROXY=off
,结果是:
[…]/go.mod:3: 由于 GOPROXY=off,模块查找被禁用
英文:
Consider the following setup:
go.mod
module example.com/main
require example.com/sub dummy
replace example.com/sub => ./sub
sub/go.mod
module example.com/sub
If I run go mod tidy
in the main directory, it emits
go: errors parsing go.mod:
[…]/go.mod:3: unrecognized import path "example.com/sub": reading https://example.com/sub?go-get=1: 404 Not Found
Even if the URL existed, my understanding is that due to the replace
directive, go mod
has no business whatsoever with the original source because I replaced it. So why is it querying the source then? And how can I prevent that?
I already tried to set GOPROXY=off
which resulted in
[…]/go.mod:3: module lookup disabled by GOPROXY=off
答案1
得分: 5
看一下go mod tidy
,首先尝试(Go 1.16+,来自issue 26603):
go mod tidy -e
> -e
标志使得tidy
在加载包时遇到错误时尝试继续进行。
英文:
Looking at go mod tidy
, try first (Go 1.16+, from issue 26603):
go mod tidy -e
> The -e
flag makes tidy
attempt to proceed despite errors encountered while loading packages.
答案2
得分: 4
只需分配一个适当的版本号,如v0.0.0
,它就会起作用。
Go模块使用语义化版本模型,不能使用像dummy
这样的任意版本。支持的版本格式在模块版本编号中有描述。
额外提示:避免嵌套Go模块。这可能会导致混乱的设置,并在以后的工具使用中出现问题。
英文:
Just assign a proper version number like v0.0.0
and it will work.
Go modules use the semantic versioning model and cannot have arbitrary versions like dummy
. The supported version formats are described in Module version numbering.
Bonus note: avoid nesting Go modules. That may lead to a messy setup and problems with tooling down the road.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论