如何防止 go mod tidy 查找替换的模块路径

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

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.

huangapple
  • 本文由 发表于 2022年1月1日 10:25:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/70546683.html
匿名

发表评论

匿名网友

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

确定