Examples of using Protobuf to get an output with import prefixes

I wanted to achieve the behavior of adding an import prefix to compiled .proto libraries. This is commonly done to avoid package namespace collisions, especially when the package is already defined by other files or libraries.

For instance, if a proto file contains the import statement import "deps.proto", it can be converted to from a.b import deps in the case of Python.

The straightforward approach to achieve this with the protoc plugin is to nest the proto files within directories that correspond to the desired import prefix (e.g., a/b for a.b). This method generally works well, except when the proto files need to import each other.

In such cases, it becomes necessary to rewrite all the imports in the proto files with the appropriate prefix. You can refer to the protoc-import-statement-with-prefix for an example of how to do this.

Bazel rules use (rules_proto_grpc)

Similarly, when using rules_proto_grpc, which delegates to the protoc compiler, and using the import_prefix, the only way to use an import prefix is to also add it to all of the existing import statements of the .proto file.

Other

Buf's resources indicate that this behavior with import paths is expected: https://buf.build/docs/reference/protobuf-files-and-packages#imports .

This all seems like expected behavior based on this Google Group message.

Related issues: