How to execute a target?

513 views
Skip to first unread message

Hilco Wijbenga

unread,
Feb 24, 2021, 2:08:16 AM2/24/21
to bazel-discuss
Hi all,

I want to build a tool ("my_java_tool") and execute it to generate
code. I seem to be getting the syntax wrong.

def _my_java_tool_binary_impl(ctx):
<snip/>
ctx.actions.run(
<snip/>
executable = ctx.attr.the_executable_jar,
)

my_java_tool_binary = rule(
implementation = _my_java_tool_binary_impl,
attrs = {
"the_executable_jar": attr.label(
executable = True,
cfg = "exec",
allow_files = True,
default = "//my_java_tool:the_executable_jar",
),
},
)

This causes an error: got value of type 'Target', want 'File, string,
or FilesToRunProvider'.

Assuming I'm on the right track at all, what is the right syntax?

Cheers,
Hilco

Austin Schuh

unread,
Feb 24, 2021, 2:16:59 AM2/24/21
to Hilco Wijbenga, bazel-discuss
I think you want cfg = "host" because the tool is going to run on your build machine, not on the target.

Drop the allow_files and see if that fixes it.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAE1pOi0YwOvRSeP1Roo%2Bf-jhTYEfqXRzCGdV68Ow%2BwUV4ViTGg%40mail.gmail.com.

Gregg Reynolds

unread,
Feb 24, 2021, 3:41:27 AM2/24/21
to Hilco Wijbenga, bazel-discuss
On Wed, Feb 24, 2021 at 1:08 AM Hilco Wijbenga <hilco.w...@gmail.com> wrote:
Hi all,

I want to build a tool ("my_java_tool") and execute it to generate
code. I seem to be getting the syntax wrong.

def _my_java_tool_binary_impl(ctx):
    <snip/>
    ctx.actions.run(
        <snip/>
        executable = ctx.attr.the_executable_jar,
    )


Try

     executable = ctx.file.the_executable_jar,
 

Gregg Reynolds

unread,
Feb 24, 2021, 3:42:10 AM2/24/21
to Hilco Wijbenga, bazel-discuss
And use allow_single_file = True

John Cater

unread,
Feb 24, 2021, 8:22:15 AM2/24/21
to Gregg Reynolds, Hilco Wijbenga, bazel-discuss
This exact case (and many others) are covered in the examples repo, found at https://github.com/bazelbuild/examples/tree/master/rules

For this specific case, see actions_run/execute.bzl, which shows using `ctx.executable` instead of `ctx.attr` or `ctx.file`.

Also, the original use of `cfg = "exec"` is correct: the host platform is usually always available as an execution platform, but with remote execution there can be more execution platforms that are not the host platform. Using `cfg = "exec"` therefore has the same effect without remote execution, and avoids a number of potential problems with remote execution.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.

Gunnar Wagenknecht

unread,
Feb 24, 2021, 10:37:46 AM2/24/21
to Hilco Wijbenga, bazel-discuss
Hi Hilco,

> I want to build a tool ("my_java_tool") and execute it to generate
> code. I seem to be getting the syntax wrong.


If you are looking for a builder template I can highly recommend:
https://github.com/salesforce/bazel-java-builder-template

It does proper argument handling, supports workers and uses the executable from a java_binary.

See lines 32, 155 in mybuilder/defs.bzl
https://github.com/salesforce/bazel-java-builder-template/blob/0110f1d1023827d8b6f08fa7356485eb784d11f6/mybuilder/defs.bzl#L32
https://github.com/salesforce/bazel-java-builder-template/blob/0110f1d1023827d8b6f08fa7356485eb784d11f6/mybuilder/defs.bzl#L155

The java_binary is defined here:
https://github.com/salesforce/bazel-java-builder-template/blob/0110f1d1023827d8b6f08fa7356485eb784d11f6/src/main/java/com/salesforce/bazel/javabuilder/mybuilder/BUILD#L2


-Gunnar


--
Gunnar Wagenknecht
gun...@wagenknecht.org, http://guw.io/



Reply all
Reply to author
Forward
0 new messages