skylark rule using an java_binary executable fails because runfiles directory is not created
When I used an executable built from a java_binary from skylark, it fails complaining that the runfiles directory is not available. This is because Bazel builds the wrapper script for the java_binary target but not the runfiles directory.
However if I use a genrule with the same java_binary executable it works fine. This seems like a problem with skylark.
See the following example:
=== BUILD ===
load("test", "test")
java_library(
name = "test",
srcs = ["TestCase.java"],
)
java_binary(
name = "test_app",
runtime_deps = [":test"],
main_class = "TestCase",
)
test(name = "test_txt")
genrule(
name = "test_genrule",
tools = [":test_app"],
outs = ["test_txt2"],
cmd = "$(location :test_app) > $@",
)
=== test.bzl ===
def test_impl(ctx):
ctx.action(
outputs = [ctx.outputs.txt],
mnemonic = "TestStep",
inputs = [ctx.executable._test_app],
command = "%s > %s" % (ctx.executable._test_app.path, ctx.outputs.txt.path),
use_default_shell_env = True)
return struct()
test = rule(
test_impl,
attrs = {
"_test_app": attr.label(executable=True, default=Label("//:test_app"), allow_files=True),
},
outputs = {"txt": "lib%{name}.txt"}
)
=== TestCase.java ===
class TestCase {
public TestCase() {
}
public static void main(String [] args) {
System.out.println("test case");
}
}
I found a workaround to this issue. It doesn't mean the problem is resolved though. At least documentation or better examples should be made. The use of experimental ctx.command_helper doesn't make this a very stable solution.
I changed the rule implementation to the following then it works:
def test_impl(ctx):
cmd_helper = ctx.command_helper([ctx.attr._test_app], {})
ctx.action(
outputs = [ctx.outputs.txt],
mnemonic = "TestStep",
inputs = cmd_helper.resolved_tools,
command = "%s > %s" % (ctx.executable._test_app.path, ctx.outputs.txt.path),
use_default_shell_env = True)
return struct()
It turns out the workaround no longer works since ctx.command_helper is removed. The last time I tested this is still broken and we're still having problem to a java_binary target from skylark correctly.
The java_binary providers seem to have some new goodies that can be used here. To get the "depends on all outputs of the java_binary target" behavior, the following can be added to the action inputs:
java_binary_input.files_to_run.executable
java_binary_input.default_runfiles.files
java_binary_input.data_runfiles.files
Unfortunately, it doesn't look like there's any docs for these providers. I just happened to find them using print/dir. :(
shaoster: I think this will do.
Given that I think the documentation should be fixed to cover this use case.
Unfortunately, if you are creating a skylark rule that takes java_binary as an attr, the following doesn't work:
'java_binary_dep' : attr.label(providers=['default_runfiles'])
So it seems the duck-type checking for the exposed native providers hasn't been implemented?
More info: When I try to work around this issue by wrapping a native rule with a skylark rule, the following code:
return struct(
new_name = ctx.attr.java_binary_dep.default_runfiles
)
fails with
Value of provider 'new_name' is of an illegal type: runfiles
However, if I maintain the name, default_runfiles and data_runfiles seem to work:
return struct(
default_runfiles = ctx.attr.java_binary.default_runfiles
data_runfiles = ctx.attr.java_binary.data_runfiles
)
Not sure if this is expected behavior.
Update: based on further inspection of the docs (http://bazel.io/docs/skylark/cookbook.html#runfiles), it seems like default_runfiles, and data_runfiles are special and whatever reads the returned struct expects a different type for them compared to other providers.
actions API cleanup
What is the current best way to work around this issue these days? Not having much luck adding
ctx.attr._scalac.data_runfiles.files and ctx.attr._scalac.data_runfiles.files to the input to the ctx action.
Where
"_scalac": attr.label(executable=True, cfg="host",default=Label("//src/java/io/bazel/rulesscala/scalac")),
(scalac is a java_binary)
ctx.action(
inputs=ins + list(ctx.attr._scalac.data_runfiles.files) + list(ctx.attr._scalac.data_runfiles.files),
outputs=outs,
executable=ctx.executable._scalac,
mnemonic="Scalac",
progress_message="scala %s" % ctx.label,
execution_requirements={"supports-workers": "1"},
arguments=["--jvm_flag=%s" % flag for flag in ctx.attr.jvm_flags] + ["@" + argfile.path],
)
I only see this error when trying to use remote workers, so not sure if there is some other magic required for that case?
How do you use remote workers? Are you sure you're not talking about remote cache? Just so you know remote workers is purely a prototype, not a feature intended as a feature for production use. What's the command line you used?
I'm using remote workers via:
startup --host_jvm_args="-Dbazel.DigestFunction=SHA1"
build --watchfs
build --rest_cache_url=http://localhost:1337
build --experimental_multi_threaded_digest
build --spawn_strategy=remote --remote_worker=127.0.0.1:8080
build --jobs=3
I modified the hard coded usage in the bazel worker from being hazelcast to looking for the rest url parameters. Other than that stock bazel 0.4.4
(And thanks for the FYI -- i know its not solid yet, but trying to get just a ball park sense of how well it would/wouldn't work for us and how it might behave. Side benefit is that hopefully it would sidestep the remote -> standalone fallback issue when just using the cache.)
I see. The remote worker is very much just a prototype. It shows interested parties how to implement a real remote worker, it itself is not a real reliable remote worker.
Also --rest_cache_url and --remote-worker are not meant to work together.
A user should use either one of the following:
- --rest_cache_uril
- --remote_cache
- --remote_cache and --remote_worker
This needs some more documentation. I have a change for that. We also need a separate feature request to have the result of sandbox spawn and worker spawn to be cacheable. I'll open a thread to discuss this with Philipp and Ulf.
Interesting, all good info. Be very curious to see how this evolves/more documentation. Remote cache vs rest_cache_uri currently give the same errors anyway. If i have time at some point i'll look closer at that prototype code see if something obviously wrong in there.
Thanks for the info!
@hhclam from
The remote worker is very much just a prototype. It shows interested parties how to implement a real remote worker, it itself is not a real reliable remote worker.
How reliable is the protocol/client side bazel stuff? if it was just the worker that was prototype is it worth anyone spending time trying to make things work there?
The protocol and client side in Bazel is being actively worked on by Ola from Google. I think it's relatively stable. The "demo" remote worker has been left in dust for a long time.
I'm interested in building a production quality remote worker, but I'm not working on this at the moment
The remote strategy doesn't support runfiles right now, see #1593.
Independently of that, for Skylark actions, you need to create the action in just the right way to make sure that runfiles are setup correctly. That should work today, but I'm not sure what the right Skylark incantation is.
This seems to fixed now.