[Python] grpc_tools: make PythonGrpcGenerator handle dot . in proto paths the same way as native Generator/PyiGenerator#39586
Closed
asheshvidyut wants to merge 1 commit intogrpc:masterfrom
Closed
Conversation
|
|
There was a problem hiding this comment.
Pull Request Overview
This PR fixes an incorrect output directory issue in the Python gRPC generator by modifying how file names are processed.
- Replaces '-' with '_' as before
- Adds a new transformation to replace '.' with '/' to correctly set the directory structure for generated files
There was a problem hiding this comment.
Pull Request Overview
This PR updates the Python gRPC code generator to correctly map proto package names into nested output directories by converting dots to path separators.
- Replace
.with/in the base filename to reflect package hierarchy - Ensures generated
_pb2.pyand_pb2_grpc.pyfiles are placed in subdirectories matching the proto package
Comments suppressed due to low confidence (1)
src/compiler/python_generator.cc:953
- Consider adding or updating tests to cover scenarios where proto filenames include multiple package levels (e.g.,
foo.bar.baz.proto) to verify that the nested directory creation and file output work as expected.
std::replace(base.begin(), base.end(), '.', '/');
5d60576 to
92be0e0
Compare
sreenithi
approved these changes
May 20, 2025
. in proto paths the same way as native Generator/PyiGenerator
aaliddell
added a commit
to rules-proto-grpc/rules_proto_grpc
that referenced
this pull request
Jul 2, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #39583
Problem
When we run
grpc_tools.protocon a proto files nested inside a folder having a dot(.), protobuf generated files after replacing dots with /. But grpc python generator generated it without converting dot to /. Hence there were two directory structure generated for the following configuration after running the command -Ref - https://github.com/protocolbuffers/protobuf/blob/8694620342626b208506335396b1fcf4b8449f31/bazel/py_proto_library.bzl#L72
Solution
After investigating on the correct approach to generate proto files, among the two directory structure generated. (one with dot other with /). We found out that if we have dot in folder and python files are nested inside them, python imports does not work. Example this import statement -
import github.com.hello.worlddoes not work for directory structure -github.com/hello/world.pybut it only works for directory structuregithub/com/hello/world.py.Hence it made sense to update the
src/compiler/python_generator.ccto replace dot with a slash.Testing
Tested manually on the reported issue protos -