Bazel引入StableHLO时出现仓库循环定义错误的求助
问题背景
通过Bazel的WORKSPACE引入StableHLO,基础配置可正常运行,但添加特定依赖后触发循环仓库定义错误。
WORKSPACE配置
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") STABLEHLO_COMMIT = "8c7d87b6f4d3e1d692afb94494530ed993b6eb59" http_archive( name = "stablehlo", build_file = "@stablehlo//:BUILD.bazel", url = "https://github.com/openxla/stablehlo/archive/{commit}.tar.gz".format(commit = STABLEHLO_COMMIT), )
触发错误的操作
在项目src/stablehlo/stablehlo/reference/目录的BUILD文件中添加@stablehlo//:reference_ops依赖:
cc_library( name = "reference", linkstatic = True, alwayslink = True, srcs = glob(["*.cpp"]), hdrs = glob(["*.h"]), deps = [ "@stablehlo//:reference_ops", ], visibility = ["//visibility:public"], )
错误信息
ERROR: Circular definition of repositories: .-> @@stablehlo [self-edge] `-- INFO: Repository stablehlo instantiated at: /ir/WORKSPACE:5:13: in <toplevel> Repository rule http_archive defined at: /root/.cache/bazel/_bazel_root/28c542a9beec5e078b3e48145a4e82ef/external/bazel_tools/tools/build_defs/repo/http.bzl:382:31: in <toplevel> ERROR: Analysis of target '//:ir' failed; build aborted INFO: Elapsed time: 0.118s, Critical Path: 0.00s INFO: 1 process: 1 internal. ERROR: Build did NOT complete successfully FAILED: Fetching repository @@stablehlo; starting
其他相关配置
- 根目录BUILD文件:
cc_binary( name = "ir", linkshared = True, linkstatic = True, srcs = [ "//src/stablehlo/stablehlo/reference", ], deps = [ "//src/stablehlo/stablehlo/reference", ], )
- Bazel版本:6.1.2
- 启动参数:
build --action_env=BAZEL_CXXOPTS="-std=c++17" build --nobuild_runfile_links build --nolegacy_external_runfiles build --experimental_repo_remote_exec
已尝试的无效操作
- 将库名称改为
foo并调整相关文件,问题依旧 - 使用
local_repository的类似配置可正常运行,但http_archive方式不行
原因分析
问题出在http_archive的build_file参数:你指定了@stablehlo//:BUILD.bazel,但这个路径是待下载的stablehlo仓库内部的文件。Bazel初始化stablehlo仓库时,需要先读取这个BUILD文件,但读取该文件又依赖stablehlo仓库本身已经被初始化,形成了自引用循环。
当项目未引用@stablehlo的目标时,Bazel可能延迟加载仓库定义,不会触发循环;但一旦添加对@stablehlo//:reference_ops的依赖,Bazel需要解析该目标,必须初始化仓库,此时循环就暴露出来了。
解决方法
方法1:使用StableHLO官方推荐的引入方式
StableHLO仓库本身包含完整的BUILD配置,无需手动指定build_file。修改WORKSPACE为:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "stablehlo", url = "https://github.com/openxla/stablehlo/archive/8c7d87b6f4d3e1d692afb94494530ed993b6eb59.tar.gz", strip_prefix = "stablehlo-8c7d87b6f4d3e1d692afb94494530ed993b6eb59", # 可选:添加sha256校验值,从GitHub下载页面获取 # sha256 = "xxx", ) # 加载StableHLO的依赖(如果需要) load("@stablehlo//:stablehlo_deps.bzl", "stablehlo_deps") stablehlo_deps()
关键修改:去掉build_file参数,添加strip_prefix(匹配tar.gz解压后的目录前缀)。
方法2:自定义build_file(如果必须)
不要引用待下载仓库内部的文件,改用项目本地的BUILD文件。比如在项目根目录创建stablehlo.BUILD,然后修改WORKSPACE:
http_archive( name = "stablehlo", build_file = "//:stablehlo.BUILD", url = "https://github.com/openxla/stablehlo/archive/8c7d87b6f4d3e1d692afb94494530ed993b6eb59.tar.gz", strip_prefix = "stablehlo-8c7d87b6f4d3e1d692afb94494530ed993b6eb59", )
这种方式下,build_file来自本地项目,不会依赖未初始化的stablehlo仓库,避免循环。
验证
修改WORKSPACE后,重新运行Bazel构建命令,循环依赖错误应该会消失,@stablehlo//:reference_ops可正常被引用。
内容的提问来源于stack exchange,提问作者joel

