@@ -55,7 +55,9 @@ func (*Resolver) Name() string { return languageName }
55
55
// If nil is returned, the rule will not be indexed. If any non-nil slice is
56
56
// returned, including an empty slice, the rule will be indexed.
57
57
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 ) {
59
61
return nil
60
62
}
61
63
cfgs := c .Exts [languageName ].(pythonconfig.Configs )
@@ -320,3 +322,22 @@ func convertDependencySetToExpr(set *treeset.Set) bzl.Expr {
320
322
}
321
323
return & bzl.ListExpr {List : deps }
322
324
}
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
+ }
0 commit comments