Skip to content

Commit af8ff34

Browse files
committed
Generate imports for each proto src, as we should, and update tests accordingly
1 parent 706809c commit af8ff34

File tree

7 files changed

+19
-16
lines changed

7 files changed

+19
-16
lines changed

gazelle/python/generate.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ const (
4242
pyTestEntrypointTargetname = "__test__"
4343
conftestFilename = "conftest.py"
4444
conftestTargetname = "conftest"
45-
protoKey = "_proto_target"
4645
protoRelKey = "_proto_rel"
46+
protoSrcsKey = "_proto_srcs"
4747
)
4848

4949
var (
@@ -575,13 +575,15 @@ func generateProtoLibraries(args language.GenerateArgs, cfg *pythonconfig.Config
575575
// First, enumerate all the proto_library in this package.
576576
var protoRuleNames []string
577577
protoRel := map[string]string{}
578+
protoSrcs := map[string][]string{}
578579

579580
for _, r := range args.OtherGen {
580581
if r.Kind() != "proto_library" {
581582
continue
582583
}
583584
protoRuleNames = append(protoRuleNames, r.Name())
584585
protoRel[r.Name()] = args.Rel
586+
protoSrcs[r.Name()] = r.AttrStrings("srcs")
585587
}
586588
sort.Strings(protoRuleNames)
587589

@@ -614,8 +616,9 @@ func generateProtoLibraries(args language.GenerateArgs, cfg *pythonconfig.Config
614616
addVisibility(visibility).
615617
addResolvedDependency(":" + protoRuleName).
616618
generateImportsAttribute().build()
617-
pyProtoLibrary.SetPrivateAttr(protoKey, protoRuleName)
619+
618620
pyProtoLibrary.SetPrivateAttr(protoRelKey, protoRel[protoRuleName])
621+
pyProtoLibrary.SetPrivateAttr(protoSrcsKey, protoSrcs[protoRuleName])
619622

620623
res.Gen = append(res.Gen, pyProtoLibrary)
621624
res.Imports = append(res.Imports, pyProtoLibrary.PrivateAttr(config.GazelleImportsKey))

gazelle/python/resolve.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,17 @@ func importsProtoLibrary(cfg *pythonconfig.Config, r *rule.Rule) []resolve.Impor
134134

135135
// Determine the root module and emit an import for that,
136136
// i.e. for //foo:foo_py_pb2, we'd get foo.foo_pb2
137-
protoRuleAttr := r.PrivateAttr(protoKey)
138137
protoRelAttr := r.PrivateAttr(protoRelKey)
139-
if protoRuleAttr == nil || protoRelAttr == nil {
138+
protoSrcsAttr := r.PrivateAttr(protoSrcsKey)
139+
if protoRelAttr == nil || protoSrcsAttr == nil {
140140
return nil
141141
}
142142

143-
protoRule := protoRuleAttr.(string)
144-
generatedPbFileName := strings.TrimSuffix(protoRule, "_proto") + "_pb2.py"
145143
protoRel := protoRelAttr.(string)
146-
147-
specs = append(specs, importSpecFromSrc(cfg.PythonProjectRoot(), protoRel, generatedPbFileName))
144+
for _, protoSrc := range protoSrcsAttr.([]string) {
145+
generatedPbFileName := strings.TrimSuffix(protoSrc, ".proto") + "_pb2.py"
146+
specs = append(specs, importSpecFromSrc(cfg.PythonProjectRoot(), protoRel, generatedPbFileName))
147+
}
148148

149149
return specs
150150
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import test1_generates_dependency.foo.test1_generates_dependency_foo_pb2
1+
import test1_generates_dependency.foo.foo_pb2
22

3-
x = test1_generates_dependency_foo_pb2.Foo()
3+
x = foo_pb2.Foo()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# gazelle:resolve py test2_generates_using_resolve.foo.test2_generates_using_resolve_foo_pb2 //test2_generates_using_resolve/bar:bar_py_pb2
1+
# gazelle:resolve py test2_generates_using_resolve.bar.bar_pb2 //test2_generates_using_resolve/bar:bar_py_pb2

gazelle/python/testdata/resolves_proto_imports/test2_generates_using_resolve/BUILD.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
load("@rules_python//python:defs.bzl", "py_library")
22

3-
# gazelle:resolve py test2_generates_using_resolve.foo.test2_generates_using_resolve_foo_pb2 //test2_generates_using_resolve/bar:bar_py_pb2
3+
# gazelle:resolve py test2_generates_using_resolve.bar.bar_pb2 //test2_generates_using_resolve/bar:bar_py_pb2
44

55
py_library(
66
name = "test2_generates_using_resolve",
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import test2_generates_using_resolve.foo.test2_generates_using_resolve_foo_pb2
1+
import test2_generates_using_resolve.bar.bar_pb2
22

3-
x = test2_generates_using_resolve_foo_pb2.Bar()
3+
x = bar_pb2.Bar()
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import test3_uses_naming_convention.bar.test3_uses_naming_convention_bar_pb2
1+
import test3_uses_naming_convention.bar.bar_pb2
22

3-
x = test3_uses_naming_convention_bar_pb2.Bar()
3+
x = bar_pb2.Bar()

0 commit comments

Comments
 (0)