Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

A Go program panics at start-up because a protobuf file or name is registered twice

Your error

panic: proto: file "foo.proto" is already registered3 more lines it can show

Fix it fast

Most common cause, 1 of 3

The same .proto file was generated into two Go packages

Delete your own generated copy and import the owner's Go package everywhere.

Fix
# stopgap only: build with the warn policy baked in
go build -ldflags "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn" .
# real fix: drop the local copy, then tidy imports
go mod tidy
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

The panic proto: file "x.proto" is already registered means one .proto file was compiled into two Go packages and both are linked into the binary, so the global protobuf registry sees it twice. Generic package names and an old genproto module cause the same start-up panic. Keep one generated package per .proto file; google.golang.org/protobuf v1.36.12 still panics by default.

  • Applies to google.golang.org/protobuf v1.36.12, protoc-gen-go v1.36.12, Go 1.27
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Switch the conflict policy to warn for one run. The program keeps starting and prints every clash, each with the two Go import paths that registered it, so you can see which cause you have.

Run this first
GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn go run . 2>&1 | grep -A3 WARNING

Then find the line you see in this list and open its fix:

  1. 1The same .proto file was generated into two Go packagesThe message says is already registered and lists previously from and currently from with two different import paths
  2. 2Two different .proto files share a package name or have noneThe message says has a name conflict over a full name such as service.Request, and the two files are different
  3. 3An old google.golang.org/genproto still ships well-known typesThe panic says duplicate registration of google/protobuf/field_mask.proto, api.proto, type.proto or source_context.proto and the generated definition has moved

What does the error look like?

  • panic: proto: file "foo.proto" is already registered
  • proto: file "api/v1/user.proto" has a name conflict over acme.user.v1.User
  • See https://protobuf.dev/reference/go/faq#namespace-conflict
  • panic: duplicate registration of "google/protobuf/field_mask.proto"

Cause 1 of 3

The same .proto file was generated into two Go packages

You seeThe message says is already registered and lists previously from and currently from with two different import paths

Check

A proto file is already registered panic with two import paths means one .proto file was turned into Go code twice, usually because a copy was vendored into your repo while a dependency ships its own generated package. Search the repo for local copies of that file.

Check
find . -name "foo.proto" -not -path "./vendor/*"
grep -rn "go_package" --include=*.proto .

Fix

Delete your own generated copy and import the owner's Go package everywhere. If the owner publishes no go_package for the file, agree on one central package with them instead of each team generating its own. The warn policy, set at build time, only hides the clash while you clean up.

Fix
# stopgap only: build with the warn policy baked in
go build -ldflags "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn" .
# real fix: drop the local copy, then tidy imports
go mod tidy

Verify

Start the binary without the environment variable or ldflags. It boots with no panic and no WARNING lines.

Verify
go build -o app . && ./app

Confirmed by protobuf.dev

Did this fix it?

Cause 2 of 3

Two different .proto files share a package name or have none

You seeThe message says has a name conflict over a full name such as service.Request, and the two files are different

Check

When the panic reads has a name conflict over pkg.Name, two separate files declared the same fully qualified name. Files with no package line, or with a short generic one like service or common, collide easily once several teams link into one binary.

Check
grep -rnE "^package " --include=*.proto .

Fix

Give each file a package name that nobody else will pick, for example prefixed with your company and product, then regenerate the Go code. Changing a package is a breaking change for gRPC service names, Any payloads and extensions, so plan it with the clients.

Fix
# in billing.proto
package acme.billing.v1;
option go_package = "example.com/acme/billing/v1;billingv1";

protoc --go_out=. --go_opt=paths=source_relative billing.proto

Verify

Run the warn command again. The name conflict line for that type is gone.

Verify
GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn go run . 2>&1 | grep -c WARNING

Confirmed by protobuf.dev

Did this fix it?

Cause 3 of 3

An old google.golang.org/genproto still ships well-known types

You seeThe panic says duplicate registration of google/protobuf/field_mask.proto, api.proto, type.proto or source_context.proto and the generated definition has moved

Check

A duplicate registration panic for field_mask, api, type or source_context means an outdated genproto module still registers types that now live in google.golang.org/protobuf/types/known. The registry code asks for genproto at commit cb27e3aa (May 2020) or later.

Check
go list -m google.golang.org/genproto

Fix

Upgrade genproto, which is the command the panic itself prints, then tidy the module file.

Fix
go get -u google.golang.org/genproto
go mod tidy

Verify

go list shows a recent pseudo-version and the program starts without the duplicate registration message.

Verify
go list -m google.golang.org/genproto && go run .

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Never commit generated .pb.go files for a .proto you do not own; import the owner's module.
  • Start every .proto with a package line that includes your organisation and a version, such as acme.billing.v1.
  • Run the binary once in CI with no conflict override, so a new duplicate fails the build instead of production start-up.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.