Hoopig Zhu
Hoopig Zhu

Reputation: 1

Failed to generate pb file when using proto3 version.[Nodejs]

1.I yarn add grpc-tools.

2.cmd command: protoc --js_out=import_style=commonjs,binary:./ --plugin=protoc-gen-grpc=./grpc_node_plugin.exe --grpc_out=./ UHDInterface.proto

3.Finally reported an error

UHDInterface.proto: is a proto3 file that contains optional fields, but code generator protoc-gen-grpc hasn't been updated to support optional fields in proto3. Please ask the owner of this code generator to support proto3 optional.--grpc_out

Upvotes: 0

Views: 1443

Answers (3)

Josh Laird
Josh Laird

Reputation: 7234

Your colleague was right, you can use optional with protobuf since v3.15.0 as Ayush has previously linked.

Your particular issue is because you need to update io.grpc:protoc-gen-grpc-java in your dependency tree.

I'm using v3.15.0 with Maven and had to update my io.grpc:protoc-gen-grpc-java like below:

<pluginArtifact>
  io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
</pluginArtifact>

where ${grpc.version} = 1.31.0. See more in their repo

Upvotes: 0

Ayush Singh
Ayush Singh

Reputation: 46

Protobuf

proto3 fully supports optional as of protobuf 3.15.0 (experimental support was added in v3.12.0 but required passing in a --experimental_allow_proto3_optional flag to protoc).

I also find it weird that it's not documented in the Language Guide (proto3) yet, but you can find some great documentation and examples here - Protobuf Field Presence.

gRPC

To generate gRPC files from proto files using the optional keyword, make sure you're using the latest plugins. For JavaScript, you can use the executable included in the grpc-tools NPM package - --plugin=protoc-gen-grpc_js=./node_modules/.bin/grpc_tools_node_protoc_plugin.

Alternatively, for other langauges (e.g. Python's grpcio-tools unfortunately doesn't include the grpc_python_plugin executable, but includes this functionality in python -m grpc_tools.protoc), you can try building the gRPC repo to get the binaries.

Please let me known if anything is unclear and I can edit!

Upvotes: 3

HelloWood
HelloWood

Reputation: 805

proto3 doesn't support optional and required filed in message defination any more, just remove these field in UHDInterface.proto

You can reference why messge type remove 'required,optional'? for more detail.

Upvotes: 0

Related Questions