Skip to content

Commit 694c11f

Browse files
committed
Update
1 parent 1380885 commit 694c11f

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

gazelle/python/resolve.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ func (*Resolver) Name() string { return languageName }
5555
// If nil is returned, the rule will not be indexed. If any non-nil slice is
5656
// returned, including an empty slice, the rule will be indexed.
5757
func (py *Resolver) Imports(c *config.Config, r *rule.Rule, f *rule.File) []resolve.ImportSpec {
58-
if r.Kind() == "py_binary" {
58+
// To avoid multiple labels indexing the same import,
59+
// check if there is a corresponding py_library rule with the same srcs.
60+
if r.Kind() == "py_binary" && !indexPyBinaryImport(r, f) {
5961
return nil
6062
}
6163
cfgs := c.Exts[languageName].(pythonconfig.Configs)
@@ -320,3 +322,22 @@ func convertDependencySetToExpr(set *treeset.Set) bzl.Expr {
320322
}
321323
return &bzl.ListExpr{List: deps}
322324
}
325+
326+
func indexPyBinaryImport(r *rule.Rule, f *rule.File) bool {
327+
pyBinarySrcs := r.AttrStrings("srcs")
328+
if len(pyBinarySrcs) == 0 {
329+
return false
330+
}
331+
for _, otherRule := range f.Rules {
332+
if otherRule.Kind() != "py_library" {
333+
continue
334+
}
335+
pyLibrarySrcs := otherRule.AttrStrings("srcs")
336+
for _, src := range pyLibrarySrcs {
337+
if src == pyBinarySrcs[0] {
338+
return false
339+
}
340+
}
341+
}
342+
return true
343+
}

gazelle/python/testdata/binary_without_entrypoint_per_file_generation/BUILD.out

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ py_library(
1919
name = "lib2",
2020
srcs = ["lib2.py"],
2121
visibility = ["//:__subpackages__"],
22-
deps = [":lib"],
22+
deps = [
23+
":lib",
24+
":lib_and_main",
25+
],
2326
)
2427

2528
py_binary(
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
import lib
2+
import lib_and_main

0 commit comments

Comments
 (0)