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

如何为非Bazel目标模块在Bzlmod本地注册表添加BUILD文件?

解决Bzlmod本地注册表中非Bazel依赖的BUILD文件缺失问题

一、给非Bazel模块添加BUILD文件的可行方案

1. 补丁方式添加BUILD文件(修正之前的失败问题)

之前补丁没生效,大概率是路径匹配或格式问题。以errorprone 2.24.1为例:

  • 在注册表的modules/errorprone/2.24.1目录下新建patches文件夹,创建add_build_file.patch文件
  • 补丁内容要对应解压后的源码结构(注意和source.json里的strip_prefix匹配):
diff --git a/BUILD.bazel b/BUILD.bazel
new file mode 100644
index 0000000..d41d8cd
--- /dev/null
+++ b/BUILD.bazel
@@ -0,0 +1,8 @@
+java_library(
+    name = "annotations",
+    srcs = glob(["annotations/src/main/java/com/google/errorprone/annotations/**/*.java"]),
+    visibility = ["//visibility:public"],
+)
  • 修改source.json,加入补丁配置:
{
  "url": "https://github.com/google/error-prone/archive/refs/tags/v2.24.1.tar.gz",
  "integrity": "sha256:...",
  "strip_prefix": "error-prone-2.24.1",
  "patches": [
    "patches/add_build_file.patch"
  ]
}

2. 用模板文件自动生成BUILD

如果补丁不好调试,也可以直接在模块目录放BUILD模板:

  • 在modules/errorprone/2.24.1下创建BUILD.bazel.template,写入需要的BUILD规则
  • 编写MODULE.bazel,通过repo_rule复制模板生成BUILD文件:
module(
    name = "errorprone",
    version = "2.24.1",
)

repo_rule(
    implementation = _copy_build_file,
    attrs = {},
)

def _copy_build_file(ctx):
    ctx.file("BUILD.bazel", ctx.read(ctx.path("BUILD.bazel.template")))

二、适配Maven依赖的模块扩展方案

把你之前的new_simple_repo改成模块扩展,衔接逻辑如下:

  1. 编写模块扩展规则:
def _maven_simple_ext_impl(ctx):
    for dep in ctx.attr.deps:
        # 下载Maven包
        ctx.download(
            url = dep.url,
            output = dep.output,
            integrity = dep.integrity,
        )
        # 生成对应的BUILD文件
        ctx.file(dep.output + "/BUILD.bazel", dep.build_file_content)

maven_simple_ext = module_extension(
    implementation = _maven_simple_ext_impl,
    attrs = {
        "deps": attr.label_list(
            allow_files = False,
            providers = [MavenDepInfo],
        ),
    },
)

# 定义Maven依赖的信息载体
MavenDepInfo = provider(
    fields = ["url", "output", "integrity", "build_file_content"],
)

def maven_dep(name, url, output, integrity, build_file_content):
    return MavenDepInfo(
        url = url,
        output = output,
        integrity = integrity,
        build_file_content = build_file_content,
    )
  1. 在注册表模块的MODULE.bazel中使用该扩展:
module(
    name = "my-guava-dep",
    version = "32.1.3-jre",
)

maven_simple_ext(
    name = "setup_deps",
    deps = [
        maven_dep(
            name = "guava",
            url = "https://repo1.maven.org/maven2/com/google/guava/guava/32.1.3-jre/guava-32.1.3-jre.jar",
            output = "guava.jar",
            integrity = "sha256:...",
            build_file_content = """
java_import(
    name = "guava",
    jars = ["guava.jar"],
    visibility = ["//visibility:public"],
)
""",
        ),
    ],
)

三、关键疑问解答

  • Maven依赖必须配置source.json吗?
    不需要。如果完全通过模块扩展拉取和配置依赖,直接在MODULE.bazel里用扩展规则处理就行,不需要source.json。source.json只适用于从源码包(比如GitHub tarball)拉取依赖的场景。

  • 为什么之前的补丁没生效?
    最常见的原因是补丁里的路径和source.json中strip_prefix后的目录结构不匹配,或者补丁格式错误(比如换行符、路径分隔符)。可以先手动解压源码包,确认要放BUILD文件的位置,再编写对应路径的补丁。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 00:15:28