proto-gen-go: generation of GO files without go_package option when proto has import - go

There is gRPC api https://github.com/LTD-Beget/antivirus/tree/master/proto/v1
I want to generate files for GO from this API, but there is no way to add go_package option.
In old version of protoc was cmd option as import_path, and it can can be generated for example by next command
root#d887087e9fa2:/shared/api# protoc -I/usr/local/include -I/shared/api --go_out=import_path=antivirus_proto_v1,M/shared/api/antivirus/proto/v1/antivirus.proto=antivirus_proto_v1,M/shared/api/antivirus/proto/v1/structures.proto=antivirus_proto_v1,plugins=grpc:internal/grpc /shared/api/antivirus/proto/v1/antivirus.proto /shared/api/antivirus/proto/v1/structures.proto
But with new version of protoc
protoc-gen-go: no such flag -import_path
--go_out: protoc-gen-go: Plugin failed with status code 1.
follow the guidelines
In order to generate Go code, the Go package's import path must be provided for every .proto file (including those transitively depended upon by the .proto files being generated). There are two ways to specify the Go import path:
by declaring it within the .proto file, or
by declaring it on the command line when invoking protoc.
but when i declaring it on cmd
root#d887087e9fa2:/shared/api# protoc -I/usr/local/include -I/shared/api --go_out=generated --go-grpc_out=generated --go_opt=M/shared/api/antivirus/proto/v1/antivirus.proto=antivirus/antivirus --go_opt=M/shared/api/antivirus/proto/v1/structures.proto=antivirus/structures /shared/api/antivirus/proto/v1/antivirus.proto /shared/api/antivirus/proto/v1/structures.proto
protoc-gen-go: unable to determine Go import path for "antivirus/proto/v1/structures.proto"
Please specify either:
• a "go_package" option in the .proto source file, or
• a "M" argument on the command line.
See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.
what am i missing?

Related

Separate client and server generated by protoc

I am trying to have protoc-generated server interface and client implementation in separate packages
The header part of my .proto files is the following:
syntax = "proto3";
option go_package = "github.com/<username>/<myservice>/pkg/grpc";
And I am using this command to generate .go files:
protoc --go_out=. --go_opt=paths=source_relative\
--go-grpc_out=. --go-grpc_opt=paths=source_relative\
pkg/grpc/*.proto
It generates pkg/grpc/<name>.pb.go files containing models and pkg/grpc/<name>_grpc.pb.go files containing server interface and client implementation (picture)
But I want the server to go to, say internal/pkg/grpc/, while the client and the models remaining inside pkg/grpc/, and the server correctly importing the models.
Versions:
protoc version is libprotoc 3.19.0
protoc-gen-go-grpc version is protoc-gen-go-grpc 1.1.0
protoc-gen-go version is protoc-gen-go v1.27.1
I am new to golang and protobuf, so if whatever I am asking happens to be bad practice, feel free to point me to the idiomatic one
It seems there isn't an option to do this. The plugin protoc-gen-go-grpc writes the output service code to the same file with _grpc.pb.go suffix, where "service" includes both client and server code.
You can only define different output paths per plugin:
protoc-gen-go supports --go_out and --go_opt flags
protoc-gen-go-grpc supports --go-grpc_out and --go-grpc_opt flags

How do I import annotations into proto?

I am trying to import annotations into my proto file. I copied the "google" folder to "project / proto/google".
import "google/api/annotations.proto";
the path google / api / annotations.proto - the project is located.
I am tried use the go mode init and vendor, it didn`t help
In the proto file, only google/protobuf offers autocomplete and that's it, I don't see such a folder in GOPATH
What do I do to make it work?
Edit 1-
enter image description here
I use
protoc -I $GOPATH/src/github.com/jeka2708/test-grpc/proto/ --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/login.proto
google/api/annotations.proto is not part of the base standard of include files that comes with the protoc compiler distribution (see latest here).
To incorporate it in your protoc build - simply download the version you need (latest, you'll also need it's included dependency http.proto) - and place it in a directory who's suffix matches the import path i.e.
/SOME/DIRECTORY/PATH/google/api/annotations.proto
/SOME/DIRECTORY/PATH/google/api/http.proto
The prefix path could be the system default include path (/usr/include) e.g.
/usr/include/google/api/annotations.proto
/usr/include/google/api/http.proto
or you can place in some project directory:
/home/user/my_proj/google/api/annotations.proto
/home/user/my_proj/google/api/http.proto
and the protoc compiler will default to looking in the current working directory - or you can specify custom include paths via the command-line options:
Usage: ./protoc [OPTION] PROTO_FILES
Parse PROTO_FILES and generate output based on the options given:
-IPATH, --proto_path=PATH Specify the directory in which to search for
imports. May be specified multiple times;
directories will be searched in order. If not
given, the current working directory is used.
If not found in any of the these directories,
the --descriptor_set_in descriptors will be
checked for required proto file.

WARNING: Missing 'go_package' option

Im getting the following error once i run the protocol command to generate pb.go file
But I can generate the pb.go file. how do I mitigate the following error
Missing 'go_package' option in "job.proto",
please specify it with the full Go package path as
a future release of protoc-gen-go will require this be specified.
"Import path" is the path another package would use to import the generated code, e.g. github.com/me/myproject/model or You can simply define the import path based on your preference.
You can simply define your optional import path as follows
option go_package = ".;<Your_Import_path>";
For example, we can assume the package path as "/pub", So the statement as follows.
option go_package = ".;pub";
Then you can simply execute the protoc command to generate the pb.go file
protoc -I=<ABS_PATH_OUTPUT_DIR> --go_out=<ABS_PATH_PROTO_FILE>

Errors compiling google-fhir proto files in Go

I am unable to compile google's fhir proto for go
1. I was able to generate annotations.pb.go with warnings which can be resolved
protoc --proto_path=proto --go_out=. proto/annotations.proto
2020/05/27 12:42:17 WARNING: Missing 'go_package' option in "annotations.proto",
please specify it with the full Go package path as
a future release of protoc-gen-go will require this be specified.
See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.
2. Unfortunately, I am unable to resolve fixing issues surrounding file not found.
Example the profile_config.proto which contained the following imports
import "proto/annotations.proto";
import "proto/r4/core/codes.proto";
import "proto/r4/core/datatypes.proto";
Attempting to executed resulted in "not found"
protoc --proto_path=proto --go_out=. proto/profile_config.proto
proto/annotations.proto: File not found.
proto/r4/core/codes.proto: File not found.
proto/r4/core/datatypes.proto: File not found.
Perhaps these proto files can only be used only with java and any other language would require modification on the files.
Regarding 1., you need to edit every .proto you want to compile and add the go_package option. For instance:
option go_package = "github.com/my-org/my-proj/go/gen/fhir/proto"
Regarding 2., you are setting --proto_path=proto, which leads protoc to search for proto/annotations.proto into the following path:
./proto/proto/annotations.proto
If you don't set this option or set it to --proto_path=. you will be able to compile.
I also suggest taking a look at this pull-request.

Understanding protobuf import and output relative paths

I am fairly certain this is operator error and I am at the point I am not thinking clearly.
Here is the setup:
$GOPATH/src/github.com/<company>/<service a>/proto/a.proto
$GOPATH/src/github.com/<company>/<service b>/proto/b.proto
etc.
Now in the proto file I am using imports similar to go (perhaps the issue) such that a.proto has:
import "github.com/<company>/<service b>/b.proto"
I have possibly two separate issues.
I cannot get the import to compile properly using go:generate protoc
I cannot get the output a.pb.go file to be placed in the $GOPATH/src/github.com/<company>/<service a>/proto/ path.
I have attempted multiple configurations probably not in the correct combination.
Using option go_package = "github.com/<company>/<service b>/proto" in each .proto file
Multiple variations of go generate;
go:generate protoc --proto_path=.:$GOPATH/src --go_out=$GOPATH/src a.proto
go:generate protoc --proto_path=.:$GOPATH/src --go_out=. a.proto
go:generate protoc --go_out=import_prefix=github.com/<company>/:. api.proto
I clearly have a poor understanding on how protoc looks at import paths and file outputs. Anyone point me in the direction of what I am doing wrong?
Thanks!
Update #1
In a.proto;
option go_package = "github.com/<company>/<service a>/proto";
import "github.com/<company>/<service b>/proto/b.proto";
and the go generate;
//go:generate protoc --proto_path=$GOPATH/src --go_out=$GOPATH/src/github.com/<company>/<service a>/proto a.proto
Which is called from a go file in the proto directory with the a.proto.
I received the error;
a.proto: File does not reside within any path specified using --proto_path (or -I). You must specify a --proto_path ch encompasses this file. Note that the proto_path must be an exact prefix of the .proto file names -- protoc is too dumb to figure out when two paths (e.g. absolute and relative) are equivalent (it's harder than you think).
I have confirmed $GOPATH is to the expected location.
Solution
Thanks to Shivam Jindal for pointing me in the correct direction. The import is exactly as described in his solution. The output was a problem of my misusing both --go_out and option go_package. I used the go_package to specify the output location and --go_out to specify the root similar to --proto_path. Now everything works.
option go_package = "github.com/<company>/<service a>/proto";
and
//go:generate protoc --proto_path=$GOPATH/src/ --go_out=$GOPATH/src/ $GOPATH/src/github.com/<company>/<service a>/proto/a.proto
Thanks!
Firstly, option go_package is not meant for other dependency import at all, it's the Go package name where the new proto bindings for Go (a.pb.go file) will be placed.
Now coming to the output file location, I can see you are using go-generate. Firstly it depends from which directory you are invoking that if the path used in --go_out= is relative path. I would say use absolute paths. If you want to put the output file in that location you mentioned, use --go_out=$GOPATH/src/github.com/<company>/<service a>/proto/ in go-generate.
To correctly import the other file b.proto in your a.proto use the fully qualified import path as you have done. Just that use --proto_path $GOPATH/src in go-generate. Also please update the question with the errors you are seeing in case it does not work.
Please see this for more information on import paths.

Resources