Search code examples
goprotocol-buffersprotocgrpc-go

Solving protoc-gen-go: unable to determine Go import path problem by adding a "M" argument


Same symptom as protoc-gen-go: unable to determine Go import path for "simple.proto"

For simple proto file with following content.

syntax="proto3";

package main;

message Person {
      string name = 1;
      int32 age = 2; 
}

I am trying to generate go code for it using protoc. I run:

protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative simple.proto

I receive following error:

protoc-gen-go: unable to determine Go import path for "simple.proto"

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

All the answer there focus on the first option -- adding a "go_package" option in the .proto source file, but I'm looking for the answer for the second option "a "M" argument on the command line".

Same as the comments under https://stackoverflow.com/a/62540631/2125837

I'm looking for ways to change the module path via protoc, to generate Go code for both a client and server that are part of different modules, I tried using go_opt=module but it doesn't work with source_relative.

Is there any ways to make it work by adding "a "M" argument on the command line", instead of adding a "go_package" option in the .proto source file please?

Specifically, for the file of https://github.com/mmcc007/go/blob/master/examples/helloworld/helloworld/helloworld.proto

Here are my failed attempts:

$ protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative --proto_path=examples/helloworld/helloworld helloworld.proto 

protoc-gen-go: unable to determine Go import path for "helloworld.proto"
. . .
--go_out: protoc-gen-go: Plugin failed with status code 1.


$ protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative --proto_path=examples/helloworld/helloworld --go_opt=Mhelloworld.proto=github.com/mmcc007/go/blob/master/examples/helloworld/helloworld helloworld.proto 

protoc-gen-go-grpc: unable to determine Go import path for "helloworld.proto"
. . .
--go_out: protoc-gen-go: Plugin failed with status code 1.


Solution

  • I found this to be working using --proto_path for easy reference using "THE M FLAG" :-)

    protoc --proto_path=./proto \
      --go_out=./go \
      --go_opt=Mhelloworld.proto=example.com/project/protos/fizz \
      ./proto/helloworld.proto
    

    Note: The M before helloworld.proto