Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Location expansion with some APIs differ from others #23200

Closed
keith opened this issue Aug 2, 2024 · 0 comments
Closed

Location expansion with some APIs differ from others #23200

keith opened this issue Aug 2, 2024 · 0 comments
Labels

Comments

@keith
Copy link
Member

keith commented Aug 2, 2024

Description of the bug:

When you have a simple py_binary, that you use in a genrule:

py_binary(
    name = "py_binary",
    srcs = ["py_binary.py"],
)
genrule(
    name = "genrule",
    outs = ["genrule.txt"],
    cmd = "$(location :py_binary) $(OUTS) from-genrule",
    tools = [":py_binary"],
)

This works fine. But if you try to create your own rule that handles something similar, the ctx.expand_location API results in this error:

BUILD.bazel:23:15: in broken_genrule rule //:custom_broken_genrule: label '//:py_binary' in $(location) expression expands to more than one file, please use $(locations //:py_binary) instead.  Files (at most 5 shown) are: [./py_binary.py, bazel-out/darwin_arm64-fastbuild/bin/py_binary]

Using the ctx.resolve_command API does seem to solve this, but it returns an array as the command, which might not be preferred, and requires post-processing any escaped $ signs.

I tried passing specific files to expand_location, but it seems to require a Target not any files, so it doesn't seem like something I can filter to only the inputs I care about.

Which category does this issue belong to?

No response

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

bazel build :all in repro.zip

Which operating system are you running Bazel on?

No response

What is the output of bazel info release?

7.x

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse HEAD ?

No response

If this is a regression, please try to identify the Bazel commit where the bug was introduced with bazelisk --bisect.

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

@satyanandak satyanandak added the team-Rules-Python Native rules for Python label Aug 5, 2024
fmeum added a commit to fmeum/bazel that referenced this issue Jan 9, 2025
Work towards bazelbuild#11820
Fixes bazelbuild#20038
Fixes bazelbuild#23200
Fixes bazelbuild#24613

RELNOTES: Extra targets provided to `ctx.expand_location` now expand to their executable (if any) instead of resulting in an error if they provide a number of files different from one.

RELNOTES[INC]: The `--incompatible_locations_prefers_executable` flag has been added and enabled, which makes it so that `ctx.expand_location` expands `$(locations :x)` to the executable of an extra target `:x` if it provides one and the number of files provided by it is not one.

Closes bazelbuild#24690.

PiperOrigin-RevId: 713453768
Change-Id: I0d6e052bc70deea029554ab722feb544f9597a23
(cherry picked from commit 457d248)
github-merge-queue bot pushed a commit that referenced this issue Jan 9, 2025
…24874)

Work towards #11820
Fixes #20038
Fixes #23200
Fixes #24613

RELNOTES: Extra targets provided to `ctx.expand_location` now expand to
their executable (if any) instead of resulting in an error if they
provide a number of files different from one.

RELNOTES[INC]: The `--incompatible_locations_prefers_executable` flag
has been added and enabled, which makes it so that `ctx.expand_location`
expands `$(locations :x)` to the executable of an extra target `:x` if
it provides one and the number of files provided by it is not one.

Closes #24690.

PiperOrigin-RevId: 713453768
Change-Id: I0d6e052bc70deea029554ab722feb544f9597a23 
(cherry picked from commit 457d248)

Fixes #24646
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants