protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

protoc: add way to use internal generators as plugins via CodeGeneratorRequest and CodeGeneratorResponse

Open mvdan opened this issue 7 years ago • 2 comments
trafficstars

One can make use of external generators via plugins, which have to implement the Request/Response message interface defined here: https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/compiler/plugin.proto

This is useful because one can run protobuf code generators in a fairly customised way. There's no need for the input proto files to be on disk, and the generated files don't need to be written to disk either.

We can accomplish half of this with the generators which are part of protoc itself. In particular, we can supply the input proto files via --descriptor_set_in, and which ones to generate as extra arguments. This is very similar to CodeGeneratorRequest, in particular the proto_file and file_to_generate fields.

It's a bit cumbersome to translate a CodeGeneratorRequest to this command-line format, but at least it's possible

However, there's no way to obtain the generated files as a CodeGeneratorResponse. The generators will just write the generated files to disk directly. Currently, the only way to accomplish this that comes to mind is to supply a temporary output directory, and walk it to find what files were generated, reading each of them into memory. This is possible in practice, but it's very cumbersome.

I propose that we make it easy to use protoc's native generators as plugins, via the well known CodeGeneratorRequest and CodeGeneratorResponse interface. We already have a --plugin flag to specify what plugin executables to run, so perhaps we could add --internal_plugin:

protoc --internal_plugin=js < code-generator-request > code-generator-response

With this flag, any of the code generators which are part of protoc could be accessible as a plugin via standard input and output with just protoc --internal_plugin=<lang>.

mvdan avatar Nov 14 '18 17:11 mvdan

Would love to bump this issue if possible - this would be a great add.

bufdev avatar Aug 19 '21 13:08 bufdev

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago.

github-actions[bot] avatar May 12 '24 10:05 github-actions[bot]

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please reopen it.

This issue was closed and archived because there has been no new activity in the 14 days since the inactive label was added.

github-actions[bot] avatar May 27 '24 10:05 github-actions[bot]

This issue is inactive but still relevant, it should NOT be closed.

mvdan avatar May 27 '24 10:05 mvdan