-
-
Notifications
You must be signed in to change notification settings - Fork 594
fix(gazelle): Skip indexing py_binary rules if a corresponding py_library rule contains the same srcs #2822
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
2
commits into
bazel-contrib:main
Choose a base branch
from
yushan26:index-py-library
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.
+89
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -55,6 +55,9 @@ func (*Resolver) Name() string { return languageName } | |
// If nil is returned, the rule will not be indexed. If any non-nil slice is | ||
// returned, including an empty slice, the rule will be indexed. | ||
func (py *Resolver) Imports(c *config.Config, r *rule.Rule, f *rule.File) []resolve.ImportSpec { | ||
if !indexPyBinaryImport(r, f) { | ||
return nil | ||
} | ||
cfgs := c.Exts[languageName].(pythonconfig.Configs) | ||
cfg := cfgs[f.Pkg] | ||
srcs := r.AttrStrings("srcs") | ||
|
@@ -317,3 +320,29 @@ func convertDependencySetToExpr(set *treeset.Set) bzl.Expr { | |
} | ||
return &bzl.ListExpr{List: deps} | ||
} | ||
|
||
// indexPyBinaryImport returns whether the corresponding py_binary rule need to be indexed. | ||
// To avoid multiple labels indexing the same import, | ||
// check if there is a corresponding py_library rule with the same srcs. | ||
func indexPyBinaryImport(r *rule.Rule, f *rule.File) bool { | ||
// If the rule is not a py_binary, it should be indexed. | ||
if r.Kind() != "py_binary" { | ||
return true | ||
} | ||
pyBinarySrcs := r.AttrStrings("srcs") | ||
if len(pyBinarySrcs) == 0 { | ||
return false | ||
} | ||
for _, otherRule := range f.Rules { | ||
if otherRule.Kind() != "py_library" { | ||
continue | ||
} | ||
pyLibrarySrcs := otherRule.AttrStrings("srcs") | ||
for _, src := range pyLibrarySrcs { | ||
if src == pyBinarySrcs[0] { | ||
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. A py_binary can have multiple What are the requirements for not indexing the binary?
|
||
return false | ||
} | ||
} | ||
} | ||
return true | ||
} |
3 changes: 3 additions & 0 deletions
3
gazelle/python/testdata/import_with_same_srcs_library_and_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,3 @@ | ||
# gazelle:python_extension enabled | ||
# gazelle:python_library_naming_convention py_default_library | ||
# gazelle:python_generation_mode package |
3 changes: 3 additions & 0 deletions
3
gazelle/python/testdata/import_with_same_srcs_library_and_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,3 @@ | ||
# gazelle:python_extension enabled | ||
# gazelle:python_library_naming_convention py_default_library | ||
# gazelle:python_generation_mode package |
2 changes: 2 additions & 0 deletions
2
gazelle/python/testdata/import_with_same_srcs_library_and_binary/README.md
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 @@ | ||
# Import with same srcs for library and binary | ||
This test case asserts a file with py_library and py_binary rules that include the same .py file in srcs will resolve to the py_library rule correctly. |
1 change: 1 addition & 0 deletions
1
gazelle/python/testdata/import_with_same_srcs_library_and_binary/WORKSPACE
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 @@ | ||
# This is a Bazel workspace for the Gazelle test data. |
8 changes: 8 additions & 0 deletions
8
gazelle/python/testdata/import_with_same_srcs_library_and_binary/bar/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,8 @@ | ||
load("@rules_python//python:defs.bzl", "py_library") | ||
|
||
py_library( | ||
name = "bar", | ||
srcs = ["bar.py"], | ||
visibility = ["//:__subpackages__"], | ||
deps = ["//foo"], | ||
) |
8 changes: 8 additions & 0 deletions
8
gazelle/python/testdata/import_with_same_srcs_library_and_binary/bar/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,8 @@ | ||
load("@rules_python//python:defs.bzl", "py_library") | ||
|
||
py_library( | ||
name = "bar", | ||
srcs = ["bar.py"], | ||
visibility = ["//:__subpackages__"], | ||
deps = ["//foo:py_default_library"], | ||
) |
1 change: 1 addition & 0 deletions
1
gazelle/python/testdata/import_with_same_srcs_library_and_binary/bar/bar.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 @@ | ||
import foo.script |
Empty file.
13 changes: 13 additions & 0 deletions
13
gazelle/python/testdata/import_with_same_srcs_library_and_binary/foo/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 = "script", | ||
srcs = ["script.py"], | ||
visibility = ["//:__subpackages__"], | ||
) | ||
|
||
py_library( | ||
name = "py_default_library", | ||
srcs = ["script.py"], | ||
visibility = ["//:__subpackages__"], | ||
) |
3 changes: 3 additions & 0 deletions
3
gazelle/python/testdata/import_with_same_srcs_library_and_binary/foo/script.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,3 @@ | ||
|
||
if __name__ == "__main__": | ||
print("Hello, world!") |
17 changes: 17 additions & 0 deletions
17
gazelle/python/testdata/import_with_same_srcs_library_and_binary/test.yaml
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,17 @@ | ||
# 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. | ||
|
||
--- | ||
expect: | ||
exit_code: 0 |
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.
Wrap at ~80chars please