Skip to content

Commit c80b137

Browse files
committed
always generating py_library
1 parent 95fb54a commit c80b137

File tree

9 files changed

+56
-21
lines changed

9 files changed

+56
-21
lines changed

gazelle/python/generate.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ var (
4848
buildFilenames = []string{"BUILD", "BUILD.bazel"}
4949
)
5050

51-
func GetActualKindName(kind string, args language.GenerateArgs) string {
52-
if kindOverride, ok := args.Config.KindMap[kind]; ok {
51+
func GetActualKindName(kind string, c *config.Config) string {
52+
if kindOverride, ok := c.KindMap[kind]; ok {
5353
return kindOverride.KindName
5454
}
5555
return kind
@@ -90,9 +90,9 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
9090
}
9191
}
9292

93-
actualPyBinaryKind := GetActualKindName(pyBinaryKind, args)
94-
actualPyLibraryKind := GetActualKindName(pyLibraryKind, args)
95-
actualPyTestKind := GetActualKindName(pyTestKind, args)
93+
actualPyBinaryKind := GetActualKindName(pyBinaryKind, args.Config)
94+
actualPyLibraryKind := GetActualKindName(pyLibraryKind, args.Config)
95+
actualPyTestKind := GetActualKindName(pyTestKind, args.Config)
9696

9797
pythonProjectRoot := cfg.PythonProjectRoot()
9898

@@ -244,16 +244,10 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
244244
mainFileNames := make([]string, 0, len(mainModules))
245245
for name := range mainModules {
246246
mainFileNames = append(mainFileNames, name)
247-
248-
// Remove the file from srcs if we're doing per-file library generation so
249-
// that we don't also generate a py_library target for it.
250-
if cfg.PerFileGeneration() {
251-
srcs.Remove(name)
252-
}
253247
}
254248
sort.Strings(mainFileNames)
255249
for _, filename := range mainFileNames {
256-
pyBinaryTargetName := strings.TrimSuffix(filepath.Base(filename), ".py")
250+
pyBinaryTargetName := strings.TrimSuffix(filepath.Base(filename), ".py") + "_bin"
257251
if err := ensureNoCollision(args.File, pyBinaryTargetName, actualPyBinaryKind); err != nil {
258252
fqTarget := label.New("", args.Rel, pyBinaryTargetName)
259253
log.Printf("failed to generate target %q of kind %q: %v",
@@ -271,7 +265,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
271265
}
272266
}
273267

274-
// If we're doing per-file generation, srcs could be empty at this point, meaning we shouldn't make a py_library.
275268
// If there is already a package named py_library target before, we should generate an empty py_library.
276269
if srcs.Empty() {
277270
if args.File == nil {

gazelle/python/resolve.go

Lines changed: 3 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 r.Kind() == GetActualKindName(pyBinaryKind, c) {
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/BUILD.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
# gazelle:resolve py pandas @pip//:pandas
44

55
filegroup(
6-
name = "collided_main",
6+
name = "collided_main_bin",
77
srcs = ["collided_main.py"],
88
)

gazelle/python/testdata/binary_without_entrypoint/BUILD.out

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
55
# gazelle:resolve py pandas @pip//:pandas
66

77
filegroup(
8-
name = "collided_main",
8+
name = "collided_main_bin",
99
srcs = ["collided_main.py"],
1010
)
1111

1212
py_binary(
13-
name = "main",
13+
name = "main_bin",
1414
srcs = ["main.py"],
1515
visibility = ["//:__subpackages__"],
1616
deps = [
@@ -20,7 +20,7 @@ py_binary(
2020
)
2121

2222
py_binary(
23-
name = "main2",
23+
name = "main2_bin",
2424
srcs = ["main2.py"],
2525
visibility = ["//:__subpackages__"],
2626
deps = [":py_default_library"],
@@ -44,4 +44,4 @@ py_library(
4444
py_test(
4545
name = "main_test",
4646
srcs = ["main_test.py"],
47-
)
47+
)

gazelle/python/testdata/binary_without_entrypoint/test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
---
1616
expect:
1717
stderr: |
18-
gazelle: failed to generate target "//:collided_main" of kind "py_binary": a target of kind "filegroup" with the same name already exists
18+
gazelle: failed to generate target "//:collided_main_bin" of kind "py_binary": a target of kind "filegroup" with the same name already exists

gazelle/python/testdata/binary_without_entrypoint_per_file_generation/BUILD.out

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,39 @@ py_library(
2626
)
2727

2828
py_binary(
29+
name = "lib_and_main_bin",
30+
srcs = ["lib_and_main.py"],
31+
visibility = ["//:__subpackages__"],
32+
)
33+
34+
py_library(
2935
name = "lib_and_main",
3036
srcs = ["lib_and_main.py"],
3137
visibility = ["//:__subpackages__"],
3238
)
3339

3440
py_binary(
41+
name = "main_bin",
42+
srcs = ["main.py"],
43+
visibility = ["//:__subpackages__"],
44+
deps = ["@pip//:pandas"],
45+
)
46+
47+
py_library(
3548
name = "main",
3649
srcs = ["main.py"],
3750
visibility = ["//:__subpackages__"],
3851
deps = ["@pip//:pandas"],
3952
)
4053

4154
py_binary(
55+
name = "main2_bin",
56+
srcs = ["main2.py"],
57+
visibility = ["//:__subpackages__"],
58+
deps = [":lib2"],
59+
)
60+
61+
py_library(
4262
name = "main2",
4363
srcs = ["main2.py"],
4464
visibility = ["//:__subpackages__"],

gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/BUILD.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ load("@rules_python//python:defs.bzl", "py_binary")
33
# gazelle:python_generation_mode file
44

55
py_binary(
6-
name = "a",
6+
name = "a_bin",
77
srcs = ["a.py"],
88
visibility = ["//:__subpackages__"],
99
)

gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/BUILD.out

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1-
load("@rules_python//python:defs.bzl", "py_binary")
1+
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
22

33
# gazelle:python_generation_mode file
44

55
py_binary(
6+
name = "a_bin",
7+
srcs = ["a.py"],
8+
visibility = ["//:__subpackages__"],
9+
)
10+
11+
py_library(
612
name = "a",
713
srcs = ["a.py"],
814
visibility = ["//:__subpackages__"],
915
)
1016

1117
py_binary(
18+
name = "b_bin",
19+
srcs = ["b.py"],
20+
visibility = ["//:__subpackages__"],
21+
)
22+
23+
py_library(
1224
name = "b",
1325
srcs = ["b.py"],
1426
visibility = ["//:__subpackages__"],

gazelle/python/testdata/py312_syntax/BUILD.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ py_library(
99
)
1010

1111
py_binary(
12+
name = "pep_695_type_parameter_bin",
13+
srcs = ["pep_695_type_parameter.py"],
14+
visibility = ["//:__subpackages__"],
15+
deps = [":_other_module"],
16+
)
17+
18+
py_library(
1219
name = "pep_695_type_parameter",
1320
srcs = ["pep_695_type_parameter.py"],
1421
visibility = ["//:__subpackages__"],

0 commit comments

Comments
 (0)