rules_python icon indicating copy to clipboard operation
rules_python copied to clipboard

`python_generation_mode project` generating unnecesary/empty `py_test`

Open kpark-hrp opened this issue 2 years ago • 1 comments

🐞 bug report

Affected Rule

Gazelle with # gazelle:python_generation_mode project

The issue is caused by the rule:

Is this a regression?

Yes, the previous version in which this bug was not present was: 0.26.0

Description

A clear and concise description of the problem...

Since 0.27.0, having # gazelle:python_generation_mode project Gazelle directive is generating an empty py_test despite __test__.py not existing.

I believe it is related this change: https://github.com/bazelbuild/rules_python/pull/1538

For example,

py_test(
    name = "folder_test",
    imports = ["../.."],
    main = "__test__.py",
)

There isn't even a srcs parameter, so it doesn't even work.

🔬 Minimal Reproduction

  • A directory with Python files and a BUILD.bazel
    • No test files
    • No __test__.py
  • Add # gazelle:python_generation_mode project directive
  • Run Python Gazelle on that directory

🌍 Your Environment

  • Replicated on both MacOS and Ubuntu Linux

kpark-hrp avatar Jan 12 '24 19:01 kpark-hrp

We are currently working around this with this patch:

--- python/generate.go
+++ python/generate.go
@@ -375,7 +375,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
 			addModuleDependencies(deps).
 			generateImportsAttribute()
 	}
-	if (hasPyTestEntryPointFile || hasPyTestEntryPointTarget || cfg.CoarseGrainedGeneration()) && !cfg.PerFileGeneration() {
+	if (hasPyTestEntryPointFile || hasPyTestEntryPointTarget) && !cfg.PerFileGeneration() {
 		// Create one py_test target per package
 		if hasPyTestEntryPointFile {
 			// Only add the pyTestEntrypointFilename to the pyTestFilenames if

Would be great to have a better fix though.

michaelboyd2 avatar Jan 26 '24 10:01 michaelboyd2

Fixed by #1809

aignas avatar Apr 01 '24 02:04 aignas