Skip to content
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 @@ -88,9 +88,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
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,2 @@
# 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 @@
# 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,2 @@
if __name__ == "__main__":
print("Hello, world!")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
expect:
exit_code: 0