-
-
Notifications
You must be signed in to change notification settings - Fork 610
feat(gazelle): python_proto_naming_convention
directive controls py_proto_library
naming
#3093
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
base: main
Are you sure you want to change the base?
feat(gazelle): python_proto_naming_convention
directive controls py_proto_library
naming
#3093
Conversation
gazelle/README.md
Outdated
@@ -208,6 +208,8 @@ Python-specific directives are as follows: | |||
| Controls the `py_binary` naming convention. Follows the same interpolation rules as `python_library_naming_convention`. | | | |||
| `# gazelle:python_test_naming_convention` | `$package_name$_test` | | |||
| Controls the `py_test` naming convention. Follows the same interpolation rules as `python_library_naming_convention`. | | | |||
| `# gazelle:python_proto_naming_convention` | `$proto_name$_py_pb2` | | |||
| Controls the `py_proto_library naming convention. It interpolates `$proto_name$` with the proto_library rule name, minus any trailing _proto. E.g. if the proto_library name is `foo_proto`, setting this to `$proto_name$_my_lib` would render to `foo_my_lib`. | | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't add a details section for this below, because none of the other naming convention directives have this, and I think it's pretty self-explanatory?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK SGTM 👍.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! I tested this on our monorepo and generated 227 py_proto_library
targets. Hot damn!
There are a variety of nits and requests but otherwise this is looking great, thanks!
CHANGELOG.md
Outdated
@@ -113,6 +113,8 @@ END_UNRELEASED_TEMPLATE | |||
dep is not added to the {obj}`py_test` target. | |||
* (gazelle) New directive `gazelle:python_generate_proto`; when `true`, | |||
Gazelle generates `py_proto_library` rules for `proto_library`. `false` by default. | |||
* (gazelle) New directive `gazelle:python_proto_naming_convention`; controls | |||
naming of `py_proto_library` rules. See `gazelle/README.md`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: please remove "See gazelle/README.md
." - I'll be refactoring Gazelle docs soon (#3082) and don't want to have to remember to update this 🙃
|
||
1. Has no effect on pre-existing `py_proto_library` when `gazelle:python_generate_proto` is disabled. | ||
2. Uses the default value when proto generation is on and `python_proto_naming_convention` is not set. | ||
2. Uses the provided naming convention when proto generation is on and `python_proto_naming_convention` is set. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: 3.
|
||
1. Has no effect on pre-existing `py_proto_library` when `gazelle:python_generate_proto` is disabled. | ||
2. Uses the default value when proto generation is on and `python_proto_naming_convention` is not set. | ||
2. Uses the provided naming convention when proto generation is on and `python_proto_naming_convention` is set. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a test case for:
- existing py_proto_library with name
foo_py_proto
andpython_proto_naming_convention
is set to$proto_name$_bar
.
Does Gazelle rename the target or leave it alone?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, good idea. I'm pretty sure that right now we'd leave the pre-existing target alone and generate a second target following the convention.
(later: oh, looks like we rename it instead. still, I think the following holds.)
If I'm considering what we want Gazelle to do, I think such a pre-existing target would interfere with dependency resolution -- we'd have two py_proto_library
that could potentially fill the same import in that case.
So I don't think we want Gazelle to leave it alone and generate a new target; we'd want it to either:
- rename the target, or
- keep the preexisting target as the sole
py_proto_library
.
Of these two, I think the most "Gazelle-y" would be to keep the preexisting target around as the sole py_proto_library
. That's what we appear to do with py_library
, and that's also what the golang extension does with go_proto_library
(I tested both by renaming an existing target and re-running Gazelle). I can write a test for that and adjust the logic to match.
2. Uses the default value when proto generation is on and `python_proto_naming_convention` is not set. | ||
2. Uses the provided naming convention when proto generation is on and `python_proto_naming_convention` is set. | ||
|
||
[gh-3081]: https://github.com/bazel-contrib/rules_python/issues/3081 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: unused link. Use it or remove it.
gazelle/README.md
Outdated
@@ -208,6 +208,8 @@ Python-specific directives are as follows: | |||
| Controls the `py_binary` naming convention. Follows the same interpolation rules as `python_library_naming_convention`. | | | |||
| `# gazelle:python_test_naming_convention` | `$package_name$_test` | | |||
| Controls the `py_test` naming convention. Follows the same interpolation rules as `python_library_naming_convention`. | | | |||
| `# gazelle:python_proto_naming_convention` | `$proto_name$_py_pb2` | | |||
| Controls the `py_proto_library naming convention. It interpolates `$proto_name$` with the proto_library rule name, minus any trailing _proto. E.g. if the proto_library name is `foo_proto`, setting this to `$proto_name$_my_lib` would render to `foo_my_lib`. | | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a note that says the python library will always be imported as foo_pb2
no matter what the py_proto_library
target name is.
So if we generate name = "foo_py_pb2"
, then the python import is import foo_pb2
.
proto_library(
name = "foo_proto",
srcs = ["foo.proto"],
...
)
py_proto_library(
name = "and_now_for_something_completely_different",
srcs = [":foo_proto"],
)
import foo_pb2 # not "import and_now_for_something_completely_different"
gazelle/README.md
Outdated
@@ -208,6 +208,8 @@ Python-specific directives are as follows: | |||
| Controls the `py_binary` naming convention. Follows the same interpolation rules as `python_library_naming_convention`. | | | |||
| `# gazelle:python_test_naming_convention` | `$package_name$_test` | | |||
| Controls the `py_test` naming convention. Follows the same interpolation rules as `python_library_naming_convention`. | | | |||
| `# gazelle:python_proto_naming_convention` | `$proto_name$_py_pb2` | | |||
| Controls the `py_proto_library naming convention. It interpolates `$proto_name$` with the proto_library rule name, minus any trailing _proto. E.g. if the proto_library name is `foo_proto`, setting this to `$proto_name$_my_lib` would render to `foo_my_lib`. | | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that Gazelle also can't discover the py_proto_library
rules yet, so add that note too. E.g.:
Note that Gazelle is not currently able to map an import
import foo_pb2
to apy_proto_library
target.
or something like that.
gazelle/README.md
Outdated
@@ -208,6 +208,8 @@ Python-specific directives are as follows: | |||
| Controls the `py_binary` naming convention. Follows the same interpolation rules as `python_library_naming_convention`. | | | |||
| `# gazelle:python_test_naming_convention` | `$package_name$_test` | | |||
| Controls the `py_test` naming convention. Follows the same interpolation rules as `python_library_naming_convention`. | | | |||
| `# gazelle:python_proto_naming_convention` | `$proto_name$_py_pb2` | | |||
| Controls the `py_proto_library naming convention. It interpolates `$proto_name$` with the proto_library rule name, minus any trailing _proto. E.g. if the proto_library name is `foo_proto`, setting this to `$proto_name$_my_lib` would render to `foo_my_lib`. | | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK SGTM 👍.
… violate the naming convention, and adjust behaviour to match
Closes #3081.
This adds support in the Gazelle plugin for controlling how the generated
py_proto_library
rules are named; support for these was originally added in #3057. We do this via a new Gazelle directive,python_proto_naming_convention
, which is similar topython_library_naming_convention
and the like, except it interpolates$proto_name$
, which is theproto_library
rule minus any trailing_proto
. We default to$proto_name$_py_pb2
.For instance, for a
proto_library
namedfoo_proto
, the default value would generatefoo_py_pb2
, aligning with the convention stated in the Bazel docs.