Skip to content

Commit 1884aac

Browse files
committed
fix 3497
1 parent 89d00af commit 1884aac

4 files changed

Lines changed: 30 additions & 9 deletions

File tree

gazelle/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/bazel-contrib/rules_python/gazelle
22

3-
go 1.21.13
3+
go 1.22.0
44

55
require (
66
github.com/bazelbuild/bazel-gazelle v0.36.0

gazelle/python/generate.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,24 @@ func matchesAnyGlob(s string, globs []string) bool {
6666
return false
6767
}
6868

69+
// findConftestPaths returns package paths containing conftest.py, from currentPkg
70+
// up through ancestors, stopping at pythonProjectRoot (or repo root if empty).
71+
func findConftestPaths(repoRoot, currentPkg, pythonProjectRoot string) []string {
72+
var result []string
73+
for pkg := currentPkg; ; pkg = filepath.Dir(pkg) {
74+
if pkg == "." {
75+
pkg = ""
76+
}
77+
if _, err := os.Stat(filepath.Join(repoRoot, pkg, conftestFilename)); err == nil {
78+
result = append(result, pkg)
79+
}
80+
if pkg == "" || pkg == pythonProjectRoot {
81+
break
82+
}
83+
}
84+
return result
85+
}
86+
6987
// GenerateRules extracts build metadata from source files in a directory.
7088
// GenerateRules is called in each directory where an update is requested
7189
// in depth-first post-order.
@@ -481,14 +499,15 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
481499
}
482500

483501
for _, pyTestTarget := range pyTestTargets {
484-
if conftest != nil {
485-
conftestModule := Module{Name: importSpecFromSrc(pythonProjectRoot, args.Rel, conftestFilename).Imp}
486-
if pyTestTarget.annotations.includePytestConftest == nil {
487-
// unset; default behavior
488-
pyTestTarget.addModuleDependency(conftestModule)
489-
} else if *pyTestTarget.annotations.includePytestConftest {
490-
// set; add if true, do not add if false
491-
pyTestTarget.addModuleDependency(conftestModule)
502+
shouldAddConftest := pyTestTarget.annotations.includePytestConftest == nil ||
503+
*pyTestTarget.annotations.includePytestConftest
504+
505+
if shouldAddConftest {
506+
for _, conftestPkg := range findConftestPaths(args.Config.RepoRoot, args.Rel, pythonProjectRoot) {
507+
conftestModule := Module{
508+
Name: importSpecFromSrc(pythonProjectRoot, conftestPkg, conftestFilename).Imp,
509+
}
510+
pyTestTarget.deps.Add(conftestModule)
492511
}
493512
}
494513
pyTest := pyTestTarget.build()

gazelle/python/testdata/simple_test_with_conftest/bar/BUILD.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ py_test(
2323
deps = [
2424
":bar",
2525
":conftest",
26+
"//:conftest",
2627
],
2728
)

gazelle/python/testdata/simple_test_with_conftest_sibling_imports_disabled/bar/BUILD.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ py_test(
2222
main = "__test__.py",
2323
deps = [
2424
":conftest",
25+
"//:conftest",
2526
"//:simple_test_with_conftest_sibling_imports_disabled",
2627
],
2728
)

0 commit comments

Comments
 (0)