Gazelle是否可以自动加载传递依赖项?

huangapple go评论87阅读模式
英文:

Is it possible to automatically load transitive dependencies with Gazelle?

问题

我想使用Gazelle来管理我的Go依赖项(以及它们的依赖项)在Bazel中。运行bazel run //:gazelle update-repos firebase.google.com/go会在我的WORKSPACE文件中添加一个正确配置的go_repository

go_repository(
    name = "com_google_firebase_go",
    importpath = "firebase.google.com/go",
    sum = "h1:3TdYC3DDi6aHn20qoRkxwGqNgdjtblwVAyRLQwGn/+4=",
    version = "v3.13.0+incompatible",
)

然而,这并不能直接工作。运行bazel build @com_google_firebase_go//:go_default_library会返回一个错误:

ERROR: /private/var/tmp/_bazel_spencerconnaughton/9b09d78e8f2190e9af61aa37bcab571e/external/com_google_firebase_go/BUILD.bazel:3:11: no such package '@org_golang_google_api//option': The repository '@org_golang_google_api' could not be resolved and referenced by '@com_google_firebase_go//:go'
ERROR: Analysis of target '@com_google_firebase_go//:go_default_library' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.596s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (23 packages loaded, 133 targets configured)

有没有办法告诉Gazelle加载@org_golang_google_api的传递依赖项和其他依赖项,而不需要为每个依赖项运行update-repos命令?

英文:

I'd like to use Gazelle to manage my Go dependencies (and their dependencies) in Bazel. Running bazel run //:gazelle update-repos firebase.google.com/go adds a properly configured go_repository to my WORKSPACE file:

go_repository(
    name = "com_google_firebase_go",
    importpath = "firebase.google.com/go",
    sum = "h1:3TdYC3DDi6aHn20qoRkxwGqNgdjtblwVAyRLQwGn/+4=",
    version = "v3.13.0+incompatible",
)

However, this does not work out of the box. Running bazel build @com_google_firebase_go//:go_default_library returns an error:

ERROR: /private/var/tmp/_bazel_spencerconnaughton/9b09d78e8f2190e9af61aa37bcab571e/external/com_google_firebase_go/BUILD.bazel:3:11: no such package '@org_golang_google_api//option': The repository '@org_golang_google_api' could not be resolved and referenced by '@com_google_firebase_go//:go'
ERROR: Analysis of target '@com_google_firebase_go//:go_default_library' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.596s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (23 packages loaded, 133 targets configured)

Is there a way to tell gazelle to load the @org_golang_google_api transitive dependency and others without needing to run update-repos for each one?

答案1

得分: 4

我也一直在努力解决这个问题,但似乎你可以通过使用go mod来简单解决它 Gazelle是否可以自动加载传递依赖项?

如果你使用go mod和go get命令生成go.mod文件,传递依赖项将自动包含在内。然后,你可以在bazel-gazelle命令中使用这个go.mod文件 Gazelle是否可以自动加载传递依赖项?

假设你的项目名为github.com/somesampleproject:

go mod init github.com/somesampleproject

然后,使用go get将你的依赖项添加到go.mod文件中:

go get firebase.google.com/go

go mod实际上也处理传递依赖项,如文档中所述这里。所以你的go.mod文件现在应该包含所有所需的依赖项 Gazelle是否可以自动加载传递依赖项?

我在我们的一个项目中快速尝试了这个方法(如gazelle文档中所述):

bazel run //:gazelle -- update-repos -from_file=<insert-subfolder-here>/go.mod

现在我们的WORKSPACE文件包含了firebase依赖项,以及firebase本身的依赖项。

如果你使用工具验证你的go.mod文件,以确保没有已知安全漏洞的依赖项,那就更好了。一个快速的谷歌搜索返回了snyknancy和IDE(如vs code)中的内置支持 Gazelle是否可以自动加载传递依赖项?

英文:

I have been struggling with this as well, but it seems that you can simply solve it by using go mod Gazelle是否可以自动加载传递依赖项?

If you use the go mod and go get commands to generate go.mod files, the transative dependencies are included automatically. You can then use this go.mod file in your bazel-gazelle command Gazelle是否可以自动加载传递依赖项?

let's say your project is called github.com/somesampleproject:

go mod init github.com/somesampleproject

Then, use go get to add your dependency to the go.mod file:

go get firebase.google.com/go

go mod actually handles the transitive dependencies as well, as described in the documentation here. So your go.mod file should contain all of your required dependencies now Gazelle是否可以自动加载传递依赖项?

I quickly tried this out with gazelle in one of our projects (as described in the gazelle documentation)

bazel run //:gazelle -- update-repos -from_file=\&lt;insert-subfolder-here&gt;/go.mod

now our WORKSPACE file includes the firebase dependency, but also the dependencies that firebase has itself.

For bonus points you could use tools to validate your go.mod file to make sure you have no dependencies with known security bugs, a quick google search returned snyk, nancy and built-in support in ide's such as vs code apparently Gazelle是否可以自动加载传递依赖项?

huangapple
  • 本文由 发表于 2022年2月17日 07:05:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/71150406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定