7

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.

xpt
  • 20,363
  • 37
  • 127
  • 216
  • Please can you clarify the sentence "*to generate Go code for both a client and server that are part of different modules*". Do you want to have a module for the client and a module for the server or do you want to compile different files to different import paths? Note that `protoc` doesn't care about *modules*, it only generates Go code. How you distribute that code is up to you – blackgreen May 04 '22 at 06:32
  • Can I ask what's the purpose ? Why not having a separate module for your protos and specify a `go_package`? – Clément Jean May 04 '22 at 07:40
  • 1
    @ClémentJean / @blackgreen, put it is way, if I put `go_package` in the .proto source file, then it'll tie to a specific repo. But for such sample repo, if people to fork it elsewhere, it'll be more convenient to set the import path on cli for protoc, instead of hard-coding it. But regardless for any reason, if the 2nd option is available, I want to know how to make use of it. – xpt May 04 '22 at 13:58
  • https://developers.google.com/protocol-buffers/docs/reference/go-generated#package provides an example using the `-M` option – menghanl May 04 '22 at 17:40
  • @menghanl, yep, that url is part of the error output. If you take a look at the 2nd command in my OP, you can see that I'm following the example using the -M option from the official doc, yet, still getting that _"unable to determine Go import path"_ error. – xpt May 04 '22 at 21:23
  • 1
    did you find a solution to this? – Chris G. Jun 27 '22 at 14:51

2 Answers2

7

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

Chris G.
  • 23,930
  • 48
  • 177
  • 302
  • 1
    I find it very disappointing that your answer is the only source of info I've found that has finally helped me achieve the simple task of choosing where do I want the generated go code to go. The docs are un-findable, and the comments around the web do not really address this simple task. Thank you @Chris G. – pablete Sep 20 '22 at 23:24
  • you could check it from this documentation https://developers.google.com/protocol-buffers/docs/reference/go-generated#package – Kelwin Tantono Oct 14 '22 at 12:03
  • 1
    fwiw.- I agree with @pablete - that blurb in the docs doesn't actually explain whether to use relative path or absolute path in the PROTO_FILE or the GO_IMPORT_PATH - also - the GO_IMPORT_PATH seems to be a weird combination of both namespace and file path. I was never able to get the "-go_opt=M" command line arg to work - and just resorted to dumping the "option go_package = ..." into my .proto files. I had used these same proto files to generate one language - but couldn't get them to work for golang. – Tim Oct 16 '22 at 15:03
  • In my case M option doesn't work .... it's ok with option go_package but it's doesn't see --go_opt=M... Have no idea why it happening – falko Jul 21 '23 at 08:00
2

You need to add the path in the command or in the proto file

syntax = "proto3";

option go_package = "pkg/api";

this is the same for java, and python as well.

option java_package = "pkg/api";
option py_generic_services = "pkg/api";

As for the command

Makefile

PROJ_PATH=${CURDIR}

.PHONY: proto
proto: ## Generate protobuf code
# Compile proto files inside the project.
    protoc api.proto --proto_path=${PROJ_PATH}/proto --go_out=. --go-grpc_out=.
Mike3355
  • 11,305
  • 24
  • 96
  • 184