Skip to content

[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
asheshvidyut:fix/39583
Closed

[Python] grpc_tools: make PythonGrpcGenerator handle dot . in proto paths the same way as native Generator/PyiGenerator#39586
asheshvidyut wants to merge 1 commit intogrpc:masterfrom
asheshvidyut:fix/39583

Conversation

@asheshvidyut
Copy link
Copy Markdown
Member

@asheshvidyut asheshvidyut commented May 16, 2025

Fixes #39583

Problem

When we run grpc_tools.protoc on 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 -

python -m grpc_tools.protoc --proto_path=. --python_out=./generated/ --grpc_python_out=./generated/ github.com/openconfig/gnmi/proto/gnmi/gnmi.proto github.com/openconfig/gnmi/proto/gnmi_ext/gnmi_ext.proto

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.world does not work for directory structure - github.com/hello/world.py but it only works for directory structure github/com/hello/world.py.

Hence it made sense to update the src/compiler/python_generator.cc to replace dot with a slash.

Testing

Tested manually on the reported issue protos -

Screenshot 2025-05-19 at 6 29 12 PM

@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented May 16, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: asheshvidyut / name: Ashesh Vidyut (92be0e0)

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@grpc grpc deleted a comment from Copilot AI May 19, 2025
@asheshvidyut asheshvidyut requested review from Copilot and removed request for Copilot May 19, 2025 12:49
@asheshvidyut asheshvidyut added the release notes: no Indicates if PR should not be in release notes label May 19, 2025
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.py and _pb2_grpc.py files 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(), '.', '/');

Comment thread src/compiler/python_generator.cc
Comment thread src/compiler/python_generator.cc
@asheshvidyut asheshvidyut force-pushed the fix/39583 branch 2 times, most recently from 5d60576 to 92be0e0 Compare May 20, 2025 03:54
@sergiitk sergiitk added release notes: yes Indicates if PR needs to be in release notes and removed release notes: no Indicates if PR should not be in release notes labels May 21, 2025
@asheshvidyut asheshvidyut changed the title Fixes incorrect output directory issue[39583] [Python] Fixes incorrect output directory issue[39583] May 26, 2025
@sergiitk sergiitk changed the title [Python] Fixes incorrect output directory issue[39583] [Python] grpc_tools: make PythonGrpcGenerator handle dot . in proto paths the same way as native Generator/PyiGenerator Jun 6, 2025
aaliddell added a commit to rules-proto-grpc/rules_proto_grpc that referenced this pull request Jul 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gRPC Python plugin output directory does not match one used by core compiler

4 participants