protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

devsite: update tutorial for specifying the Go import path

Open oldgnoah1379 opened this issue 3 years ago • 15 comments

I cannot generate code, even though I have followed the tutorial correctly the result is always: protoc-gen-go: unable to determine Go import path for "user/user.proto" error_proto

oldgnoah1379 avatar Mar 19 '21 15:03 oldgnoah1379

https://developers.google.com/protocol-buffers/docs/reference/go-generated#package

As the error says, you need to specify the import path of the Go package containing the .proto file. The simplest way to do this is with a go_package option:

option go_package = "example.com/package/name";

(In the case of the screenshot above, the package name will be the module name in the go.mod plus "/user".)

neild avatar Mar 19 '21 16:03 neild

Changing this issue to be about documentation. The tutorial needs to be updated.

dsnet avatar Mar 19 '21 18:03 dsnet

I previously had this on my proto file:

option go_package = ".;proto";

Changing it to something like "example.com/package/name" has the inconvenient effect of having

protoc -Iproto proto/test.proto --go_out=/tmp/proto

output the code to /tmp/proto/example.com/package/name/test.pb.go.

Is there a way to output the code /tmp/proto/test.pb.go?

Thanks

Update: I think this works:

option go_package = "/;proto";

anr avatar Mar 26 '21 20:03 anr

Is there a way to output the code /tmp/proto/test.pb.go?

Pass --go_opt=paths=source_relative to protoc.

See: https://developers.google.com/protocol-buffers/docs/reference/go-generated#invocation

neild avatar Mar 30 '21 21:03 neild

Thanks, @neild, it's a cleaner way.

However, it looks like this option doesn't apply to --go-grpc_out; tested with:

option go_package = "example.com/package/name";

and

protoc -Iproto proto/test.proto --go_opt=paths=source_relative --go_out=foo --go-grpc_out=foo

If I am not missing anything, maybe I should open an issue on https://github.com/grpc/grpc-go.

anr avatar Mar 30 '21 22:03 anr

You need to provide the option to both plugins: --go-grpc_opt=paths=source_relative.

neild avatar Mar 30 '21 22:03 neild

Makes sense, thanks again!

anr avatar Mar 31 '21 17:03 anr

I tried passing --go_opt=paths=source_relative to protoc but got Unknown flag: --go_opt. Has anyone else encountered this?

amyxguo avatar Apr 08 '21 00:04 amyxguo

That means that the version of the protobuf toolchain is too old. I'm not sure when the "_opt" feature was introduced, but you can find the latest releases here: https://github.com/protocolbuffers/protobuf/releases

Alternatively, I you can fold the options into the "go_out" flag:

--go_out=paths=source_relative:$OUT_DIR

dsnet avatar Apr 08 '21 00:04 dsnet

@neild Thanks . it just made my day sir

amirhoseinjfri avatar Apr 10 '21 08:04 amirhoseinjfri

Thanks, @neild, it's a cleaner way.

However, it looks like this option doesn't apply to --go-grpc_out; tested with:

option go_package = "example.com/package/name";

and

protoc -Iproto proto/test.proto --go_opt=paths=source_relative --go_out=foo --go-grpc_out=foo

If I am not missing anything, maybe I should open an issue on https://github.com/grpc/grpc-go.

How to define the path to import the package in the client/server code?

ZhixinLai avatar Apr 11 '21 01:04 ZhixinLai

I think --go_opt=paths=source_relative should be the default.

frederikhors avatar Apr 15 '21 18:04 frederikhors

I think --go_opt=paths=source_relative should be the default.

Unfortunately impossible to change without breaking existing users.

Also, I strongly suspect --go_opt=module=<module> is better for most cases these days.

neild avatar Apr 15 '21 19:04 neild

You right! I tried it now, it's working really well.

Can we close this issue?

frederikhors avatar Apr 15 '21 21:04 frederikhors

The devsite tutorial probably still needs to be updated. I'll look into it early next week.

dsnet avatar Apr 15 '21 21:04 dsnet