bazelbuild / bazelbuild/bazel-skylib

`run_binary` will put files in the sandbox but then can't find them for `outs`.

Open
#554 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Starlark
Stars
444
Forks
202
PR merge metrics
No merged PRs in 30d

Description

# Summary

`run_binary` generates files in the sandbox directory but cannot find them when trying to set the `outs` values.

So it seems like we can't use `run_binary` as a source of files/data for other targets. Is this expected?

# Steps to Reproduce

Create these files:

```starlark
# MODULE.bazel
module(
name = "example",
version = "0.0.0",
compatibility_level = 1,
)

bazel_dep(name = "rules_python", version = "1.0.0")
bazel_dep(name = "bazel_skylib", version = "1.7.1")

python = use_extension("@rules_python//python/extensions:python.bzl", "python")

python.toolchain(
is_default = True,
python_version = "3.12.2",
)
use_repo(python, "python_3_12_2")
```

```python
# my_script.py
import sys
import pathlib

def main():
file = pathlib.Path(sys.argv[1])
file.write_text("This is my generated data.")
print(f" Wrote to file at {file.resolve(strict=True)}", file=sys.stderr)
print(" Here's proof. The written text was:", file=sys.stderr)
print(f" {file.read_text()}", file=sys.stderr)

if __name__ == "__main__":
main()
```

```starlark
# BUILD.bazel
load("@bazel_skylib//rules:run_binary.bzl", "run_binary")
load("@rules_python//python:defs.bzl", "py_binary")

py_binary(
name = "my_script",
srcs = ["my_script.py"],
)

run_binary(
name = "run_my_script",
tool = ":my_script",
args = ["some_file.txt"],
outs = ["some_file.txt"],
)
```

And, if you're using Bazelisk, a `.bazelversion` file with:

```
7.4.1
```

Then call:

```
bazel build //:run_my_script
```

# Expected Result

The `run_binary` target should be able to use the file generated by `py_binary`.

# Actual Result

Bazel errors out, saying that the output file was not created.

```console
$ bazel build //:run_my_script
INFO: Analyzed target //:run_my_script (1 packages loaded, 3 targets configured).
INFO: From RunBinary some_file.txt:
Wrote to file at /usr/local/google/home/dthor/.cache/bazel/_bazel_dthor/971d8fef15cecd42cbe8747d79f170f6/sandbox/linux-sandbox/3/execroot/_main/some_file.txt
Here's proof. The written text was:
This is my generated data.
ERROR: /usr/local/google/home/dthor/dev/skylib-example/BUILD.bazel:9:11: output 'some_file.txt' was not created
ERROR: /usr/local/google/home/dthor/dev/skylib-example/BUILD.bazel:9:11: RunBinary some_file.txt failed: not all outputs were created or valid
Target //:run_my_script failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.454s, Critical Path: 0.26s
INFO: 2 processes: 1 internal, 1 linux-sandbox.
ERROR: Build did NOT complete successfully
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.