golang 模块改名导致本地测试失败

golang module name change causes local tests to fail

我有一个别人代码的分支。他们的模块名称格式如下:

module github.com/foo/bar/v3

我在本地做了一些更改并将本地 go.mod 更新为 v4 而不是 v3 但这现在导致测试 运行ning本地失败(见下文,我已将输出通用化)。

Note: the go.sum at this point is empty.

$ go test -v

go: finding module for package github.com/foo/bar/v3
go: found github.com/foo/bar/v3 in github.com/foo/bar/v3 v3.0.0
# github.com/foo/bar/v4_test [github.com/foo/bar/v4.test]
./some_test.go:232:19: x.Y undefined (type *package.Example has no field or method Y)
FAIL    github.com/foo/bar/v4 [build failed]

我不确定它为什么要尝试找到包的实际 v3 版本,从而更新 go.sum 以包含它?

我从测试文件中可以看出这个包使用了不同的包名(例如package foo_test),所以它在编写测试代码时不依赖于导出的数据结构。所以也许这就是为什么会发生这种情况?它看到对 x.Y 的引用,然后在 github.

中查找 x

但是我不确定为什么当我在 go.mod 文件中使用 v3 参考时测试会 运行 正常?

对这里发生的事情有什么想法,以及当你在一个分叉项目上工作时 bumping go module 的正确过程应该是什么?

谢谢。

如果您在 go.mod 文件中更改 module 名称,则必须将所有 import 路径替换为更新后的 module 名称。

当您将模块 github.com/foo/bar/v3 替换为 github.com/foo/bar/v4 时,您必须 find and replace 在整个项目中对 github.com/foo/bar/v3 的所有引用都使用 github.com/foo/bar/v4

那么你的 $ go test -v 应该 运行 正确。