Skip to content

Commit 32d607e

Browse files
committed
Skip indexing py_binary rules
1 parent 1e21dbd commit 32d607e

File tree

14 files changed

+63
-5
lines changed

14 files changed

+63
-5
lines changed

gazelle/python/resolve.go

+3
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 r.Kind() == "py_binary" {
59+
return nil
60+
}
5861
cfgs := c.Exts[languageName].(pythonconfig.Configs)
5962
cfg := cfgs[f.Pkg]
6063
srcs := r.AttrStrings("srcs")

gazelle/python/testdata/binary_without_entrypoint_per_file_generation/BUILD.out

+1-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ py_library(
1919
name = "lib2",
2020
srcs = ["lib2.py"],
2121
visibility = ["//:__subpackages__"],
22-
deps = [
23-
":lib",
24-
":lib_and_main",
25-
],
22+
deps = [":lib"],
2623
)
2724

2825
py_binary(
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
import lib
2-
import lib_and_main
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
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
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This is a Bazel workspace for the Gazelle test data.
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+
)
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+
)
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.
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+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
if __name__ == "__main__":
3+
print("Hello, world!")
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)