将Go依赖项替换为分支:”…用于两个不同的模块路径”

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

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.

huangapple
  • 本文由 发表于 2023年7月11日 12:48:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658787.html
匿名

发表评论

匿名网友

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

确定