Skip to content

Troubleshooting code generation#

How to check if generated code is up-to-date#

Run buf generate and check for a diff in your VCS (either locally or as part of your CI workflow).

C# error: Failure: file "..." was generated multiple times#

C# plugins require you to set a base_namespace option in buf.gen.yaml to tell the plugin to generate files in a directory hierarchy matching the class namespace. A configuration similar to this should fix the issue:

version: v1
managed:
  enabled: true
plugins:
  - plugin: buf.build/protocolbuffers/csharp
    out: gen/proto/csharp
    opt: base_namespace=NAMESPACE

Python error: ModuleNotFoundError: No module named 'buf…'#

This is a known issue with generating Python code. For a simple workaround, we recommend creating an __init__.py file in the root of your generated code.

PHP gRPC plugin doesn't generate interfaces with the Buf CLI but does generate them with protoc.#

The php_generic_services file option must be set to generate interfaces, but Protobuf removed the option. It only works with older versions of protoc and doesn't work with the Buf CLI.

Referencing imports#

If buf generate can't find a file in an import statement, it's usually because of a mismatch between the relative locations of the input to the buf generate command and the location specified in the import path. The buf generate command uses the location of your workspace's buf.yaml file as the root by default, so given this directory structure:

.
├── buf.gen.yaml
├── buf.yaml
└── proto
    ├── foo
    │   └── foo.proto
    └── bar
        └── bar.proto

If you run buf generate from the root, then your import paths should be an absolute path from that location. For example, if you wanted bar.proto to import foo.proto, the statement should be:

proto/bar/bar.proto
import "proto/foo/foo.proto";

However, if you specify an input to buf generate other than the module's root, the input is prepended to the import path when looking for the file. For example, if you pass the proto directory as the input:

$ buf generate proto

then the Buf CLI looks for foo.proto at proto/proto/foo/foo.proto, which doesn't exist. You would need to change your import path to be:

proto/bar/bar.proto
import "foo/foo.proto";

If you get an "unknown type" error when trying to use imported types, it's because you need to reference your types using the full package name when importing from another package. Using the above directory structure, importing Foo into your bar.proto file looks like this:

proto/bar/bar.proto
syntax = "proto3";

package proto.bar;

import "proto/foo/foo.proto";

message Bar {
  proto.foo.Foo myFoo = 1;  // Note the full package name
}