Erran
Erran

Reputation: 171

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?

Upvotes: 4

Views: 3727

Answers (1)

Benjamin Peterson
Benjamin Peterson

Reputation: 20580

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

Upvotes: 4

Related Questions