bazelbazel-rulesbazel-aspect

Why does Bazel (run_binary) tell me that he can't write a file when he creates files well?


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"
    ],
)

...

When i build target zola_build:

└─$ 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

Bazel error: output 'src/public/index.html' was not created

But, the file was created, because the file exists in cache

└─$ 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

Maybe, i need to declare something in BUILD file ?

Regards Thanks a lot

B.F

I try to build run_binary rules with bazel and create files.


Solution

  • Bazel uses a separate filesystem tree for outputs that is distinct from but has a parallel hierarchy to the source tree. (The bazel-out/ symlink in the root of your workspace is a view into it.) When a rule declares an output like src/public/index.html, Bazel is actually going to expect the action to write to something like bazel-out/k8-fastbuild/src/public/index.html. Typically, one would use a Bazel make-variable expansion like $(execpath :src/public/index.html) in the arguments of the tool to tell it to write to the correct path. Zola is going to be a bit more complex because it emits many files. You'll probably have to play around with --output-dir and Bazel's $(RULEDIR) variable. Something like --output-dir=$(RULEDIR) maybe?