From 1884aace7989ce47fbf088d9ee76c8f22d6b2807 Mon Sep 17 00:00:00 2001 From: Joshua Cannon Date: Tue, 6 Jan 2026 20:21:50 +0000 Subject: [PATCH 1/6] fix 3497 --- gazelle/go.mod | 2 +- gazelle/python/generate.go | 35 ++++++++++++++----- .../simple_test_with_conftest/bar/BUILD.out | 1 + .../bar/BUILD.out | 1 + 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/gazelle/go.mod b/gazelle/go.mod index 7623079af9..96637dfc3b 100644 --- a/gazelle/go.mod +++ b/gazelle/go.mod @@ -1,6 +1,6 @@ module github.com/bazel-contrib/rules_python/gazelle -go 1.21.13 +go 1.22.0 require ( github.com/bazelbuild/bazel-gazelle v0.36.0 diff --git a/gazelle/python/generate.go b/gazelle/python/generate.go index cbceea4693..49e9921fa9 100644 --- a/gazelle/python/generate.go +++ b/gazelle/python/generate.go @@ -66,6 +66,24 @@ func matchesAnyGlob(s string, globs []string) bool { return false } +// findConftestPaths returns package paths containing conftest.py, from currentPkg +// up through ancestors, stopping at pythonProjectRoot (or repo root if empty). +func findConftestPaths(repoRoot, currentPkg, pythonProjectRoot string) []string { + var result []string + for pkg := currentPkg; ; pkg = filepath.Dir(pkg) { + if pkg == "." { + pkg = "" + } + if _, err := os.Stat(filepath.Join(repoRoot, pkg, conftestFilename)); err == nil { + result = append(result, pkg) + } + if pkg == "" || pkg == pythonProjectRoot { + break + } + } + return result +} + // GenerateRules extracts build metadata from source files in a directory. // GenerateRules is called in each directory where an update is requested // in depth-first post-order. @@ -481,14 +499,15 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes } for _, pyTestTarget := range pyTestTargets { - if conftest != nil { - conftestModule := Module{Name: importSpecFromSrc(pythonProjectRoot, args.Rel, conftestFilename).Imp} - if pyTestTarget.annotations.includePytestConftest == nil { - // unset; default behavior - pyTestTarget.addModuleDependency(conftestModule) - } else if *pyTestTarget.annotations.includePytestConftest { - // set; add if true, do not add if false - pyTestTarget.addModuleDependency(conftestModule) + shouldAddConftest := pyTestTarget.annotations.includePytestConftest == nil || + *pyTestTarget.annotations.includePytestConftest + + if shouldAddConftest { + for _, conftestPkg := range findConftestPaths(args.Config.RepoRoot, args.Rel, pythonProjectRoot) { + conftestModule := Module{ + Name: importSpecFromSrc(pythonProjectRoot, conftestPkg, conftestFilename).Imp, + } + pyTestTarget.deps.Add(conftestModule) } } pyTest := pyTestTarget.build() diff --git a/gazelle/python/testdata/simple_test_with_conftest/bar/BUILD.out b/gazelle/python/testdata/simple_test_with_conftest/bar/BUILD.out index 4a1204e989..9b500d4733 100644 --- a/gazelle/python/testdata/simple_test_with_conftest/bar/BUILD.out +++ b/gazelle/python/testdata/simple_test_with_conftest/bar/BUILD.out @@ -23,5 +23,6 @@ py_test( deps = [ ":bar", ":conftest", + "//:conftest", ], ) diff --git a/gazelle/python/testdata/simple_test_with_conftest_sibling_imports_disabled/bar/BUILD.out b/gazelle/python/testdata/simple_test_with_conftest_sibling_imports_disabled/bar/BUILD.out index ef8591f199..25a2d39672 100644 --- a/gazelle/python/testdata/simple_test_with_conftest_sibling_imports_disabled/bar/BUILD.out +++ b/gazelle/python/testdata/simple_test_with_conftest_sibling_imports_disabled/bar/BUILD.out @@ -22,6 +22,7 @@ py_test( main = "__test__.py", deps = [ ":conftest", + "//:conftest", "//:simple_test_with_conftest_sibling_imports_disabled", ], ) From cf87049ef3eb131f6b626b82351cb34937994418 Mon Sep 17 00:00:00 2001 From: Joshua Cannon Date: Tue, 6 Jan 2026 20:23:01 +0000 Subject: [PATCH 2/6] put this back --- gazelle/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gazelle/go.mod b/gazelle/go.mod index 96637dfc3b..7623079af9 100644 --- a/gazelle/go.mod +++ b/gazelle/go.mod @@ -1,6 +1,6 @@ module github.com/bazel-contrib/rules_python/gazelle -go 1.22.0 +go 1.21.13 require ( github.com/bazelbuild/bazel-gazelle v0.36.0 From 3faabbf72ab048847a266203237f74e0edc0c7dd Mon Sep 17 00:00:00 2001 From: Joshua Cannon Date: Tue, 6 Jan 2026 20:29:11 +0000 Subject: [PATCH 3/6] changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0df0de8cee..b0df94c037 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,8 @@ END_UNRELEASED_TEMPLATE ### Fixed * (tests) No more coverage warnings are being printed if there are no sources. ([#2762](https://github.com/bazel-contrib/rules_python/issues/2762)) +* (gazelle) Ancestor `conftest.py` files are added in addition to sibling `conftest.py`. + ([#3497](https://github.com/bazel-contrib/rules_python/issues/3497)) {#v0-0-0-added} ### Added From 8746171a11b8a7ab77f9d2be10bf348b205a2dea Mon Sep 17 00:00:00 2001 From: Joshua Cannon Date: Tue, 13 Jan 2026 17:04:38 +0000 Subject: [PATCH 4/6] switch back to addModuleDependency --- gazelle/python/generate.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/gazelle/python/generate.go b/gazelle/python/generate.go index 49e9921fa9..54d03f26fa 100644 --- a/gazelle/python/generate.go +++ b/gazelle/python/generate.go @@ -504,10 +504,12 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes if shouldAddConftest { for _, conftestPkg := range findConftestPaths(args.Config.RepoRoot, args.Rel, pythonProjectRoot) { - conftestModule := Module{ - Name: importSpecFromSrc(pythonProjectRoot, conftestPkg, conftestFilename).Imp, - } - pyTestTarget.deps.Add(conftestModule) + pyTestTarget.addModuleDependency( + Module{ + Name: importSpecFromSrc(pythonProjectRoot, conftestPkg, conftestFilename).Imp, + Filepath: filepath.Join(conftestPkg, conftestFilename) + }, + ) } } pyTest := pyTestTarget.build() From dc9bd19d086352bf1e3b7fc47d749a8ebccb48ee Mon Sep 17 00:00:00 2001 From: Joshua Cannon Date: Tue, 13 Jan 2026 17:05:13 +0000 Subject: [PATCH 5/6] stopping --- gazelle/python/generate.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gazelle/python/generate.go b/gazelle/python/generate.go index 54d03f26fa..c0a59bfd05 100644 --- a/gazelle/python/generate.go +++ b/gazelle/python/generate.go @@ -67,7 +67,7 @@ func matchesAnyGlob(s string, globs []string) bool { } // findConftestPaths returns package paths containing conftest.py, from currentPkg -// up through ancestors, stopping at pythonProjectRoot (or repo root if empty). +// up through ancestors, stopping at module root. func findConftestPaths(repoRoot, currentPkg, pythonProjectRoot string) []string { var result []string for pkg := currentPkg; ; pkg = filepath.Dir(pkg) { @@ -77,7 +77,7 @@ func findConftestPaths(repoRoot, currentPkg, pythonProjectRoot string) []string if _, err := os.Stat(filepath.Join(repoRoot, pkg, conftestFilename)); err == nil { result = append(result, pkg) } - if pkg == "" || pkg == pythonProjectRoot { + if pkg == "" { break } } From d92ed4765543b8cd67549604ef7516fe9277f3a5 Mon Sep 17 00:00:00 2001 From: Joshua Cannon Date: Tue, 13 Jan 2026 17:05:36 +0000 Subject: [PATCH 6/6] comma --- gazelle/python/generate.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gazelle/python/generate.go b/gazelle/python/generate.go index c0a59bfd05..90216b00bc 100644 --- a/gazelle/python/generate.go +++ b/gazelle/python/generate.go @@ -507,7 +507,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes pyTestTarget.addModuleDependency( Module{ Name: importSpecFromSrc(pythonProjectRoot, conftestPkg, conftestFilename).Imp, - Filepath: filepath.Join(conftestPkg, conftestFilename) + Filepath: filepath.Join(conftestPkg, conftestFilename), }, ) }