我有一个关于 golang 依赖项的问题。
我的应用程序定义了一个 go.mod,如下所示:
module my.host.com/myapp
require (
ext1.com/module1 v0.0.1
)
go 1.14
ext1.com/module1 依赖于 ext2.com/module2 v0.0.2,而后者又依赖于 ext3.com/module3 v0.0.3安全扫描检测到 ext3.com/module3 v0.0.3 不安全,必须更新到 v0.0.4。
有没有办法“强制” myapp 只获取 repo3.com/module3 v0.0.4,覆盖 module2 v0.0.2 go.mod 中定义的指令?
假设 ext1.com/module1 v0.0.1 已经是最新版本,所以升级它不起作用。
“排除 ext3.com/module3 v0.0.3”也不起作用:构建成功(golang 1.17.2),但存在运行时错误。
“替换”会起作用吗?假设 ext2 v0.0.3 依赖于安全的 ext3 v0.0.4,我的意思是:
module my.host.com/myapp
require (
ext1.com/module1 v0.0.1
)
replace ext2.com/module2 v0.0.2 => ext2.com/module2 v0.0.3
go 1.14
提前致谢!
最佳答案
运行 go get -u ext3.com/module3@v0.0.4
.
这会将模块升级到 至少 v0.0.4
鉴于依赖 main -> B -> C
, 当 main
需要更高版本的 C
比B
所要求的要多,选择高版本,// indirect
.
看到这个 https://go.dev/ref/mod#go-mod-file-require
If the go directive specifies go 1.16 or lower, the go command adds an indirect requirement when the selected version of a module is higher than what is already implied (transitively) by the main module’s other dependencies. That may occur because of an explicit upgrade (go get -u ./...)
我引用这部分是因为你的 go.mod 有
go 1.14
关于go - 在 golang 中强制传递依赖版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70100325/