Skip to content

Commit 756cb2c

Browse files
committed
Remove invalid srcs in gazelle
1 parent 581cddc commit 756cb2c

File tree

13 files changed

+95
-13
lines changed

13 files changed

+95
-13
lines changed

gazelle/python/generate.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,35 @@ 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+
6998
// GenerateRules extracts build metadata from source files in a directory.
7099
// GenerateRules is called in each directory where an update is requested
71100
// in depth-first post-order.
@@ -230,7 +259,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
230259

231260
var result language.GenerateResult
232261
result.Gen = make([]*rule.Rule, 0)
233-
234262
collisionErrors := singlylinkedlist.New()
235263

236264
appendPyLibrary := func(srcs *treeset.Set, pyLibraryTargetName string) {
@@ -478,7 +506,11 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
478506
result.Gen = append(result.Gen, pyTest)
479507
result.Imports = append(result.Imports, pyTest.PrivateAttr(config.GazelleImportsKey))
480508
}
481-
509+
// Validate existing rules have valid srcs
510+
if !cfg.CoarseGrainedGeneration() {
511+
emptyRules := py.validateExistingRules(args)
512+
result.Empty = append(result.Empty, emptyRules...)
513+
}
482514
if !collisionErrors.Empty() {
483515
it := collisionErrors.Iterator()
484516
for it.Next() {

gazelle/python/kinds.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ var pyKinds = map[string]rule.KindInfo{
7070
MatchAny: false,
7171
NonEmptyAttrs: map[string]bool{
7272
"deps": true,
73-
"main": true,
7473
"srcs": true,
7574
"imports": true,
7675
},
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
load("@rules_python//python:defs.bzl", "py_binary")
2+
3+
py_binary(
4+
name = "remove_invalid_binary",
5+
srcs = ["__main__.py"],
6+
visibility = ["//:__subpackages__"],
7+
)

gazelle/python/testdata/remove_invalid_binary/BUILD.out

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Remove invalid binary
2+
3+
This test case asserts that `py_binary` should be deleted if invalid (no source files).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
workspace(name = "remove_invalid_binary")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
---
Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +0,0 @@
1-
load("@rules_python//python:defs.bzl", "py_library")
2-
3-
py_library(
4-
name = "deps_with_no_srcs_library",
5-
deps = [
6-
"//:remove_invalid_library",
7-
"@pypi//bar",
8-
"@pypi//foo",
9-
],
10-
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
load("@rules_python//python:defs.bzl", "py_test")
2+
3+
py_test(
4+
name = "remove_invalid_test",
5+
srcs = ["__test__.py"],
6+
visibility = ["//:__subpackages__"],
7+
)
8+
9+
py_test(
10+
name = "test_with_deps",
11+
srcs = ["test_main.py"],
12+
deps = [
13+
"//:remove_invalid_test",
14+
"@pypi//bar",
15+
"@pypi//foo",
16+
],
17+
)

gazelle/python/testdata/remove_invalid_pytest/BUILD.out

Whitespace-only changes.

0 commit comments

Comments
 (0)