A Go program panics at start-up because a protobuf file or name is registered twice
Fix it fast
Most common cause, 1 of 3The same .proto file was generated into two Go packages
Delete your own generated copy and import the owner's Go package everywhere.
# 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 tidyShort 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.
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.
GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn go run . 2>&1 | grep -A3 WARNINGThen find the line you see in this list and open its fix:
- 1The same .proto file was generated into two Go packages
The message says is already registered and lists previously from and currently from with two different import paths - 2Two different .proto files share a package name or have none
The message says has a name conflict over a full name such as service.Request, and the two files are different - 3An old google.golang.org/genproto still ships well-known types
The 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 registeredproto: file "api/v1/user.proto" has a name conflict over acme.user.v1.UserSee https://protobuf.dev/reference/go/faq#namespace-conflictpanic: 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.
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.
# 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 tidyVerify
Start the binary without the environment variable or ldflags. It boots with no panic and no WARNING lines.
go build -o app . && ./appConfirmed by protobuf.dev
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.
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.
# 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.protoVerify
Run the warn command again. The name conflict line for that type is gone.
GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn go run . 2>&1 | grep -c WARNINGConfirmed by protobuf.dev
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.
go list -m google.golang.org/genprotoFix
Upgrade genproto, which is the command the panic itself prints, then tidy the module file.
go get -u google.golang.org/genproto
go mod tidyVerify
go list shows a recent pseudo-version and the program starts without the duplicate registration message.
go list -m google.golang.org/genproto && go run .Confirmed by raw.githubusercontent.com
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.