使用Bazel构建Go时无法解析存储库。

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

Building Go with Bazel could not resolve a repository

问题

我正在按照"使用Bazel构建Go服务" youtube 中的步骤进行操作,并创建了一个带有依赖项的简单项目,但在构建时出现错误:

ERROR: /builds/mvfwd/issue-bazel-go/cmd/BUILD.bazel:3:11: 找不到包'@com_github_pallinder_go_randomdata//': 无法解析仓库'@com_github_pallinder_go_randomdata'并被'//cmd:cmd_lib'引用
ERROR: 目标'//cmd:cmd'的分析失败;构建中止:分析失败
INFO: 耗时: 1.263秒
INFO: 0个进程。
FAILED: 构建未成功完成(已加载1个包,配置了165个目标)
FAILED: 构建未成功完成(已加载1个包,配置了165个目标)

完整日志

版本

$ bazel --version
bazel 4.1.0

$ go version
go version go1.17 linux/amd64

文件结构

项目:https://gitlab.com/mvfwd/issue-bazel-go

步骤

请参阅 .gitlab-ci.yml

  • go mod init gitlab.com/mvfwd/issue-bazel-go
    • 创建 go.mod
  • go mod tidy
    • 创建 go.sum
  • bazel run //:gazelle
    • 创建 cmd/BUILD.bazel
  • bazel build ...
    • 构建失败,出现错误

我认为可能是"go-randomdata"中的破折号导致的问题,所以添加了另一个没有破折号的依赖项(github.com/stretchr/testify/assert),但出现了相同的问题。

我做错了什么?我漏掉了什么吗?

更新
解决方法是使用以下组合:

$ bazel run //:gazelle
$ bazel run //:gazelle -- update-repos -from_file=go.mod -to_macro=deps.bzl%go_dependencies
$ bazel run //:gazelle

这里所述。update-repos...部分将使用所有必要的依赖项更新WORKSPACE。

英文:

I am following the steps from "Building Go Services With Bazel" youtube and created a simple project with dependency that fails to build with an error:

ERROR: /builds/mvfwd/issue-bazel-go/cmd/BUILD.bazel:3:11: no such package '@com_github_pallinder_go_randomdata//': The repository '@com_github_pallinder_go_randomdata' could not be resolved and referenced by '//cmd:cmd_lib'
ERROR: Analysis of target '//cmd:cmd' failed; build aborted: Analysis failed
INFO: Elapsed time: 1.263s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (1 packages loaded, 165 targets configured)
FAILED: Build did NOT complete successfully (1 packages loaded, 165 targets configured)

Whole log

Versions

$ bazel --version
bazel 4.1.0

$ go version
go version go1.17 linux/amd64

File structure

Project: https://gitlab.com/mvfwd/issue-bazel-go

Steps

See .gitlab-ci.yml

  • go mod init gitlab.com/mvfwd/issue-bazel-go
    • creates go.mod
  • go mod tidy
    • creates go.sum
  • bazel run //:gazelle
    • creates cmd/BUILD.bazel
  • bazel build ...
    • fails with error

Thought it might be because of the dash in "go-randomdata", so added another non-dashed dependency (github.com/stretchr/testify/assert), got the same issue.

What am I doing wrong? Am I missing something?

UPD:
Solution was to use the following combination

$ bazel run //:gazelle
$ bazel run //:gazelle -- update-repos -from_file=go.mod -to_macro=deps.bzl%go_dependencies
$ bazel run //:gazelle

As described here. update-repos... part would update WORKSPACE with all the necessary dependencies.

答案1

得分: 3

解决方案是使用以下组合:

$ bazel run //:gazelle
$ bazel run //:gazelle -- update-repos -from_file=go.mod -to_macro=deps.bzl%go_dependencies
$ bazel run //:gazelle

这里所述。

update-repos... 部分将使用所有必要的依赖项更新 WORKSPACE 文件。

英文:

Solution was to use the following combination

$ bazel run //:gazelle
$ bazel run //:gazelle -- update-repos -from_file=go.mod -to_macro=deps.bzl%go_dependencies
$ bazel run //:gazelle

As described here.

update-repos... part would update WORKSPACE file with all the necessary dependencies.

huangapple
  • 本文由 发表于 2021年8月18日 13:19:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/68826939.html
匿名

发表评论

匿名网友

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

确定