bazel-contrib / bazel-contrib/rules_python

Gazelle: Presence of `__main__.py` prevents other `*.py` files from being mapped as `py_binary` targets

未关闭
#3,337 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Starlark
星标
688
派生
721
平均合并
15 小时 7 分钟
30 天内合并 PR
76

描述

# 🐞 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"],
```

贡献指南

打开贡献指南

调研方向

首先从 gazelle/ 目录运行 bazel test //python:python_test_per_file,并使用 gazelle/python/testdata/per_file 及其预期输出 BUILD.out 作为 fixture。比较有无 __main__.py 时的输出,然后更新相关的 Gazelle 行为和测试期望,以便 my_binary.py 保持为 py_binary,并且也能正确生成 __main__.py 目标。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
build-system
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
55/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。