Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Bazel run_shell not placing arguments correctly?

I have rule a:

def _a_impl(ctx):
    src = ctx.actions.declare_file("src.txt")
    ctx.actions.write(src, "nothin")
    dst = ctx.actions.declare_file("dst.txt")
    ctx.actions.run_shell(
        outputs = [dst],
        inputs = [src],
        command = "cp",
        arguments = [src.path, dst.path]
    )
    return [DefaultInfo(files = depset([dst]))]

a = rule(
    implementation = _a_impl,
)

For some reason, I'm getting the following error:

ERROR: /home/erran/example/out_dir/BUILD:9:1: error executing shell command: '/bin/bash -c cp  bazel-out/k8-fastbuild/bin/src.txt bazel-out/k8-fastbuild/bin/dst.txt' failed (Exit 1) bash failed: error executing command /bin/bash -c cp '' bazel-out/k8-fastbuild/bin/src.txt bazel-out/k8-fastbuild/bin/dst.txt

It looks like Bazel isn't parsing the arguments correctly. As you can see, the actual bash command tries to cp '' <src> <dst>

I also tried just formatting the copy command itself, which worked fine:

ctx.actions.run_shell(
    outputs = [dst],
    inputs = [src],
    command = "cp {} {}".format(src.path, dst.path)
)

Anyone know what the problem is?

like image 746
Erran Avatar asked Oct 23 '25 21:10

Erran


1 Answers

That is the documented semantics of passing string to the command parameter of run_shell. Something like this should work:

    ctx.actions.run_shell(
        outputs = [dst],
        inputs = [src],
        command = "cp $1 $2",
        arguments = [src.path, dst.path]
    )
like image 76
Benjamin Peterson Avatar answered Oct 27 '25 03:10

Benjamin Peterson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!