-
-
Notifications
You must be signed in to change notification settings - Fork 616
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
03ab5e6
13d5396
c573eb3
31dfb88
15d4b62
0485e5f
43a48bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -32,6 +32,7 @@ import ( | |||||||||||||||||||||||||||||||||||||||
"github.com/emirpasic/gods/sets/treeset" | ||||||||||||||||||||||||||||||||||||||||
godsutils "github.com/emirpasic/gods/utils" | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
"github.com/bazel-contrib/rules_python/gazelle/pythonconfig" | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
|
@@ -225,7 +226,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 +473,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(args) | ||||||||||||||||||||||||||||||||||||||||
result.Empty = append(result.Empty, emptyRules...) | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
if !collisionErrors.Empty() { | ||||||||||||||||||||||||||||||||||||||||
it := collisionErrors.Iterator() | ||||||||||||||||||||||||||||||||||||||||
for it.Next() { | ||||||||||||||||||||||||||||||||||||||||
|
@@ -485,6 +488,44 @@ 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 srcs. | ||||||||||||||||||||||||||||||||||||||||
dougthor42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||
func (py *Python) getRulesWithInvalidSrcs(args language.GenerateArgs) (invalidRules []*rule.Rule) { | ||||||||||||||||||||||||||||||||||||||||
if args.File == nil { | ||||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
filesMap := make(map[string]struct{}) | ||||||||||||||||||||||||||||||||||||||||
for _, file := range args.RegularFiles { | ||||||||||||||||||||||||||||||||||||||||
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. How about those python files in subdirs? Thoughts on my previous comment about reading |
||||||||||||||||||||||||||||||||||||||||
filesMap[file] = struct{}{} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
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. Sorry, I should have caught this earlier, but I don't think we should read rules_python/gazelle/python/generate.go Lines 116 to 132 in cab415d
For example, if a There is also logic to collect py files from subdirs that is needed by project mode: rules_python/gazelle/python/generate.go Line 148 in cab415d
Since we scope this PR down to rules_python/gazelle/python/generate.go Line 236 in cab415d
The "GenFiles" and "isTarget` logic can stay, because they are not handled anywhere else |
||||||||||||||||||||||||||||||||||||||||
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 | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
allInvalidSrcs := true | ||||||||||||||||||||||||||||||||||||||||
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. I would avoid double negative in the code. So instead of "allInvalidSrcs == false", it's easier to understand if we say "hasValidSrcs == true" |
||||||||||||||||||||||||||||||||||||||||
for _, src := range existingRule.AttrStrings("srcs") { | ||||||||||||||||||||||||||||||||||||||||
if _, ok := filesMap[src]; ok { | ||||||||||||||||||||||||||||||||||||||||
allInvalidSrcs = false | ||||||||||||||||||||||||||||||||||||||||
break | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
if isTarget(src) { | ||||||||||||||||||||||||||||||||||||||||
allInvalidSrcs = false | ||||||||||||||||||||||||||||||||||||||||
break | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
if allInvalidSrcs { | ||||||||||||||||||||||||||||||||||||||||
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 { | ||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
load("@rules_python//python:defs.bzl", "py_binary") | ||
|
||
py_binary( | ||
name = "remove_invalid_binary", | ||
srcs = ["__main__.py"], | ||
visibility = ["//:__subpackages__"], | ||
yushan26 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) |
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). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
workspace(name = "remove_invalid_binary") | ||
dougthor42 marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
py_binary( | ||
yushan26 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
name = "keep_target_binary", | ||
srcs = ["//test/binary:__main__.py"], | ||
dougthor42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
visibility = ["//:__subpackages__"], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
py_binary( | ||
name = "keep_target_binary", | ||
srcs = ["//test/binary:__main__.py"], | ||
visibility = ["//:__subpackages__"], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright 2023 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
dougthor42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
--- |
Uh oh!
There was an error while loading. Please reload this page.