Skip to content

feat!: always generating py_library #2982

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
19 changes: 6 additions & 13 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 Expand Up @@ -244,16 +244,10 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
mainFileNames := make([]string, 0, len(mainModules))
for name := range mainModules {
mainFileNames = append(mainFileNames, name)

// Remove the file from srcs if we're doing per-file library generation so
// that we don't also generate a py_library target for it.
if cfg.PerFileGeneration() {
srcs.Remove(name)
}
}
sort.Strings(mainFileNames)
for _, filename := range mainFileNames {
pyBinaryTargetName := strings.TrimSuffix(filepath.Base(filename), ".py")
pyBinaryTargetName := strings.TrimSuffix(filepath.Base(filename), ".py") + "_bin"
if err := ensureNoCollision(args.File, pyBinaryTargetName, actualPyBinaryKind); err != nil {
fqTarget := label.New("", args.Rel, pyBinaryTargetName)
log.Printf("failed to generate target %q of kind %q: %v",
Expand All @@ -271,7 +265,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
}
}

// If we're doing per-file generation, srcs could be empty at this point, meaning we shouldn't make a py_library.
// If there is already a package named py_library target before, we should generate an empty py_library.
if srcs.Empty() {
if args.File == nil {
Expand Down
3 changes: 3 additions & 0 deletions gazelle/python/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 r.Kind() == GetActualKindName(pyBinaryKind, c) {
return nil
}
cfgs := c.Exts[languageName].(pythonconfig.Configs)
cfg := cfgs[f.Pkg]
srcs := r.AttrStrings("srcs")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
# gazelle:resolve py pandas @pip//:pandas

filegroup(
name = "collided_main",
name = "collided_main_bin",
srcs = ["collided_main.py"],
)
8 changes: 4 additions & 4 deletions gazelle/python/testdata/binary_without_entrypoint/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
# gazelle:resolve py pandas @pip//:pandas

filegroup(
name = "collided_main",
name = "collided_main_bin",
srcs = ["collided_main.py"],
)

py_binary(
name = "main",
name = "main_bin",
srcs = ["main.py"],
visibility = ["//:__subpackages__"],
deps = [
Expand All @@ -20,7 +20,7 @@ py_binary(
)

py_binary(
name = "main2",
name = "main2_bin",
srcs = ["main2.py"],
visibility = ["//:__subpackages__"],
deps = [":py_default_library"],
Expand All @@ -44,4 +44,4 @@ py_library(
py_test(
name = "main_test",
srcs = ["main_test.py"],
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
---
expect:
stderr: |
gazelle: failed to generate target "//:collided_main" of kind "py_binary": a target of kind "filegroup" with the same name already exists
gazelle: failed to generate target "//:collided_main_bin" of kind "py_binary": a target of kind "filegroup" with the same name already exists
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,39 @@ py_library(
)

py_binary(
name = "lib_and_main_bin",
srcs = ["lib_and_main.py"],
visibility = ["//:__subpackages__"],
)

py_library(
name = "lib_and_main",
srcs = ["lib_and_main.py"],
visibility = ["//:__subpackages__"],
)

py_binary(
name = "main_bin",
srcs = ["main.py"],
visibility = ["//:__subpackages__"],
deps = ["@pip//:pandas"],
)

py_library(
name = "main",
srcs = ["main.py"],
visibility = ["//:__subpackages__"],
deps = ["@pip//:pandas"],
)

py_binary(
name = "main2_bin",
srcs = ["main2.py"],
visibility = ["//:__subpackages__"],
deps = [":lib2"],
)

py_library(
name = "main2",
srcs = ["main2.py"],
visibility = ["//:__subpackages__"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load("@rules_python//python:defs.bzl", "py_binary")
# gazelle:python_generation_mode file

py_binary(
name = "a",
name = "a_bin",
srcs = ["a.py"],
visibility = ["//:__subpackages__"],
)
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
load("@rules_python//python:defs.bzl", "py_binary")
load("@rules_python//python:defs.bzl", "py_binary", "py_library")

# gazelle:python_generation_mode file

py_binary(
name = "a_bin",
srcs = ["a.py"],
visibility = ["//:__subpackages__"],
)

py_library(
name = "a",
srcs = ["a.py"],
visibility = ["//:__subpackages__"],
)

py_binary(
name = "b_bin",
srcs = ["b.py"],
visibility = ["//:__subpackages__"],
)

py_library(
name = "b",
srcs = ["b.py"],
visibility = ["//:__subpackages__"],
Expand Down
7 changes: 7 additions & 0 deletions gazelle/python/testdata/py312_syntax/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ py_library(
)

py_binary(
name = "pep_695_type_parameter_bin",
srcs = ["pep_695_type_parameter.py"],
visibility = ["//:__subpackages__"],
deps = [":_other_module"],
)

py_library(
name = "pep_695_type_parameter",
srcs = ["pep_695_type_parameter.py"],
visibility = ["//:__subpackages__"],
Expand Down