bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
23.21k stars 4.07k forks source link

cquery --output=files returns _both_ src _and_ output file for py_binary #17703

Open illicitonion opened 1 year ago

illicitonion commented 1 year ago

Description of the bug:

For this BUILD file:

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

This query produces both the source file and the output file:

% bazel cquery --output=files :pyb 2>/dev/null
pyb.py
bazel-out/darwin-fastbuild/bin/pyb

In this case, it feels like just the generated file should be output, not the source file?

(This was the behaviour in Bazel 5, and I'm aware it was changed at my request in https://github.com/bazelbuild/bazel/pull/16602)

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

% touch WORKSPACE
% echo 6.1.0 > .bazelversion
% cat <<EOF >BUILD.bazel
py_binary(
    name = "pyb",
    srcs = ["pyb.py"],
)
EOF
% cat <<EOF >pyb.py
print("Hello")
EOF
% bazel cquery --output=files :pyb 2>/dev/null
pyb.py
bazel-out/darwin-fastbuild/bin/pyb

Expect that last command to instead act like this:

% bazel cquery --output=files :pyb 2>/dev/null
bazel-out/darwin-fastbuild/bin/pyb

Which operating system are you running Bazel on?

macOS

What is the output of bazel info release?

release 6.1.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

Have you found anything relevant by searching the web?

No response

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

No response

illicitonion commented 1 year ago

cc @fmeum - sorry for not testing that PR thoroughly enough before...

fmeum commented 1 year ago

I think that this is a quirk in the Python rules rather than in the implementation of --output=files:

$ bazel cquery --output=starlark :pyb '--starlark:expr=providers(target)["FileProvider"].files_to_build' 2>/dev/null
depset([<source file pyb.py>, <generated file pyb>])

https://github.com/bazelbuild/bazel/blob/daa3dbe22adb03338c75b53ea97954c9434099b4/src/main/starlark/builtins_bzl/common/python/py_executable.bzl#L195-L197 seems related. @rickeylev, can you say more about this?

rickeylev commented 1 year ago

Yeah, it's a undesirable quirk. It's been there a long time. I was never able to track down when it came about or why; my best guess is it is some holdover from a much earlier phase in Bazel's life when what a rule produced wasn't as well defined or controllable.