bazel: sh_binary scripts can't find generated data when executed by Skylark action or genrule
Consider this repository https://github.com/dfabulich/wheres-my-data
There’s a data.txt file in the build_tools directory, and a simple build script in build_tools.sh that accepts one CLI argument and copies data.txt to the given location.
data.txt:
data
build_tools.sh:
#!/bin/bash
cp build_tools/data.txt $1
It’s declared as a sh_binary in build_tools/BUILD.
sh_binary(
name='build_tools',
srcs=['build_tools.sh'],
data=['data.txt'],
visibility=['//visibility:public'],
)
There’s a simple Skylark rule in build_tools/example.bzl; the example rule runs build_tools to generate a %{name}.txt file as output.
(EDIT: previously used the command parameter to ctx.action, which triggered issue #1192 as well as the current issue #1147; I’ve switched the example below to use executable instead.)
def _impl(ctx):
ctx.action(
executable = ctx.executable._build_tools,
arguments = [ctx.outputs.text.path],
outputs = [ctx.outputs.text],
)
example = rule(
implementation = _impl,
attrs = {
"_build_tools": attr.label(executable=True, allow_files=True, default=Label("//build_tools"))
},
outputs = {
"text": "%{name}.txt",
}
)
Finally, foo/BUILD declares a foo target with the example rule.
load('/build_tools/example', 'example')
example(
name='foo',
)
When I bazel build foo, I expect it to succeed and generate foo.txt, and it does succeed on OS X, but it fails on Linux, where the sandbox is enabled, with this error:
$ bazel build foo --verbose_failures
INFO: Found 1 target...
ERROR: /tmp/github/wheres-my-data/foo/BUILD:3:1: error executing shell command: 'bazel-out/local_linux-fastbuild/bin/build_tools/build_tools bazel-out/local_linux-fastbuild/bin/foo/foo.txt' failed: namespace-sandbox failed: error executing command
(cd /home/bamboo/.cache/bazel/_bazel_bamboo/ac68465916372626e6b4e6f2ba11ef97/wheres-my-data && \
exec env - \
/home/bamboo/.cache/bazel/_bazel_bamboo/ac68465916372626e6b4e6f2ba11ef97/wheres-my-data/_bin/namespace-sandbox @/home/bamboo/.cache/bazel/_bazel_bamboo/ac68465916372626e6b4e6f2ba11ef97/wheres-my-data/bazel-sandbox/c76d9207-afd2-4fd5-a97c-cebd1a1e2303-0.params -- /bin/bash -c 'bazel-out/local_linux-fastbuild/bin/build_tools/build_tools bazel-out/local_linux-fastbuild/bin/foo/foo.txt').
cp: cannot stat 'build_tools/data.txt': No such file or directory
Target //foo:foo failed to build
About this issue
- Original URL
- State: closed
- Created 11 years ago
- Reactions: 2
- Comments: 15 (9 by maintainers)
Links to this issue
Commits related to this issue
- Workaround https://github.com/bazelbuild/bazel/issues/1147, fix working directory The working directory under Skylark is bazel-wheres-my-data, but the script expects the working directory to be bazel... — committed to dfabulich/wheres-my-data by dfabulich 11 years ago
- Workaround https://github.com/bazelbuild/bazel/issues/1147, fix working directory The working directory under Skylark is bazel-skylark-depends-on-java-binary, but the script expects the working direc... — committed to dfabulich/skylark-depends-on-java-binary by dfabulich 11 years ago
Still observing this issue in Bazel 5.1.0
Pretty odd. Also running into this issue. For some weird reason, I can get it to work if I first place the
datadependency in ash_library()and then drag thatsh_library()in asdataof thesh_binary().