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

Bazel run_binary提示文件未创建但缓存中存在该文件的问题

Bazel run_binary构建zola_build时输出文件未被识别问题解决

执行bazelisk build :zola_build后,Bazel报错提示src/public/index.html未创建,但该文件实际存在于Bazel缓存目录中。以下是问题原因、解决方案及原问题相关信息:

原因与解决方法

问题核心是Bazel沙盒环境的路径映射逻辑:你在outs里指定的src/public/index.html是工作区根目录相对路径,但zola实际在沙盒的execroot/_main/src下生成了该文件,Bazel无法将这个路径关联到你声明的输出。

需调整run_binary的参数配置,让zola的输出路径匹配Bazel的预期:

推荐方案(适配zola生成整个站点场景)

zola构建会生成大量静态文件,直接声明整个输出目录更合理:

run_binary(
    name = "zola_build",
    tool = ":zola_bin",
    # 用$(RULEDIR)指定输出到当前包的输出目录,Bazel能自动捕获
    args = ["--root", "src", "build", "--output-dir", "$(RULEDIR)/public"],
    srcs = [":zola_srcs"],
    # 声明整个public目录为输出
    output_dirs = ["public"],
)

单文件输出方案(不推荐)

如果仅需捕获单个index.html文件,需让zola生成路径严格匹配outs声明:

run_binary(
    name = "zola_build",
    tool = ":zola_bin",
    args = ["--root", "src", "build", "--output-dir", "$(RULEDIR)/src/public"],
    srcs = [":zola_srcs"],
    outs = ["src/public/index.html"],
)

关键说明

  • $(RULEDIR)是Bazel内置变量,指向当前规则所在包的输出目录,沙盒环境中Bazel会自动处理该目录的映射,确保生成的文件被正确识别为输出。
  • zola默认在--root目录下生成public文件夹,通过--output-dir重定向到$(RULEDIR)下的路径,就能让Bazel捕获到这些文件。

原问题相关信息

原BUILD.bzl代码

load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
load("@aspect_bazel_lib//lib:run_binary.bzl", "run_binary")

package(default_visibility = ["//visibility:public"])

# Import zola_bin
filegroup(
    name = "zola_bin",
    srcs = ["@zola_release//:zola_bin"],
)

filegroup(
    name = "zola_srcs",
    srcs = glob([
        "src/config.toml",
        "src/templates/**",
        "src/themes/**",
        "src/content/**",
        "src/static/**",
    ]),
)

run_binary(
    # The target name
    name = "zola_build",
    # The tool to run in the action
    tool = ":zola_bin",
    args = ["--root", "src", "build"],
    srcs = [":zola_srcs"],
    outs = [
            "src/public/index.html"
    ],
)

...

执行日志

└─$ bazelisk build :zola_build                     
INFO: Analyzed target //:zola_build (0 packages loaded, 0 targets configured).
INFO: From RunBinary src/public/index.html:
Building site...
Checking all internal links with anchors.
> Successfully checked 0 internal link(s) with anchors.
-> Creating 0 pages (0 orphan) and 0 sections
Done in 13ms.

ERROR: /home/bruno/git/gitlab.com/bruno-flament/faster-develop-on-kubernetes/frontend/blog/BUILD.bazel:29:11: output 'src/public/index.html' was not created
ERROR: /home/bruno/git/gitlab.com/bruno-flament/faster-develop-on-kubernetes/frontend/blog/BUILD.bazel:29:11: RunBinary src/public/index.html failed: not all outputs were created or valid
Target //:zola_build failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.109s, Critical Path: 0.03s
INFO: 2 processes: 1 internal, 1 linux-sandbox.
ERROR: Build did NOT complete successfully

缓存文件路径

└─$ find /home/bruno/.cache/bazel | grep index.html
/home/bruno/.cache/bazel/_bazel_bruno/9960ca47c8049e8f0790da21f5807e4c/sandbox_stash/RunBinary/65/execroot/_main/src/public/index.html

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 01:14:59