Skip to content

Commit 3744e4d

Browse files
authored
fix(gazelle): generate empty py_library only when need (bazel-contrib#1905)
bazel-contrib#1887 incorrectly generated an empty py_library for all dirs, although it will eventually be removed because it is empty, but it will easily conflict with other existing rules. Fix this error, and only generate an empty py_library for bazel packages with the same name py_library rule.
1 parent 781935f commit 3744e4d

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

gazelle/python/generate.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ import (
2727
"github.com/bazelbuild/bazel-gazelle/label"
2828
"github.com/bazelbuild/bazel-gazelle/language"
2929
"github.com/bazelbuild/bazel-gazelle/rule"
30-
"github.com/bazelbuild/rules_python/gazelle/pythonconfig"
3130
"github.com/bmatcuk/doublestar/v4"
3231
"github.com/emirpasic/gods/lists/singlylinkedlist"
3332
"github.com/emirpasic/gods/sets/treeset"
3433
godsutils "github.com/emirpasic/gods/utils"
34+
35+
"github.com/bazelbuild/rules_python/gazelle/pythonconfig"
3536
)
3637

3738
const (
@@ -270,6 +271,23 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
270271
}
271272
}
272273

274+
// If we're doing per-file generation, srcs could be empty at this point, meaning we shouldn't make a py_library.
275+
// If there is already a package named py_library target before, we should generate an empty py_library.
276+
if srcs.Empty() {
277+
if args.File == nil {
278+
return
279+
}
280+
generateEmptyLibrary := false
281+
for _, r := range args.File.Rules {
282+
if r.Kind() == actualPyLibraryKind && r.Name() == pyLibraryTargetName {
283+
generateEmptyLibrary = true
284+
}
285+
}
286+
if !generateEmptyLibrary {
287+
return
288+
}
289+
}
290+
273291
// Check if a target with the same name we are generating already
274292
// exists, and if it is of a different kind from the one we are
275293
// generating. If so, we have to throw an error since Gazelle won't
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
genrule(
2+
name = "others", # same to directory name
3+
outs = ["data.txt"],
4+
cmd = "echo foo bar baz > $@",
5+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
genrule(
2+
name = "others", # same to directory name
3+
outs = ["data.txt"],
4+
cmd = "echo foo bar baz > $@",
5+
)

0 commit comments

Comments
 (0)