Buf: Import protocol buffers from another project

2.4k Views Asked by At

I am using buf CLI to generate protos for Go via command buf generate.

I am able to generate protos successfully if any imports are defined in the same directory.

However, I could not import protos from another repository or even the same repository but different folder.

Here is how I tried to import error.proto from the same repository to service.proto. It might be a different repository as well.

syntax = "proto3";

option go_package = "github.com/organization/repository/service/proto/gen/service/v1;servicev1";

package service.v1;

import "github.com/organization/repository/common/proto/def/error/v1/error.proto";

This file relies inside directory:

service/proto/def/service/v1/service.proto

and my buf.gen.yaml look like

version: v1
plugins:
  - name: go
    out: ../../
    opt: module=github.com/organization/repository/service
  - name: go-grpc
    out: ../../
    opt: require_unimplemented_servers=false,module=github.com/organization/repository/service

I call buf generate in directory:

cd service/proto/def
buf generate

Error message is:

common/proto/def/error/v1/error.proto: does not exist

Is it possible to import that error.proto? If so, how can I do that?

1

There are 1 best solutions below

2
On

With the protobuf import statement, there is an expectation that it relates to some actual filesystem path.. be it relative to the directory including the file with the import statement, or relative to some path specified via -I / --proto_path.

So the import

import "github.com/organization/repository/common/proto/def/error/v1/error.proto";

..would imply that that path exists somewhere, but it clearly doesn't. Within a single repository, one simple option is to have your buf.work.yaml and buf.gen.yaml in the root, and use imports relative to the root of the repository. For imports relating to other repositories, you may find Buf Schema Registry (BSR) is helpful here.

See also Import protobuf file from GitHub repository