在主要依赖项更新后更新go模块导入引用 [英] Update go module import references after major dependency update

查看:570
本文介绍了在主要依赖项更新后更新go模块导入引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方案:我需要在存储库中将第三方Go模块更新为新的主要版本.

Scenario: I need to update a third party Go Module to a new major version across a repository.

示例:github.com/google/go-github/v20github.com/google/go-github/v24

在更新go.modgo.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:

  1. 如果您拥有一个图书馆并且想要引入重大更改,那么您就必须解决所有Go文件和子功能以及 更新导入路径以包括v2,v3等.此工具可以做到 自动执行一个命令.

  1. 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.

换句话说,如果您是从v2v3的模块的作者,则可以使用此实用程序;如果您是从v2v2的模块的使用者,则也可以使用此实用程序. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆