rules_py icon indicating copy to clipboard operation
rules_py copied to clipboard

[Bug]: py_binary expression expands to more than one file

Open keith opened this issue 1 year ago • 5 comments

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

keith avatar Dec 02 '24 22:12 keith

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.

mattem avatar Dec 05 '24 18:12 mattem

Will removing those extra files not break some other workflow?

keith avatar Dec 05 '24 19:12 keith

here's that change if not: https://github.com/aspect-build/rules_py/pull/467

keith avatar Dec 05 '24 20:12 keith

maybe https://github.com/bazelbuild/bazel/pull/24690 is the better solve here

keith avatar Jan 13 '25 21:01 keith

Yes, this should be fixed in Bazel. I wanted to send a patch myself, but never got time.

thesayyn avatar Jan 21 '25 18:01 thesayyn

This appears to have been resolved by https://github.com/bazelbuild/bazel/pull/24690, available in 8.0.1 and later.

arrdem avatar Jun 09 '25 21:06 arrdem