bazel-contrib / bazel-contrib/rules_python

Have `python_binary_naming_convention` directive also apply to `if __name__ == "__main__":` files

Đang mở
#1,815 2 bình luận 4 reaction 0 người được giao Xem trên GitHub
Can Close? gazelle
Ngôn ngữ chính
Starlark
Star
688
Fork
721
Merge trung bình
15 giờ 7 phút
Pull request đã merge (30 ngày)
76

Mô tả

# 🚀 feature request

### Relevant Rules

+ gazelle

### Description

The `python_binary_naming_convention` directive currently only applies to binaries that are generated from `__main__.py` files.

`py_binary` targets that are generated from files that contain `if __name__ == "__main__":` are named after their source file. From the [README](https://github.com/bazelbuild/rules_python/blob/e86252ffd6d1a1bf32ae99933acc5ab49b78ec1e/gazelle/README.md#binaries) (emphasis mine):

> If `python_generation_mode` is set to `file`, then instead of one `py_binary` target per module, Gazelle will create one `py_binary` target for each file with such a line, and **the name of the target will match the name of the script**.

This results in name collisions when a single file is both a binary and a library and gazelle will report:

> gazelle: failed to generate target "//foobar" of kind "py_binary": a target of kind "py_library" with the same name already exists

The readme also says:

> Note that it's possible for another script to depend on a py_binary target and import from the py_binary's scripts. This can have possible negative effects on Bazel analysis time and runfiles size compared to depending on a py_library target. The simplest way to avoid these negative effects is to extract library code into a separate script without a main line. Gazelle will then create a py_library target for that library code, and other scripts can depend on that py_library target.

However, for existing projects that might not be feasible.

### Describe the solution you'd like

Have `python_binary_naming_convention` directive also apply to files that contain `if __name__ ...`.

Or add another directive that accomplishes such.

### Describe alternatives you've considered

Right now I'm just manually making all ~450 or so `py_binary` targets that our monorepo has :sob:

```starlark
# libs are generated by gazelle
py_library(
name = "foo",
srcs = ["foo.py"],
deps = [
":bar",
"//bin/bom:boo",
"@pypi:hello",
],
)

# binaries are manually added, using the lib as a dependency so that I don't have to keep `deps` in sync.
py_binary(
name = "foo_bin",
srcs = ["foo.py"],
deps = [":foo"],
)
```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu với chỉ thị `python_binary_naming_convention` của Gazelle và phần Binaries trong README của Gazelle, so sánh việc sinh từ `__main__.py` với các tệp chứa `if __name__ == "__main__":`. Được xem là hoàn tất khi quy ước này áp dụng cho cả hai trường hợp và ngăn chặn xung đột tên `py_library`/`py_binary` đã được báo cáo.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
build-system, tooling
Loại issue
Tính năng
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Sôi nổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
55/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.