fix: Gazelle bug with merging py_binary targets in per-file mode and partial update#2619
Conversation
|
Is there an Issue associated with this? We have been running the same setup you describe for quite some time now and haven't see any issues related to Gazelle-generated |
|
There isn't an issue, but I'd be happy to open one if you like. I thought a PR with a testcase would be more helpful, because you can see the failing test artifact. Instead of the expected 2 Also want to add how much our team appreciates the Gazelle tool for python. We have quite a few members who aren't as experienced with Bazel, and this was one of the main reasons we felt comfortable adopting Bazel. |
|
Ah yes, very interesting! Gazelle is merging the two rules, generating py_binary(
name = "a",
srcs = ["b.py"],
visibility = ["//:__subpackages__"],
)where the name is I'm able to reproduce this both with your branch and separately 👍. It looks like Gazelle is correctly making the two pyBinary targets at this line (confirmation: injecting some logging prints both the Additionally, the returned So I wonder if it's something outside of our ( |
|
@dougthor42 I came to the same conclusion as you, The problem is here: https://github.com/bazelbuild/rules_python/blob/main/gazelle/python/kinds.go#L35 The documentation for this field confirms that this causes targets of this kind to be matched together, which must be doing some sort of merge operation. Changing this to |
dougthor42
left a comment
There was a problem hiding this comment.
Thanks for this! Things mostly LGTM, just a couple of nits.
gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/test.yaml
Outdated
Show resolved
Hide resolved
gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/a.py
Outdated
Show resolved
Hide resolved
|
Thanks for the fix! |
This PR adds a new unit test. Currently, this is just a failing test without a fix, and I am still trying to understand the code well enough to find the root cause of the issue.
Our team uses Python+Gazelle in a monorepo, and we have a handful of directories with multiple
.pyfiles containingif __name__ == "__main__". Most of the time these are present for convenience or ad-hoc invocation. We're aware of the recommendation to split these into separate files, but that can cause clutter, and it is non-obvious to most engineers what to do when encountering this issue, which presents either as a misleading error message or a no-op without creating the appropriate targets.Update
This bug occurs when ALL of the following are true:
python_generation_modeis set tofile.if __name__ == "__main__") exist in the same directory.__main__.pyfile.BUILDfile in the directory is partially complete, i.e. it containspy_binarytargets for some of the python files, but not others.In this situation, previously absent
py_binarytargets are merged into existingpy_binarytargets instead of being created as new targets.