@@ -32,7 +32,6 @@ import (
3232 "github.com/emirpasic/gods/sets/treeset"
3333 godsutils "github.com/emirpasic/gods/utils"
3434
35-
3635 "github.com/bazel-contrib/rules_python/gazelle/pythonconfig"
3736)
3837
@@ -112,7 +111,10 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
112111 hasConftestFile := false
113112
114113 testFileGlobs := cfg .TestFilePattern ()
115-
114+ filteredFiles := make (map [string ]struct {})
115+ for _ , f := range args .RegularFiles {
116+ filteredFiles [f ] = struct {}{}
117+ }
116118 for _ , f := range args .RegularFiles {
117119 if cfg .IgnoresFile (filepath .Base (f )) {
118120 continue
@@ -474,7 +476,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
474476 result .Imports = append (result .Imports , pyTest .PrivateAttr (config .GazelleImportsKey ))
475477 }
476478 if ! cfg .CoarseGrainedGeneration () {
477- emptyRules := py .getRulesWithInvalidSrcs (args )
479+ emptyRules := py .getRulesWithInvalidSrcs (cfg , args )
478480 result .Empty = append (result .Empty , emptyRules ... )
479481 }
480482 if ! collisionErrors .Empty () {
@@ -488,13 +490,17 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
488490 return result
489491}
490492
491- // getRulesWithInvalidSrcs checks existing Python rules in the BUILD file and return the rules with invalid srcs.
492- func (py * Python ) getRulesWithInvalidSrcs (args language.GenerateArgs ) (invalidRules []* rule.Rule ) {
493+ // getRulesWithInvalidSrcs checks existing Python rules in the BUILD file and return the rules with invalid source files.
494+ // Invalid source files are files that do not exist or not a target.
495+ func (py * Python ) getRulesWithInvalidSrcs (cfg * pythonconfig.Config , args language.GenerateArgs ) (invalidRules []* rule.Rule ) {
493496 if args .File == nil {
494497 return
495498 }
496499 filesMap := make (map [string ]struct {})
497500 for _ , file := range args .RegularFiles {
501+ if cfg .IgnoresFile (filepath .Base (file )) {
502+ continue
503+ }
498504 filesMap [file ] = struct {}{}
499505 }
500506 for _ , file := range args .GenFiles {
@@ -508,18 +514,18 @@ func (py *Python) getRulesWithInvalidSrcs(args language.GenerateArgs) (invalidRu
508514 if existingRule .Kind () != pyBinaryKind {
509515 continue
510516 }
511- allInvalidSrcs := true
517+ hasValidSrcs := true
512518 for _ , src := range existingRule .AttrStrings ("srcs" ) {
513- if _ , ok := filesMap [src ]; ok {
514- allInvalidSrcs = false
515- break
516- }
517519 if isTarget (src ) {
518- allInvalidSrcs = false
519- break
520+ continue
521+ }
522+ if _ , ok := filesMap [src ]; ok {
523+ continue
520524 }
525+ hasValidSrcs = false
526+ break
521527 }
522- if allInvalidSrcs {
528+ if ! hasValidSrcs {
523529 invalidRules = append (invalidRules , newTargetBuilder (existingRule .Kind (), existingRule .Name (), args .Config .RepoRoot , args .Rel , nil ).build ())
524530 }
525531 }
0 commit comments