You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Bazel构建Go项目时出现无法解析依赖仓库错误如何修复?

问题描述

参照《Building Go Services With Bazel》教程创建了一个带第三方依赖的简单Go项目,构建时报错如下:

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)

版本信息

$ bazel --version
bazel 4.1.0

$ go version
go version go1.17 linux/amd64

文件结构

项目目录结构如下:

  • cmd/
    • BUILD.bazel
    • hello.go
    • hello_test.go
  • 根目录下文件:BUILD、WORKSPACE、go.mod、go.sum

复现步骤

    1. 执行 go mod init gitlab.com/mvfwd/issue-bazel-go,生成go.mod文件
    1. 执行 go mod tidy,生成go.sum文件
    1. 执行 bazel run //:gazelle,生成cmd/BUILD.bazel文件
    1. 执行 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文件中。

内容的提问来源于stack exchange,提问作者mvfwd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 08:54:03