klin
klin

Reputation: 133

Import .proto files from another project

I want to import .proto file defined in different project into my golang project and use a message type defined in it.

Proto file I want to import is: https://github.com/lyft/clutch/blob/main/api/k8s/v1/k8s.proto

I have added the import statement as :

import "github.com/lyft/clutch/api/k8s/v1/k8s.proto";

to use message type "Job" in that file, I added

message Jobs {
repeated clutch.k8s.v1.Job job = 1;
}

When I try to compile proto file, I'm getting the error

Import "github.com/lyft/clutch/blob/main/api/k8s/v1/k8s.proto" was not found or had errors.

"clutch.k8s.v1.Job" is not defined.

Upvotes: 5

Views: 12149

Answers (1)

Matteo
Matteo

Reputation: 39390

In order to compile your proto, you should clone the dependency repos and set as include path in the protoc import, as example:

job.proto

syntax = "proto3";

package clutch.k8s.v1;

// degine
option go_package = "github.com/job";

import "api/k8s/v1/k8s.proto";


message Jobs {
  repeated clutch.k8s.v1.Job job = 1;
}


compile.sh

proto_out_dir=.
GOBIN=~/go/bin

protoc \
      --go_out "${proto_out_dir}" \
      --go_opt paths=source_relative \
      --go-grpc_out "${proto_out_dir}" \
      --go-grpc_opt require_unimplemented_servers=false,paths=source_relative \
      --plugin protoc-gen-go="${GOBIN}/protoc-gen-go" \
      --plugin protoc-gen-go-grpc="${GOBIN}/protoc-gen-go-grpc" \
--go-grpc_opt=paths=source_relative \
--proto_path=../protoc-gen-validate \
--proto_path=../api-common-protos \
--proto_path=../clutch \
--proto_path=../clutch/api \
--proto_path=. \
job.proto

Will produce:

job.pb.go

...
type Jobs struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields

    Job []*v1.Job `protobuf:"bytes,1,rep,name=job,proto3" json:"job,omitempty"`
}
...

See also how the proto are build in the source repo here

Upvotes: 4

Related Questions