英文:
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来简单解决它
如果你使用go mod和go get命令生成go.mod文件,传递依赖项将自动包含在内。然后,你可以在bazel-gazelle命令中使用这个go.mod文件
假设你的项目名为github.com/somesampleproject:
go mod init github.com/somesampleproject
然后,使用go get将你的依赖项添加到go.mod文件中:
go get firebase.google.com/go
go mod实际上也处理传递依赖项,如文档中所述这里。所以你的go.mod文件现在应该包含所有所需的依赖项
我在我们的一个项目中快速尝试了这个方法(如gazelle文档中所述):
bazel run //:gazelle -- update-repos -from_file=<insert-subfolder-here>/go.mod
现在我们的WORKSPACE文件包含了firebase依赖项,以及firebase本身的依赖项。
如果你使用工具验证你的go.mod文件,以确保没有已知安全漏洞的依赖项,那就更好了。一个快速的谷歌搜索返回了snyk、nancy和IDE(如vs code)中的内置支持
英文:
I have been struggling with this as well, but it seems that you can simply solve it by using go mod
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
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
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=\<insert-subfolder-here>/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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论