golang / protobuf

Go support for Google's protocol buffers
BSD 3-Clause "New" or "Revised" License
9.76k stars 1.58k forks source link

devsite: update tutorial for specifying the Go import path #1297

Open oldgnoah1379 opened 3 years ago

oldgnoah1379 commented 3 years ago

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

neild commented 3 years ago

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".)

dsnet commented 3 years ago

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

anr commented 3 years ago

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";
neild commented 3 years ago

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

anr commented 3 years ago

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.

neild commented 3 years ago

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

anr commented 3 years ago

Makes sense, thanks again!

amyxguo commented 3 years ago

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

dsnet commented 3 years ago

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
amirhoseinjfri commented 3 years ago

@neild Thanks . it just made my day sir

ZhixinLai commented 3 years ago

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?

frederikhors commented 3 years ago

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

neild commented 3 years ago

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.

frederikhors commented 3 years ago

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

Can we close this issue?

dsnet commented 3 years ago

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