bufprototool

How to change naming convention of Go imports generated by Buf?


I am trying to set up Buf to generate Go stubs from my proto schemas but I'm having issues with the generated code compared to the tool that is already in place (Prototool).

Prototool generates me a Go file having imports like this:

import (
    my_package "github.com/my-org/proto-schemas/golang/gen/my-package"

But for a reason that I ignore for now (probably I'm missing a config but can't find it), Buf generates the same file but with this import:

import (
    mypackage "github.com/my-org/proto-schemas/golang/gen/mypackage"
    

There are probably different naming conventions between Prototool and Buf but I was hoping I could generate the same file (same imports) using Buf and Prototool. I there a way to do that?

My buf.gen.yaml looks like:

version: v1

managed:
  enabled: false

plugins:
  - plugin: go
    out: v3/golang/gen
    opt: paths=source_relative

My buf.yaml looks like:

version: v1

breaking:
  use:
    - FILE
lint:
  use:
    - DEFAULT

Solution

  • With managed mode disabled, buf does not modify Go imports at all.

    Is it possible that you have a option go_package = "github.com/my-org/proto-schemas/golang/gen/mypackage"; in your protobuf file? buf simply parses the protobuf files and passes them to the plugin protoc-gen-go, which will generate the Go import statement.

    I'm not familiar with prototool - it's possible that it modifies the option for you, which would explain the difference.

    buf can create the go_package options automatically with managed mode. With the following buf.gen.yaml, you do not have to set the go_package option anywhere in your protobuf files:

    version: v1
    
    managed:
      enabled: true
      go_package_prefix:
        default: github.com/my-org/proto-schemas/v3/golang/gen
    
    plugins:
      - plugin: buf.build/protocolbuffers/go:v1.31.0
        out: v3/golang/gen
        opt: paths=source_relative
    

    Here, the package names are automatically build from the protobuf package declaration, prefixed with the configured value.