-
-
Notifications
You must be signed in to change notification settings - Fork 609
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 all commits
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. | ||
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. nit: please describe what "invalid srcs" means in the comment. |
||
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 { | ||
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 | ||
} | ||
allInvalidSrcs := 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__"], | ||
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. Let's update this test case to have a valid py_library target in |
||
) |
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") | ||
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. optional nit: this file can (should?) be empty. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
py_binary( | ||
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. Add load statement |
||
name = "keep_target_binary", | ||
srcs = ["//test/binary:__main__.py"], | ||
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. nit: please refer to a binary found in the local dir (eg make |
||
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. | ||
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. nit: please remove copyright header - they're no longer needed. |
||
|
||
--- |
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.
nit: remove