Skip to content

do not index py_binary unless in file mode #2989

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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions gazelle/python/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ var (
buildFilenames = []string{"BUILD", "BUILD.bazel"}
)

func GetActualKindName(kind string, args language.GenerateArgs) string {
if kindOverride, ok := args.Config.KindMap[kind]; ok {
func GetActualKindName(kind string, c *config.Config) string {
if kindOverride, ok := c.KindMap[kind]; ok {
return kindOverride.KindName
}
return kind
Expand Down Expand Up @@ -90,9 +90,9 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
}
}

actualPyBinaryKind := GetActualKindName(pyBinaryKind, args)
actualPyLibraryKind := GetActualKindName(pyLibraryKind, args)
actualPyTestKind := GetActualKindName(pyTestKind, args)
actualPyBinaryKind := GetActualKindName(pyBinaryKind, args.Config)
actualPyLibraryKind := GetActualKindName(pyLibraryKind, args.Config)
actualPyTestKind := GetActualKindName(pyTestKind, args.Config)

pythonProjectRoot := cfg.PythonProjectRoot()

Expand Down
1 change: 0 additions & 1 deletion gazelle/python/python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ func testPath(t *testing.T, gazellePath, name string, files []bazel.RunfileEntry
if err != nil {
return err
}
t.Logf("%q exists", strings.TrimPrefix(path, testdataDir))
return nil
})
})
Expand Down
5 changes: 5 additions & 0 deletions gazelle/python/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func (*Resolver) Name() string { return languageName }
func (py *Resolver) Imports(c *config.Config, r *rule.Rule, f *rule.File) []resolve.ImportSpec {
cfgs := c.Exts[languageName].(pythonconfig.Configs)
cfg := cfgs[f.Pkg]
if !cfg.PerFileGeneration() && GetActualKindName(r.Kind(), c) == pyBinaryKind {
// Don't index py_binary in except in file mode, because all non-test Python files
// are in py_library already.
return nil
}
srcs := r.AttrStrings("srcs")
provides := make([]resolve.ImportSpec, 0, len(srcs)+1)
for _, src := range srcs {
Expand Down
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
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
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This is a Bazel workspace for the Gazelle test data.
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"],
)
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"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import foo.script
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__"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

if __name__ == "__main__":
print("Hello, world!")
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