grpcprotocprotobuf-go

Protoc generates GRPC .pb.go and grpc.pb.go files in different directories


I have a GRPC service and associated requests declared in a file called github.com/myuser/myrepo/protos/iam/v1/service.proto. The header for this file looks like this:

syntax = "proto3";
package myrepo.iam.v1;

option go_package = "github.com/myuser/myrepo-go/iam/v1"; // golang

Now, when I attempt to compile this using the following protoc command from the myrepo directory:

find ./protos -type f -name "*.proto" -exec protoc --go_out=./gopb --go-grpc_out=./gopb --go_opt=module=github.com/myuser/myrepo-go {} \;

This command works and produces two files:

gopb /
- iam /
  - v1 /
    - service.pb.go
- github.com /
  - myuser /
    - myrepo-go /
      - iam /
        - v1 /
          - service_grpc.pb.go

Of these files, service.pb.go is located correctly but I'm not sure why service_grpc.pb.go is located where it is. How can I ensure that both files are written to gopb/iam/v1/?


Solution

  • You need to duplicate the --go_opt=module= flag for gRPC i.e. --go-grpc_opt=module=github.com/myuser/myrepo-go

    protoc \
    --go_out=./gopb \
    --go_opt=module=github.com/myuser/myrepo-go
    --go-grpc_out=./gopb \
    --go-grpc_opt=module=github.com/myuser/myrepo-go \
    service.proto