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

ctx.expand_location() on py_binary expands to multiple files #20038

Closed
jacky8hyf opened this issue Nov 3, 2023 · 3 comments
Closed

ctx.expand_location() on py_binary expands to multiple files #20038

jacky8hyf opened this issue Nov 3, 2023 · 3 comments
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts type: bug

Comments

@jacky8hyf
Copy link

jacky8hyf commented Nov 3, 2023

Description of the bug:

When one runs ctx.expand_location() on the expression $(locations <a py_binary>) for a custom rule, multiple files are returned.

However, this is not the case for genrule's.

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.

BUILD

load(":myrule.bzl", "myrule")

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

genrule(
    name = "gen",
    tools = [":a"],
    outs = ["gen.txt"],
    cmd = "echo $(locations :a); : > $@",
)

myrule(
    name = "mytarget",
    deps = [
        ":a",
    ],
    value = "$(locations :a)",
)

myrule.bzl

def _impl(ctx):
    print(ctx.expand_location(ctx.attr.value, ctx.attr.deps))

myrule = rule(
    implementation = _impl,
    attrs = {
        "deps": attr.label_list(),
        "value": attr.string(),
    },
)

And create an empty WORKSPACE and a.py file.

With the above setup:

  • If you run bazel build :gen, it will print bazel-out/k8-opt-exec-2B5CBBC6/bin/a, indicating that $(locations :a) correctly resolved to the generated binary.
  • If you run bazel build :mytarget, it will print ./a.py bazel-out/k8-fastbuild/bin/a, indicating that $(locations :a) resolves to multiple files.

I tried turning tools into deps for my :gen target. Then, bazel build :gen also prints two paths.

I tried adding cfg = "exec" to deps's attr.label_list, but the result is the same.

This makes me unable to resolve to the correct binary to execute without some hacky code (e.g. CL)

How do I call ctx.expand_location so it produces the same output like genrule.tools does?

Which operating system are you running Bazel on?

Linux

What is the output of bazel info release?

release 6.4.0

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 master; git rev-parse HEAD ?

No response

Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

I don't think so.

Have you found anything relevant by searching the web?

https://stackoverflow.com/questions/57497270/bazel-how-to-run-py-binary-to-produce-input-files-before-testing

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

No response

@pcjanzen
Copy link
Contributor

pcjanzen commented Nov 3, 2023

related: #16381

@rakshitasingh05 rakshitasingh05 added the team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts label Nov 3, 2023
@comius comius added P3 We're not considering working on this, but happy to review a PR. (No assignee) and removed untriaged labels Nov 3, 2023
@jacky8hyf
Copy link
Author

@xich
Copy link

xich commented Sep 18, 2024

It looks like the fix was rolled back: ab71a10

Is there any resolution to this? Even if the the locations variants gave the ".py" and the executable back in a stable order, that would help. Right now I'm filtering the list.

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
P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants