简体   繁体   English

如何防止 go 更新 go.mod 文件

[英]How to prevent go get from updating go.mod file

TL;DR: Is there any way I can forcefully prevent go get from altering the go.mod file? TL;DR:有什么办法可以强制阻止go get更改go.mod文件?

When I do a go get of certain packages, eg:当我执行go get某些软件包时,例如:

$ go get github.com/AsynkronIT/protoactor-go/protobuf/protoc-gen-gograin

It will printout that it has updated dependencies (which are defined in my go.mod file):它将打印出它已更新的依赖项(在我的go.mod文件中定义):

go get: upgraded github.com/AsynkronIT/protoactor-go v0.0.0-20200815184336-b225d28383f2 => v0.0.0-20210405044454-10bc19881ad6
# (...) Note, this happens for other packages, not just `AsynkronIT/protoactor-go`.

This causes the go.mod file to change during a CI build, and affects subsequent build stages where, while building something, it'll try to use an updated version of the dependency, which might introduce breaking changes, instead of the version defined on the go.mod file initially.这会导致go.mod文件在 CI 构建期间发生更改,并影响后续构建阶段,在此阶段,在构建某些内容时,它将尝试使用依赖项的更新版本,这可能会引入重大更改,而不是定义的版本go.mod文件最初。

I've tried using -mod=readonly or making sure the -u flag is not used but it will still update the go.mod file, eg:我尝试使用-mod=readonly或确保未使用-u标志,但它仍会更新go.mod文件,例如:

$ GOFLAGS=-mod=readonly go get github.com/AsynkronIT/protoactor-go/protobuf/protoc-gen-gograin

go get: upgraded github.com/AsynkronIT/protoactor-go v0.0.0-20200815184336-b225d28383f2 => v0.0.0-20210405044454-10bc19881ad6
# (...)

I've also tried finding similar issues, like this one , or this other one , but haven't yet find an alternative to prevent go get commands from altering the go.mod .我也尝试过寻找类似的问题,例如this onethis other one ,但还没有找到阻止go get commands 更改go.mod的替代方法。

The current workaround I'm using to stop this behavior is to do a git checkout -- go.mod right after certain go get … steps to reset any changes done by go get , and hence, avoiding breaking changes with certain dependencies newer versions. The current workaround I'm using to stop this behavior is to do a git checkout -- go.mod right after certain go get … steps to reset any changes done by go get , and hence, avoiding breaking changes with certain dependencies newer versions.

I'm using go version 1.16.3 .我正在使用 go 版本1.16.3

For Go 1.16 and after, you can use go install to install binaries without affecting go.mod对于 Go 1.16 及更高版本,您可以使用go install安装二进制文件而不影响go.mod

go install github.com/AsynkronIT/protoactor-go/protobuf/protoc-gen-gograin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM