5

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. 
4
  • Does this answer your question? Using a shared external package for proto files? Commented Sep 23, 2021 at 10:13
  • @blackgreen I didnot get the first answer.. second answer is providing the solution to use protofiles in my project in another. But what do I do if the project from which i want to import proto isn't maintained by me? Commented Sep 23, 2021 at 10:28
  • it was suggested to clone the remote repo somewhere in your project, possibly as a git submodule Commented Sep 23, 2021 at 10:30
  • 1
    I should regularly keep updating it right? Isn't that a downside. Any other cleaner way? Commented Sep 23, 2021 at 10:41

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.