Skip to content

Commit efe3db3

Browse files
committed
Skip indexing py_binary rules
Update update
1 parent 9f3512f commit efe3db3

File tree

12 files changed

+88
-0
lines changed

12 files changed

+88
-0
lines changed

gazelle/python/resolve.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ func (*Resolver) Name() string { return languageName }
5555
// If nil is returned, the rule will not be indexed. If any non-nil slice is
5656
// returned, including an empty slice, the rule will be indexed.
5757
func (py *Resolver) Imports(c *config.Config, r *rule.Rule, f *rule.File) []resolve.ImportSpec {
58+
if !indexPyBinaryImport(r, f) {
59+
return nil
60+
}
5861
cfgs := c.Exts[languageName].(pythonconfig.Configs)
5962
cfg := cfgs[f.Pkg]
6063
srcs := r.AttrStrings("srcs")
@@ -317,3 +320,29 @@ func convertDependencySetToExpr(set *treeset.Set) bzl.Expr {
317320
}
318321
return &bzl.ListExpr{List: deps}
319322
}
323+
324+
// indexPyBinaryImport returns whether the corresponding py_binary rule need to be indexed.
325+
// To avoid multiple labels indexing the same import,
326+
// check if there is a corresponding py_library rule with the same srcs.
327+
func indexPyBinaryImport(r *rule.Rule, f *rule.File) bool {
328+
// If the rule is not a py_binary, it should be indexed.
329+
if r.Kind() != "py_binary" {
330+
return true
331+
}
332+
pyBinarySrcs := r.AttrStrings("srcs")
333+
if len(pyBinarySrcs) == 0 {
334+
return false
335+
}
336+
for _, otherRule := range f.Rules {
337+
if otherRule.Kind() != "py_library" {
338+
continue
339+
}
340+
pyLibrarySrcs := otherRule.AttrStrings("srcs")
341+
for _, src := range pyLibrarySrcs {
342+
if src == pyBinarySrcs[0] {
343+
return false
344+
}
345+
}
346+
}
347+
return true
348+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# gazelle:python_extension enabled
2+
# gazelle:python_library_naming_convention py_default_library
3+
# gazelle:python_generation_mode package
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# gazelle:python_extension enabled
2+
# gazelle:python_library_naming_convention py_default_library
3+
# gazelle:python_generation_mode package
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Import with same srcs for library and binary
2+
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.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This is a Bazel workspace for the Gazelle test data.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
py_library(
4+
name = "bar",
5+
srcs = ["bar.py"],
6+
visibility = ["//:__subpackages__"],
7+
deps = ["//foo"],
8+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
py_library(
4+
name = "bar",
5+
srcs = ["bar.py"],
6+
visibility = ["//:__subpackages__"],
7+
deps = ["//foo:py_default_library"],
8+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import foo.script

gazelle/python/testdata/import_with_same_srcs_library_and_binary/foo/BUILD.in

Whitespace-only changes.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
2+
3+
py_binary(
4+
name = "script",
5+
srcs = ["script.py"],
6+
visibility = ["//:__subpackages__"],
7+
)
8+
9+
py_library(
10+
name = "py_default_library",
11+
srcs = ["script.py"],
12+
visibility = ["//:__subpackages__"],
13+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
if __name__ == "__main__":
3+
print("Hello, world!")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2023 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
---
16+
expect:
17+
exit_code: 0

0 commit comments

Comments
 (0)