@@ -231,6 +231,8 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
231231 }
232232
233233 collisionErrors := singlylinkedlist .New ()
234+ // Create a validFilesMap of mainModules to validate if python macros have valid srcs.
235+ validFilesMap := make (map [string ]struct {})
234236
235237 appendPyLibrary := func (srcs * treeset.Set , pyLibraryTargetName string ) {
236238 allDeps , mainModules , annotations , err := parser .parse (srcs )
@@ -243,6 +245,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
243245 mainFileNames := make ([]string , 0 , len (mainModules ))
244246 for name := range mainModules {
245247 mainFileNames = append (mainFileNames , name )
248+ validFilesMap [name ] = struct {}{}
246249
247250 // Remove the file from srcs if we're doing per-file library generation so
248251 // that we don't also generate a py_library target for it.
@@ -490,10 +493,8 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
490493 result .Gen = append (result .Gen , pyTest )
491494 result .Imports = append (result .Imports , pyTest .PrivateAttr (config .GazelleImportsKey ))
492495 }
493- if ! cfg .CoarseGrainedGeneration () {
494- emptyRules := py .getRulesWithInvalidSrcs (cfg , args )
495- result .Empty = append (result .Empty , emptyRules ... )
496- }
496+ emptyRules := py .getRulesWithInvalidSrcs (args , validFilesMap )
497+ result .Empty = append (result .Empty , emptyRules ... )
497498 if ! collisionErrors .Empty () {
498499 it := collisionErrors .Iterator ()
499500 for it .Next () {
@@ -507,19 +508,12 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
507508
508509// getRulesWithInvalidSrcs checks existing Python rules in the BUILD file and return the rules with invalid source files.
509510// Invalid source files are files that do not exist or not a target.
510- func (py * Python ) getRulesWithInvalidSrcs (cfg * pythonconfig. Config , args language.GenerateArgs ) (invalidRules []* rule.Rule ) {
511+ func (py * Python ) getRulesWithInvalidSrcs (args language.GenerateArgs , validFilesMap map [ string ] struct {} ) (invalidRules []* rule.Rule ) {
511512 if args .File == nil {
512513 return
513514 }
514- filesMap := make (map [string ]struct {})
515- for _ , file := range args .RegularFiles {
516- if cfg .IgnoresFile (filepath .Base (file )) {
517- continue
518- }
519- filesMap [file ] = struct {}{}
520- }
521515 for _ , file := range args .GenFiles {
522- filesMap [file ] = struct {}{}
516+ validFilesMap [file ] = struct {}{}
523517 }
524518
525519 isTarget := func (src string ) bool {
@@ -535,13 +529,13 @@ func (py *Python) getRulesWithInvalidSrcs(cfg *pythonconfig.Config, args languag
535529 hasValidSrcs = true
536530 break
537531 }
538- if _ , ok := filesMap [src ]; ok {
532+ if _ , ok := validFilesMap [src ]; ok {
539533 hasValidSrcs = true
540534 break
541535 }
542536 }
543537 if ! hasValidSrcs {
544- invalidRules = append (invalidRules , newTargetBuilder (existingRule .Kind (), existingRule .Name (), args .Config .RepoRoot , args .Rel , nil ).build ())
538+ invalidRules = append (invalidRules , newTargetBuilder (existingRule .Kind (), existingRule .Name (), args .Config .RepoRoot , args .Rel , nil , false ).build ())
545539 }
546540 }
547541 return invalidRules
0 commit comments