3

Inside city.proto file I want to use (import) protobuf's .proto files. In my golang application I use go modules.

city.proto:

syntax = "proto3";

package proto;

import "google/protobuf/timestamp.proto";

option go_package = "./proto";

message City {
    google.protobuf.Timestamp create_at = 1;
}

When I try to generate code from city.proto file it raise such error:

google/protobuf/timestamp.proto: File not found.
city.proto:3:1: Import "google/protobuf/timestamp.proto" was not found or had errors.
city.proto:25:5: "google.protobuf.Timestamp" is not defined.

I created a proto folder in the directory of my gRPC project. The city.proto file is located in this folder. I run such command:

protoc -I proto/ proto/city.proto --go_out=plugins=grpc:proto/city.

This command works only in cases when I don't use import in the proto file.

go version:

go version go1.12.9 windows/amd64

protoc --version:

libprotoc 3.11.4

echo %GOPATH%:

C:\Users\NNogerbek\go

Inside that directory I see three folders with such structure:

bin
    protoc.exe
    protoc-gen-go.exe
pkg
    mod
        **packages**
src
    google.com
        protobuf
            timestamp.proto

I run such command:

go list -f "{{ .Path }} {{ .Dir }}" -m github.com/golang/protobuf

Command result:

github.com/golang/protobuf C:\Users\NNogerbek\go\pkg\mod\github.com\golang\protobuf@v1.4.0
Nurzhan Nogerbek
  • 4,806
  • 16
  • 87
  • 193
  • What command did you run to generate your code? – Richard Belleville Apr 29 '20 at 19:28
  • I created a `proto` folder in the directory of my gRPC project. The `city.proto` file is located in this folder. I run such command: `protoc -I proto/ proto/city.proto --go_out=plugins=grpc:proto/city`. This command works in cases when I don't use import in the proto file. – Nurzhan Nogerbek Apr 30 '20 at 03:18
  • After numerous attempts, I found a way to generate the code without errors using the following command: `protoc proto/city.proto -I. -I%GOPATH%/src --go_out=:.`. How do you think is it fine to specify the path to the `src` folder in the command? – Nurzhan Nogerbek Apr 30 '20 at 04:36
  • I also want to use proto files of the third party implementation [gogo/protobuf](https://github.com/gogo/protobuf) in my own proto file. For example `import "github.com/gogo/protobuf/gogoproto/gogo.proto";`. It seems like the problem with the visibility of the files has not been resolved. – Nurzhan Nogerbek Apr 30 '20 at 05:29

1 Answers1

3

After numerous attempts, I found a way to generate the go code to my gRPC server without errors using the following command:

protoc -I. -I%GOPATH%/src --gogofaster_out=plugins=grpc:. proto/city.proto

As you can see in the command, I specify the path to the src folder where the protobuf's files are located.

Also I used gogofaster plugin of gogoprotobuf package.

Nurzhan Nogerbek
  • 4,806
  • 16
  • 87
  • 193