Skip to content

Commit 15d4b62

Browse files
committed
address comments
1 parent 31dfb88 commit 15d4b62

File tree

7 files changed

+33
-33
lines changed

7 files changed

+33
-33
lines changed

gazelle/python/generate.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

gazelle/python/testdata/remove_invalid_binary/BUILD.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
load("@rules_python//python:defs.bzl", "py_binary")
22

3+
py_library(
4+
name = "keep_library",
5+
deps = ["//keep_binary:foo"],
6+
)
37
py_binary(
48
name = "remove_invalid_binary",
59
srcs = ["__main__.py"],
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
workspace(name = "remove_invalid_binary")
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
load("@rules_python//python:defs.bzl", "py_binary")
2+
13
py_binary(
2-
name = "keep_target_binary",
3-
srcs = ["//test/binary:__main__.py"],
4+
name = "foo",
5+
srcs = ["foo.py"],
46
visibility = ["//:__subpackages__"],
57
)
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
load("@rules_python//python:defs.bzl", "py_binary")
2+
13
py_binary(
2-
name = "keep_target_binary",
3-
srcs = ["//test/binary:__main__.py"],
4+
name = "foo",
5+
srcs = ["foo.py"],
46
visibility = ["//:__subpackages__"],
57
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
if __name__ == "__main__":
2+
print("foo")
Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +0,0 @@
1-
# Copyright 2023 The Bazel Authors. All rights reserved.
2-
#
3-
# Licensed under the Apache License, Version 2.0 (the "License");
4-
# you may not use this file except in compliance with the License.
5-
# You may obtain a copy of the License at
6-
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
8-
#
9-
# Unless required by applicable law or agreed to in writing, software
10-
# distributed under the License is distributed on an "AS IS" BASIS,
11-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
# See the License for the specific language governing permissions and
13-
# limitations under the License.
14-
15-
---

0 commit comments

Comments
 (0)