英文:
Replacing Go dependency with fork: "... used for two different module paths"
问题
我有一个非常大的Go项目,依赖于github.com/golang/mock
。不幸的是,这个包已经不再维护了,开发者们建议使用go.uber.org/mock
的分支。所以我想用go.uber.org/mock
替换github.com/golang/mock
的依赖。
据我了解,这正是go.mod的replace
指令的作用(允许你替换一个依赖而不必在每个文件中更改导入路径)。所以,只需在我的go.mod
中添加以下行即可将该模块替换为分支:
replace github.com/golang/mock => go.uber.org/mock v0.2.0
不幸的是,当尝试运行任何go
命令时会出现错误:
$ go mod tidy
...
go: go.uber.org/mock@v0.2.0 used for two different module paths (github.com/golang/mock and go.uber.org/mock)
我在这里做错了什么?
如果需要,你可以在这里查看存储库/提交。
英文:
I have a very large Go project that depends on github.com/golang/mock
. Unfortunately, this package is no longer maintained, and the developers have directed people to use the fork at go.uber.org/mock
. So I'd like to replace the github.com/golang/mock
dependency with go.uber.org/mock
.
As I understand it, this is exactly what go.mod's replace
directive is for (allowing you to replace a dependency without having to change the import path in every single file). So, replacing this module with the fork should be as easy as adding the following line to my go.mod
:
replace github.com/golang/mock => go.uber.org/mock v0.2.0
Unfortunately, this is causing errors when trying to run any go
command:
$ go mod tidy
...
go: go.uber.org/mock@v0.2.0 used for two different module paths (github.com/golang/mock and go.uber.org/mock)
What am I doing wrong here?
You can view the repo/commit here if necessary.
答案1
得分: 5
根据replace指令的文档:
无论替换是使用本地路径还是模块路径指定的,如果替换模块有一个go.mod文件,它的module指令必须与其替换的模块路径匹配。
在这里不能使用replace
指令。
由于go.uber.org/mock
是使用不同的模块路径发布的,我认为你必须将其视为与github.com/golang/mock
完全不同的模块。
英文:
According to the doc of the replace directive:
> Regardless of whether a replacement is specified with a local path or module path, if the replacement module has a go.mod file, its module directive must match the module path it replaces.
The replace
directive can not be used here.
Since go.uber.org/mock
is published with a different module path, I think you have to treat it as a totally different module than github.com/golang/mock
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论