@@ -66,35 +66,6 @@ func matchesAnyGlob(s string, globs []string) bool {
66
66
return false
67
67
}
68
68
69
- // validateExistingRules checks existing Python rules in the BUILD file and return the rules with invalid srcs.
70
- func (py * Python ) validateExistingRules (args language.GenerateArgs ) (emptyRules []* rule.Rule ) {
71
- if args .File == nil {
72
- return
73
- }
74
- regularFiles := args .RegularFiles
75
- regularFilesMap := make (map [string ]struct {})
76
- for _ , file := range regularFiles {
77
- regularFilesMap [file ] = struct {}{}
78
- }
79
- for _ , existingRule := range args .File .Rules {
80
- if _ , ok := py .Kinds ()[existingRule .Kind ()]; ! ok {
81
- continue
82
- }
83
- allInvalidSrcs := true
84
- for _ , src := range existingRule .AttrStrings ("srcs" ) {
85
- if _ , ok := regularFilesMap [src ]; ok {
86
- allInvalidSrcs = false
87
- break
88
- }
89
- }
90
- // If all srcs are invalid, delete the rule.
91
- if allInvalidSrcs {
92
- emptyRules = append (emptyRules , newTargetBuilder (existingRule .Kind (), existingRule .Name (), args .Config .RepoRoot , args .Rel , nil ).build ())
93
- }
94
- }
95
- return emptyRules
96
- }
97
-
98
69
// GenerateRules extracts build metadata from source files in a directory.
99
70
// GenerateRules is called in each directory where an update is requested
100
71
// in depth-first post-order.
@@ -501,9 +472,8 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
501
472
result .Gen = append (result .Gen , pyTest )
502
473
result .Imports = append (result .Imports , pyTest .PrivateAttr (config .GazelleImportsKey ))
503
474
}
504
- // Validate existing rules have valid srcs
505
475
if ! cfg .CoarseGrainedGeneration () {
506
- emptyRules := py .validateExistingRules (args )
476
+ emptyRules := py .getRulesWithInvalidSrcs (args )
507
477
result .Empty = append (result .Empty , emptyRules ... )
508
478
}
509
479
if ! collisionErrors .Empty () {
@@ -517,6 +487,34 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
517
487
return result
518
488
}
519
489
490
+ // getRulesWithInvalidSrcs checks existing Python rules in the BUILD file and return the rules with invalid srcs.
491
+ func (py * Python ) getRulesWithInvalidSrcs (args language.GenerateArgs ) (invalidRules []* rule.Rule ) {
492
+ if args .File == nil {
493
+ return
494
+ }
495
+ regularFiles := args .RegularFiles
496
+ regularFilesMap := make (map [string ]struct {})
497
+ for _ , file := range regularFiles {
498
+ regularFilesMap [file ] = struct {}{}
499
+ }
500
+ for _ , existingRule := range args .File .Rules {
501
+ if _ , ok := py .Kinds ()[existingRule .Kind ()]; ! ok {
502
+ continue
503
+ }
504
+ allInvalidSrcs := true
505
+ for _ , src := range existingRule .AttrStrings ("srcs" ) {
506
+ if _ , ok := regularFilesMap [src ]; ok {
507
+ allInvalidSrcs = false
508
+ break
509
+ }
510
+ }
511
+ // If all srcs are invalid, delete the rule.
512
+ if allInvalidSrcs {
513
+ invalidRules = append (invalidRules , newTargetBuilder (existingRule .Kind (), existingRule .Name (), args .Config .RepoRoot , args .Rel , nil ).build ())
514
+ }
515
+ }
516
+ return invalidRules
517
+ }
520
518
// isBazelPackage determines if the directory is a Bazel package by probing for
521
519
// the existence of a known BUILD file name.
522
520
func isBazelPackage (dir string ) bool {
0 commit comments