Skip to content

Commit cf13719

Browse files
committed
Update with invalid sources
1 parent 756cb2c commit cf13719

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

gazelle/python/generate.go

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -66,35 +66,6 @@ func matchesAnyGlob(s string, globs []string) bool {
6666
return false
6767
}
6868

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-
9869
// GenerateRules extracts build metadata from source files in a directory.
9970
// GenerateRules is called in each directory where an update is requested
10071
// in depth-first post-order.
@@ -506,9 +477,8 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
506477
result.Gen = append(result.Gen, pyTest)
507478
result.Imports = append(result.Imports, pyTest.PrivateAttr(config.GazelleImportsKey))
508479
}
509-
// Validate existing rules have valid srcs
510480
if !cfg.CoarseGrainedGeneration() {
511-
emptyRules := py.validateExistingRules(args)
481+
emptyRules := py.getRulesWithInvalidSrcs(args)
512482
result.Empty = append(result.Empty, emptyRules...)
513483
}
514484
if !collisionErrors.Empty() {
@@ -522,6 +492,34 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
522492
return result
523493
}
524494

495+
// getRulesWithInvalidSrcs checks existing Python rules in the BUILD file and return the rules with invalid srcs.
496+
func (py *Python) getRulesWithInvalidSrcs(args language.GenerateArgs) (invalidRules []*rule.Rule) {
497+
if args.File == nil {
498+
return
499+
}
500+
regularFiles := args.RegularFiles
501+
regularFilesMap := make(map[string]struct{})
502+
for _, file := range regularFiles {
503+
regularFilesMap[file] = struct{}{}
504+
}
505+
for _, existingRule := range args.File.Rules {
506+
if _, ok := py.Kinds()[existingRule.Kind()]; !ok {
507+
continue
508+
}
509+
allInvalidSrcs := true
510+
for _, src := range existingRule.AttrStrings("srcs") {
511+
if _, ok := regularFilesMap[src]; ok {
512+
allInvalidSrcs = false
513+
break
514+
}
515+
}
516+
// If all srcs are invalid, delete the rule.
517+
if allInvalidSrcs {
518+
invalidRules = append(invalidRules, newTargetBuilder(existingRule.Kind(), existingRule.Name(), args.Config.RepoRoot, args.Rel, nil).build())
519+
}
520+
}
521+
return invalidRules
522+
}
525523
// isBazelPackage determines if the directory is a Bazel package by probing for
526524
// the existence of a known BUILD file name.
527525
func isBazelPackage(dir string) bool {

0 commit comments

Comments
 (0)