bazel-contrib / bazel-contrib/rules_python

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

Ouverte
#3,337 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
Langage dominant
Starlark
Étoiles
688
Forks
721
Merge moyen
15 h 7 min
PR mergées (30 j)
76

Description

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

Guide de contribution

Ouvrir le guide de contribution

Piste de recherche

Commencez par exécuter bazel test //python:python_test_per_file depuis le répertoire gazelle/, en utilisant gazelle/python/testdata/per_file et sa sortie attendue BUILD.out comme fixture. Comparez la sortie avec et sans __main__.py, puis mettez à jour le comportement pertinent de Gazelle et l’attendu du test afin que my_binary.py reste un py_binary et que la cible __main__.py soit également générée correctement.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
build-system
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Clairement spécifiée
Accessibilité débutants
55/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.