在主要依赖项更新后更新go模块导入引用 [英] Update go module import references after major dependency update
问题描述
方案:我需要在存储库中将第三方Go模块更新为新的主要版本.
Scenario: I need to update a third party Go Module to a new major version across a repository.
示例:github.com/google/go-github/v20
至github.com/google/go-github/v24
在更新go.mod
和go.sum
之后,如何以编程方式更新存储库中的所有import
语句以进行匹配?
After updating go.mod
and go.sum
, how can I programmatically update all import
statements within the repository to match?
这是一种不简单的方法:
Here is a simple approach that is not fool-proof:
find *.go -type f -print0 | xargs -0 sed -i '' 's/"github.com\/google\/go-github\/v20/"github.com\/google\/go-github\/v24/g'
这将捕获所有字符串实例,而不仅限于import
内(尽管这不一定是一件坏事).但是我担心在将更新从github.com/abc/def
升级到github.com/abc/dev/v2
的情况下,因为例如可能还会有一个名为github.com/abc/def-core
的模块.
This would catch all string instances and not just within import
(although that is not necessarily a bad thing). But I worry in cases where the update may be from github.com/abc/def
to github.com/abc/dev/v2
because there may also be a module named github.com/abc/def-core
, for example.
请注意,此问题无意也捕捉到您可能有重大更改,需要更新代码" yada yada -这是一个单独的主题,在这里没有被问到.
Please note that this question is not intending to also capture the "you may have breaking changes and need to update your code" yada yada - that's a separate topic and not asked about here.
推荐答案
一个好的自动化解决方案是 https://github.com/marwan-at-work/mod ,它可以在*.go
代码和go.mod
中自动添加,删除或更改所需的/vN
.
A good automated solution is https://github.com/marwan-at-work/mod, which can automatically add, remove, or change the required /vN
in your *.go
code and your go.mod
.
使用您的示例,它应该能够将代码从使用github.com/google/go-github/v20
改为使用github.com/google/go-github/v24
.
Using your example, it should be able to update your code from using github.com/google/go-github/v20
to instead use github.com/google/go-github/v24
.
从存储库中:
动机
有两个很好的用例可以做到这一点:
There are two good use cases to do this:
-
如果您拥有一个图书馆并且想要引入重大更改,那么您就必须解决所有Go文件和子功能以及 更新导入路径以包括v2,v3等.此工具可以做到 自动执行一个命令.
If you own a library and you want to introduce a breaking change, then you have to go around all your Go files and sub-pacakges and update the import paths to include v2, v3, etc. This tool just does it automatically with one command.
如果您拥有已被标记为v2或更高版本但与语义导入版本控制不兼容的库,则此工具可以解决 一个命令对您来说也是一个问题.介绍go.mod文件 使用正确的导入路径,只需运行一次mod upgrade或mod -t = X升级(其中x是最新的主要标签),以更新go文件的导入路径,以匹配您所处的任何标签.
If you own a library that is already tagged v2 or above but is incompatible with Semantic Import Versioning, then this tool can solve the problem for you with one command as well. Introduce a go.mod file with the correct import path, and just run mod upgrade once or mod -t=X upgrade (where x is the latest tag major) to update the import paths of your go files to match whatever tag you're at.
除了这两个用例之外,该实用程序最近还增加了对客户端的支持,方法是通过更改客户端的*.go
代码中的/vN
和客户端的go.mod
自动升级客户端以使用模块的其他主要版本. .
In addition to those two use cases, this utility also relatively recently added support for automatically upgrading clients to use a different major version of a module by changing the /vN
in the client's *.go
code and the client's go.mod
.
换句话说,如果您是从v2
到v3
的模块的作者,则可以使用此实用程序;如果您是从v2
到v2
的模块的使用者,则也可以使用此实用程序. v3
.
In other words, this utility can be used if you are the author of a module going from v2
to v3
, and it can also be used if you are the consumer of a module going from v2
to v3
.
它使用诸如golang.org/x/tools/go/ast/astutil
之类的包来操纵*.go
源的AST以便更新导入路径,因此,不应受到您对使用sed
表示的关注类型的影响.
It uses packages such as golang.org/x/tools/go/ast/astutil
to manipulate the AST of *.go
source in order to update import paths, and hence should not be susceptible to the types of concerns you expressed about using sed
.
这篇关于在主要依赖项更新后更新go模块导入引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!