-
-
Notifications
You must be signed in to change notification settings - Fork 610
fix(gazelle) Delete python targets with invalid srcs #3046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yushan26
wants to merge
7
commits into
bazel-contrib:main
Choose a base branch
from
yushan26:cleanup-python-targets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
03ab5e6
Remove invalid srcs in gazelle
yushan8 13d5396
Update with invalid sources
yushan8 c573eb3
Update
yushan8 31dfb88
Update with py_binary kind only
yushan8 15d4b62
address comments
yushan8 0485e5f
update
yushan8 43a48bd
update test
yushan8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -225,7 +225,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes | |||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
var result language.GenerateResult | ||||||||||||||||||||||||||||||||||||||||||||||
result.Gen = make([]*rule.Rule, 0) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
collisionErrors := singlylinkedlist.New() | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
appendPyLibrary := func(srcs *treeset.Set, pyLibraryTargetName string) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -473,7 +472,10 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes | |||||||||||||||||||||||||||||||||||||||||||||
result.Gen = append(result.Gen, pyTest) | ||||||||||||||||||||||||||||||||||||||||||||||
result.Imports = append(result.Imports, pyTest.PrivateAttr(config.GazelleImportsKey)) | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
if !cfg.CoarseGrainedGeneration() { | ||||||||||||||||||||||||||||||||||||||||||||||
emptyRules := py.getRulesWithInvalidSrcs(cfg, args) | ||||||||||||||||||||||||||||||||||||||||||||||
result.Empty = append(result.Empty, emptyRules...) | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
if !collisionErrors.Empty() { | ||||||||||||||||||||||||||||||||||||||||||||||
it := collisionErrors.Iterator() | ||||||||||||||||||||||||||||||||||||||||||||||
for it.Next() { | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -485,6 +487,48 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes | |||||||||||||||||||||||||||||||||||||||||||||
return result | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
// getRulesWithInvalidSrcs checks existing Python rules in the BUILD file and return the rules with invalid source files. | ||||||||||||||||||||||||||||||||||||||||||||||
// Invalid source files are files that do not exist or not a target. | ||||||||||||||||||||||||||||||||||||||||||||||
func (py *Python) getRulesWithInvalidSrcs(cfg *pythonconfig.Config, args language.GenerateArgs) (invalidRules []*rule.Rule) { | ||||||||||||||||||||||||||||||||||||||||||||||
if args.File == nil { | ||||||||||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
filesMap := make(map[string]struct{}) | ||||||||||||||||||||||||||||||||||||||||||||||
for _, file := range args.RegularFiles { | ||||||||||||||||||||||||||||||||||||||||||||||
if cfg.IgnoresFile(filepath.Base(file)) { | ||||||||||||||||||||||||||||||||||||||||||||||
continue | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
filesMap[file] = struct{}{} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
for _, file := range args.GenFiles { | ||||||||||||||||||||||||||||||||||||||||||||||
filesMap[file] = struct{}{} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
isTarget := func(src string) bool { | ||||||||||||||||||||||||||||||||||||||||||||||
return strings.HasPrefix(src, "@") || strings.HasPrefix(src, "//") || strings.HasPrefix(src, ":") | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
for _, existingRule := range args.File.Rules { | ||||||||||||||||||||||||||||||||||||||||||||||
if existingRule.Kind() != pyBinaryKind { | ||||||||||||||||||||||||||||||||||||||||||||||
continue | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
hasValidSrcs := true | ||||||||||||||||||||||||||||||||||||||||||||||
for _, src := range existingRule.AttrStrings("srcs") { | ||||||||||||||||||||||||||||||||||||||||||||||
if isTarget(src) { | ||||||||||||||||||||||||||||||||||||||||||||||
continue | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
if _, ok := filesMap[src]; ok { | ||||||||||||||||||||||||||||||||||||||||||||||
continue | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
hasValidSrcs = false | ||||||||||||||||||||||||||||||||||||||||||||||
break | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+514
to
+524
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
It should be something like this. |
||||||||||||||||||||||||||||||||||||||||||||||
if !hasValidSrcs { | ||||||||||||||||||||||||||||||||||||||||||||||
invalidRules = append(invalidRules, newTargetBuilder(existingRule.Kind(), existingRule.Name(), args.Config.RepoRoot, args.Rel, nil).build()) | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
return invalidRules | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
// isBazelPackage determines if the directory is a Bazel package by probing for | ||||||||||||||||||||||||||||||||||||||||||||||
// the existence of a known BUILD file name. | ||||||||||||||||||||||||||||||||||||||||||||||
func isBazelPackage(dir string) bool { | ||||||||||||||||||||||||||||||||||||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
load("@rules_python//python:defs.bzl", "py_binary") | ||
|
||
py_library( | ||
name = "keep_library", | ||
deps = ["//keep_binary:foo"], | ||
) | ||
py_binary( | ||
name = "remove_invalid_binary", | ||
srcs = ["__main__.py"], | ||
visibility = ["//:__subpackages__"], | ||
yushan26 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
load("@rules_python//python:defs.bzl", "py_library") | ||
|
||
py_library( | ||
name = "keep_library", | ||
deps = ["//keep_binary:foo"], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Remove invalid binary | ||
|
||
This test case asserts that `py_binary` should be deleted if invalid (no source files). |
Empty file.
13 changes: 13 additions & 0 deletions
13
gazelle/python/testdata/remove_invalid_binary/keep_binary/BUILD.in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
load("@rules_python//python:defs.bzl", "py_binary", "py_library") | ||
|
||
py_binary( | ||
yushan26 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
name = "foo", | ||
srcs = ["foo.py"], | ||
visibility = ["//:__subpackages__"], | ||
) | ||
|
||
py_library( | ||
name = "keep_binary", | ||
srcs = ["foo.py"], | ||
visibility = ["//:__subpackages__"], | ||
) |
13 changes: 13 additions & 0 deletions
13
gazelle/python/testdata/remove_invalid_binary/keep_binary/BUILD.out
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
load("@rules_python//python:defs.bzl", "py_binary", "py_library") | ||
|
||
py_binary( | ||
name = "foo", | ||
srcs = ["foo.py"], | ||
visibility = ["//:__subpackages__"], | ||
) | ||
|
||
py_library( | ||
name = "keep_binary", | ||
srcs = ["foo.py"], | ||
visibility = ["//:__subpackages__"], | ||
) |
2 changes: 2 additions & 0 deletions
2
gazelle/python/testdata/remove_invalid_binary/keep_binary/foo.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
if __name__ == "__main__": | ||
print("foo") |
Empty file.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about those python files in subdirs? Thoughts on my previous comment about reading
mainModules
instead ofargs.RegularFiles
?