add support for module import prefix on Python compiler #17286
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.
Problem
When generating code in monorepos, some generated code is used with a prefix path, see https://rules-proto-grpc.com/en/latest/lang/python.html .
However, when a prefix_path is used, the generated proto import paths are no longer valid unless the module imports are also prefixed similarly.
This adds an option to support adding an import prefix to the printed imports.
Approach
This follows a similar approach already in use in the objectivec compiler.
How did I validate the approach?
I ran some basic manual tests like the following and validated that the module prefix was added to imports:
The diff when comparing with and without the
module_import_prefix=test.prefix
parameter:Remaining TODOs
I couldn't find tests for parameter behavior. plugin_unittest, https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/compiler/python/plugin_unittest.cc doesn't test much of the plugin logic.
I would appreciate some guidance in how to add a test for this feature (or if a test is needed).
Related Issues