Search code examples
goprotocol-buffersgrpcprotoc

Protobuf failing to produce the code with the `unable to determine Go import path for` error


I have two proto files according to the following structure in my file system:

protos
├── kws
│   └── kws.proto
├── common
│   └── common.proto

And here is their content:

common.proto

syntax = "proto3";

package common;

enum Code {
  SUCCESS = 0;
  CANCELLED = 1;
  UNKNOWN = 2;
  MALFORMED_INPUT = 3;
}

message Status {
  Code code = 1;
  optional string reason = 2;
}

kws.proto

syntax = "proto3";
import "google/protobuf/empty.proto";
import "protos/common/common.proto";

package kws;

service KWSService {
  rpc SpotKeywords(SpotKeywordsRequest) returns (SpotKeywordsResponse) {}
  rpc GetKeywords(google.protobuf.Empty) returns (GetKeywordsResponse) {}
  rpc TrainKWS(TrainKWSRequest) returns (TrainKWSResponse) {}
}

message SpotKeywordsRequest {
  repeated bytes audio_data = 1;
  repeated string keywords = 2;
}

message SpotKeywordsResponse {
  repeated Keyword keyword_info = 1;
}

message Keyword {
  string keyword = 1;
  int32 spot_time = 2;
  common.Status status = 3;
}

message GetKeywordsResponse {
  repeated string keywords = 1;
}


message TrainKWSRequest {
  repeated bytes audio_data = 1;
  string keyword = 2;
}

message TrainKWSResponse {
  bool trained = 1;
}

I want to clone this protos in a Go project and use the generated code. suppose the directory of the project is mygoproject. I want the generated code placed in mygoproject/protogen/kws/kws.pb.go for example. Also I want to script the whole procedure.

I issue the following command:

protoc -I/usr/local/include -I. --go_opt=Mprotos/common/common.proto=mygoproject/protogen/common   --go_opt=Mprotos/kws/kws.proto=mygoproject/protogen/kws  --go_out=mygoproject/protogen/kws --go-grpc_out=mygoproject/protogen/kws protos/common/common.proto   protos/kws/kws.proto

But protoc gives me the following error:

protoc-gen-go-grpc: unable to determine Go import path for "protos/common/common.proto"

Please specify either:
        • a "go_package" option in the .proto source file, or
        • a "M" argument on the command line.

See https://protobuf.dev/reference/go/go-generated#package for more information.

--go-grpc_out: protoc-gen-go-grpc: Plugin failed with status code 1.

How to solve this?


Solution

  • With the Help of another community I found the answer I post it here maybe it become useful for others

    protoc \
      -I/usr/local/include \
      -I. \
      --go_out=. \
      --go-grpc_out=. \
      --go_opt=Mprotos/common/common.proto=github.com/protogen/common \
      --go_opt=Mprotos/kws/kws.proto=github.com/protogen/kws \
      --go-grpc_opt=Mprotos/common/common.proto=github.com/protogen/common \
      --go-grpc_opt=Mprotos/kws/kws.proto=github.com/protogen/kws \
      protos/kws/kws.proto \
      protos/common/common.proto