如何用opentelemetry-collector-contrib自带工具修复gci lint错误?
解决opentelemetry-collector-contrib中gci lint错误的自动修复方案
问题现象
在opentelemetry-collector-contrib仓库执行make golint时,会遇到如下gci相关的lint错误:
File is not
gci-ed with --skip-generated -s standard,default,prefix(github.com/open-telemetry/opentelemetry-collector-contrib)
尝试make gofmt或./.tools/goimports -w -local "github.com/open-telemetry/opentelemetry-collector-contrib"命令均无法解决该问题。
问题原因
报错信息中的--skip-generated、-s是golangci-lint内部调用gci时使用的参数,但直接通过make install-tools安装在.tools目录下的gci工具并不支持这些参数(从工具的帮助输出可确认),需要用该工具支持的命令格式执行格式化。
修复命令
使用make install-tools安装的.tools/gci工具,执行以下命令即可自动格式化代码,满足golangci-lint的gci规则要求:
./.tools/gci write --local "github.com/open-telemetry/opentelemetry-collector-contrib" ./...
命令说明
write:直接将格式化结果写入源文件(替代工具帮助中的-w参数)--local:指定本地包前缀,对应lint规则中的prefix(github.com/open-telemetry/opentelemetry-collector-contrib)./...:递归处理当前目录下所有Go文件
gci工具默认会自动处理standard(标准库)、default(第三方包)分类的导入排序,无需额外指定参数。
验证
执行完上述格式化命令后,再次运行make golint,即可通过gci的lint检查。
内容的提问来源于stack exchange,提问作者Subject17
相关产品推荐
相关产品推荐

