bazel-contrib / bazel-contrib/rules_python
Gazelle: Presence of `__main__.py` prevents other `*.py` files from being mapped as `py_binary` targets
- 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ả
# 🐞 bug report
### Affected Rule
Gazelle
### Is this a regression?
Still checking
### Description
Presence of `__main__.py` prevents other `*.py` files from being mapped as `py_binary` targets.
## 🔬 Minimal Reproduction
Add one file to `gazelle/python/testdata/per_file`:
```python
# gazelle/python/testdata/per_file/my_binary.py
if __name__ == "__main__":
print("hey")
```
Run the test (from the `gazelle/` dir) `bazel test //python:python_test_per_file` and see that a `py_binary` target is added. So far so good!
Now add a `__main__.py` file, no content needed: `touch gazelle/python/testdata/per_file/__main__.py`
And run the test again. You'll see that `my_binary.py` was incorrectly added as a `py_library` target.
## Other
This is the "correct" `BUILD.out` file:
```diff
--- a/gazelle/python/testdata/per_file/BUILD.out
+++ b/gazelle/python/testdata/per_file/BUILD.out
@@ -1,4 +1,4 @@
-load("@rules_python//python:defs.bzl", "py_library", "py_test")
+load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
# gazelle:python_generation_mode file
@@ -23,6 +23,19 @@ py_library(
deps = [":custom"],
)
+py_binary(
+ name = "my_binary",
+ srcs = ["my_binary.py"],
+ visibility = ["//:__subpackages__"],
+)
+
+py_binary(
+ name = "per_file_bin",
+ srcs = ["__main__.py"],
+ main = "__main__.py",
+ visibility = ["//:__subpackages__"],
+)
+
py_test(
name = "bar_test",
srcs = ["bar_test.py"],
```
Hướng dẫn đóng góp
Hướng nghiên cứu
Bắt đầu bằng cách chạy bazel test //python:python_test_per_file từ thư mục gazelle/, sử dụng gazelle/python/testdata/per_file và đầu ra mong đợi BUILD.out của nó làm fixture. So sánh đầu ra khi có và không có __main__.py, sau đó cập nhật hành vi Gazelle liên quan và kỳ vọng của bài kiểm thử để my_binary.py vẫn là một py_binary và target __main__.py cũng được tạo chính xác.
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
- Loại issue
- Lỗi
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 55/100