[Bug]: py_binary expression expands to more than one file
What happened?
Using rules_python's py_binary like this:
py_binary(
name = "upstream_py_binary",
srcs = ["say.py"],
main = "say.py",
)
py_binary(
name = "upstream_py_binary2",
srcs = ["say.py"],
main = "say.py",
data = [":upstream_py_binary"],
env = {"FOO": "$(execpath :upstream_py_binary)"},
)
Works fine, but if you do the same with rules_py's py_binary, you get this error:
ERROR: /home/ubuntu/dev/bazelbuild/rules_py/examples/py_binary/BUILD.bazel:9:10: in py_binary rule //examples/py_binary:py_binary2: label '//examples/py_binary:py_binary' in $(location) expression expands to more than one file, please use $(locations //examples/py_binary:py_binary) instead. Files (at most 5 shown) are: [bazel-out/aarch64-fastbuild/bin/examples/py_binary/py_binary, bazel-out/aarch64-fastbuild/bin/examples/py_binary/py_binary.venv.pth, examples/py_binary/say.py]
Version
Development (host) and target OS/architectures: ubuntu 22.04
Output of bazel --version: bazel 7.4.0
Version of the Aspect rules, or other relevant rules from your WORKSPACE or MODULE.bazel file: see repro case below
How to reproduce
On this branch: https://github.com/aspect-build/rules_py/pull/456
bazel build :upstream_py_binary2 works fine
bazel build :py_binary2 fails with the above error
Any other information?
No response
The reason this works in rules_python is that they use an exposed internal method to expand the files, rather than the one on ctx, all Bazel built-in rules exhibit this behavior, iirc there's an open issue upstream for it.
That being said, it likely makes sense to remove the other outputs from files on DefaultInfo.
Will removing those extra files not break some other workflow?
here's that change if not: https://github.com/aspect-build/rules_py/pull/467
maybe https://github.com/bazelbuild/bazel/pull/24690 is the better solve here
Yes, this should be fixed in Bazel. I wanted to send a patch myself, but never got time.
This appears to have been resolved by https://github.com/bazelbuild/bazel/pull/24690, available in 8.0.1 and later.