Skip to content

Commit 7053e26

Browse files
taowang487Tao Wang
andauthored
Gazelle: Delete stale py_library and py_test targets (#3817)
## Summary Fix the issue #3375 Right now the `py_library` and `py_test` targets with missing srcs wouldn't be cleaned up by Gazelle in file mode. This can be reproduced by a unit test added in commit ad0e48c ## Testing In commit ad0e48c , without the fix, `bazel test //python/...` failed in the newly added tests. The stale `py_library ` and `py_test ` cannot be removed. In latest HEAD d32fb7b, `bazel test //python/...` can pass ``` INFO: From Testing //python:python_test_remove_invalid_per_file: ==================== Test output for //python:python_test_remove_invalid_per_file: --- FAIL: TestGazelleBinary (0.02s) --- FAIL: TestGazelleBinary/remove_invalid_per_file (0.08s) python_test.go:186: remove_invalid_per_file/BUILD diff (-want,+got): ( """ ... // 8 identical lines ) + py_library( + name = "deleted_lib", + srcs = ["deleted.py"], + visibility = ["//:__subpackages__"], + ) + + py_test( + name = "bar_test", + srcs = ["bar_test.py"], + ) + py_test( - name = "bar_test", - srcs = ["bar_test.py"], + name = "deleted_test", + srcs = ["deleted_test.py"], ) """ ) ``` --------- Co-authored-by: Tao Wang <watao@microsoft.com>
1 parent 42c8e75 commit 7053e26

25 files changed

Lines changed: 235 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ END_UNRELEASED_TEMPLATE
7474

7575
{#v0-0-0-fixed}
7676
### Fixed
77+
* (gazelle) `py_library` and `py_test` targets with missing source files can now be
78+
removed by Gazelle ([#3375](https://github.com/bazel-contrib/rules_python/issues/3375)).
79+
However `map_kind` and `alias_kind` will not be removed unless people are running a
80+
gazelle version that includes
81+
[bazel-gazelle#2362](https://github.com/bazel-contrib/bazel-gazelle/pull/2362)
7782
* (bootstrap) Fixed a potential race condition with symlink creation during
7883
startup.
7984
* (gazelle) Fixed handling of auto-included `__init__.py` files when generating `py_binary`

gazelle/python/generate.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -593,26 +593,53 @@ func (py *Python) getRulesWithInvalidSrcs(args language.GenerateArgs, validFiles
593593
validFilesMap[file] = struct{}{}
594594
}
595595

596+
// allFilesMap extends validFilesMap with all regular files on disk.
597+
// py_binary uses validFilesMap (main modules + generated files), while py_library
598+
// and py_test use allFilesMap since any file is a valid src for them.
599+
allFilesMap := make(map[string]struct{}, len(validFilesMap)+len(args.RegularFiles))
600+
for file := range validFilesMap {
601+
allFilesMap[file] = struct{}{}
602+
}
603+
for _, file := range args.RegularFiles {
604+
allFilesMap[file] = struct{}{}
605+
}
606+
596607
isTarget := func(src string) bool {
597608
return strings.HasPrefix(src, "@") || strings.HasPrefix(src, "//") || strings.HasPrefix(src, ":")
598609
}
599610
for _, existingRule := range args.File.Rules {
600-
if !kindMatches(args.Config, existingRule, pyBinaryKind) {
611+
var matchedKind string
612+
var filesMap map[string]struct{}
613+
if kindMatches(args.Config, existingRule, pyBinaryKind) {
614+
matchedKind = pyBinaryKind
615+
filesMap = validFilesMap
616+
} else if kindMatches(args.Config, existingRule, pyLibraryKind) {
617+
matchedKind = pyLibraryKind
618+
filesMap = allFilesMap
619+
} else if kindMatches(args.Config, existingRule, pyTestKind) {
620+
matchedKind = pyTestKind
621+
filesMap = allFilesMap
622+
} else {
623+
continue
624+
}
625+
626+
srcs := existingRule.AttrStrings("srcs")
627+
if len(srcs) == 0 {
601628
continue
602629
}
603630
var hasValidSrcs bool
604-
for _, src := range existingRule.AttrStrings("srcs") {
631+
for _, src := range srcs {
605632
if isTarget(src) {
606633
hasValidSrcs = true
607634
break
608635
}
609-
if _, ok := validFilesMap[src]; ok {
636+
if _, ok := filesMap[src]; ok {
610637
hasValidSrcs = true
611638
break
612639
}
613640
}
614641
if !hasValidSrcs {
615-
invalidRules = append(invalidRules, newTargetBuilder(pyBinaryKind, existingRule.Name(), "", "", nil, false).build())
642+
invalidRules = append(invalidRules, newTargetBuilder(matchedKind, existingRule.Name(), "", "", nil, false).build())
616643
}
617644
}
618645
return invalidRules

gazelle/python/testdata/remove_invalid_library/BUILD.in renamed to gazelle/python/testdata/remove_invalid_library_package_mode/BUILD.in

File renamed without changes.

gazelle/python/testdata/remove_invalid_library/BUILD.out renamed to gazelle/python/testdata/remove_invalid_library_package_mode/BUILD.out

File renamed without changes.

gazelle/python/testdata/remove_invalid_library/README.md renamed to gazelle/python/testdata/remove_invalid_library_package_mode/README.md

File renamed without changes.

gazelle/python/testdata/remove_invalid_library/WORKSPACE renamed to gazelle/python/testdata/remove_invalid_library_package_mode/WORKSPACE

File renamed without changes.

gazelle/python/testdata/remove_invalid_library/my_test.py renamed to gazelle/python/testdata/remove_invalid_library_package_mode/my_test.py

File renamed without changes.

gazelle/python/testdata/remove_invalid_library/others/BUILD.in renamed to gazelle/python/testdata/remove_invalid_library_package_mode/others/BUILD.in

File renamed without changes.

gazelle/python/testdata/remove_invalid_library/others/BUILD.out renamed to gazelle/python/testdata/remove_invalid_library_package_mode/others/BUILD.out

File renamed without changes.

gazelle/python/testdata/remove_invalid_library/test.yaml renamed to gazelle/python/testdata/remove_invalid_library_package_mode/test.yaml

File renamed without changes.

0 commit comments

Comments
 (0)