Skip to content

Commit d8b3f5d

Browse files
committed
Add test ensuring that pre-existing targets are kept intact when they violate the naming convention, and adjust behaviour to match
1 parent 7dafe83 commit d8b3f5d

File tree

6 files changed

+54
-2
lines changed

6 files changed

+54
-2
lines changed

gazelle/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ py_libary(
266266

267267
#### Directive: `python_proto_naming_convention`:
268268

269-
Set this directive to a string pattern to control how the generated `py_proto_library` targets are named. Gazelle will replace `$proto_name$` in the pattern with the name of the `proto_library` rule, stripping out a trailing `_proto`. For example:
269+
Set this directive to a string pattern to control how the generated `py_proto_library` targets are named. When generating new `py_proto_library` rules, Gazelle will replace `$proto_name$` in the pattern with the name of the `proto_library` rule, stripping out a trailing `_proto`. For example:
270270

271271
```starlark
272272
# gazelle:python_generate_proto true
@@ -286,7 +286,7 @@ py_proto_library(
286286
)
287287
```
288288

289-
The default naming convention is `$proto_name$_pb2_py`, so by default in the above example Gazelle would generate `foo_pb2_py`.
289+
The default naming convention is `$proto_name$_pb2_py`, so by default in the above example Gazelle would generate `foo_pb2_py`. Any pre-existing rules are left in place and not renamed.
290290

291291
Note that the Python library will always be imported as `foo_pb2` in Python code, regardless of the naming convention. Also note that Gazelle is currently not able to map said imports, e.g. `import foo_pb2`, to fill in `py_proto_library` targets as dependencies of other rules. See [this issue](https://github.com/bazel-contrib/rules_python/issues/1703).
292292

gazelle/python/generate.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,10 +582,16 @@ func generateProtoLibraries(args language.GenerateArgs, cfg *pythonconfig.Config
582582

583583
// Next, enumerate all the pre-existing py_proto_library in this package, so we can delete unnecessary rules later.
584584
pyProtoRules := map[string]bool{}
585+
pyProtoRulesForProto := map[string]string{}
585586
if args.File != nil {
586587
for _, r := range args.File.Rules {
587588
if r.Kind() == "py_proto_library" {
588589
pyProtoRules[r.Name()] = false
590+
591+
protos := r.AttrStrings("deps")
592+
for _, proto := range protos {
593+
pyProtoRulesForProto[strings.TrimPrefix(proto, ":")] = r.Name()
594+
}
589595
}
590596
}
591597
}
@@ -594,6 +600,11 @@ func generateProtoLibraries(args language.GenerateArgs, cfg *pythonconfig.Config
594600
// Generate a py_proto_library for each proto_library.
595601
for _, protoRuleName := range protoRuleNames {
596602
pyProtoLibraryName := cfg.RenderProtoName(protoRuleName)
603+
if ruleName, ok := pyProtoRulesForProto[protoRuleName]; ok {
604+
// There exists a pre-existing py_proto_library for this proto. Keep this name.
605+
pyProtoLibraryName = ruleName
606+
}
607+
597608
pyProtoLibrary := newTargetBuilder(pyProtoLibraryKind, pyProtoLibraryName, pythonProjectRoot, args.Rel, &emptySiblings).
598609
addVisibility(visibility).
599610
addResolvedDependency(":" + protoRuleName).

gazelle/python/testdata/directive_python_proto_naming_convention/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ correctly:
66
1. Has no effect on pre-existing `py_proto_library` when `gazelle:python_generate_proto` is disabled.
77
2. Uses the default value when proto generation is on and `python_proto_naming_convention` is not set.
88
3. Uses the provided naming convention when proto generation is on and `python_proto_naming_convention` is set.
9+
4. With a pre-existing `py_proto_library` not following a given naming convention, keeps it intact and does not rename it.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load("@rules_proto//proto:defs.bzl", "proto_library")
2+
3+
# gazelle:python_generate_proto true
4+
# gazelle:python_proto_naming_convention $proto_name$_bar
5+
6+
proto_library(
7+
name = "foo_proto",
8+
srcs = ["foo.proto"],
9+
visibility = ["//:__subpackages__"],
10+
)
11+
12+
py_proto_library(
13+
name = "foo_py_proto",
14+
visibility = ["//:__subpackages__"],
15+
deps = [":foo_proto"],
16+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
load("@com_google_protobuf//bazel:py_proto_library.bzl", "py_proto_library")
2+
load("@rules_proto//proto:defs.bzl", "proto_library")
3+
4+
# gazelle:python_generate_proto true
5+
# gazelle:python_proto_naming_convention $proto_name$_bar
6+
7+
proto_library(
8+
name = "foo_proto",
9+
srcs = ["foo.proto"],
10+
visibility = ["//:__subpackages__"],
11+
)
12+
13+
py_proto_library(
14+
name = "foo_py_proto",
15+
visibility = ["//:__subpackages__"],
16+
deps = [":foo_proto"],
17+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
3+
package foo;
4+
5+
message Foo {
6+
string bar = 1;
7+
}

0 commit comments

Comments
 (0)