This repository was archived by the owner on May 27, 2026. It is now read-only.
Resolve import cycle when enum is in same Go package#1378
Merged
Conversation
The externalEnums function compared proto package names to determine if an enum needed an external import. This is incorrect because two proto files can have different proto package names but share the same go_package option, placing them in the same Go package. When this happens, the generated .pb.validate.go file imports its own Go package, creating an import cycle that breaks go build. Changed the comparison from proto package name to Go import path using fns.ImportPath(), which is the same API that protoc-gen-star's own importableTypeName function uses for the identical purpose. Fixes bufbuild#585 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix self-import cycle in generated
*.pb.validate.gofiles when proto files within the same Go package reference each other's enum types.Problem
When proto file A imports proto file B, and both files share the same
go_packagebut have different proto package names, the generatedA.pb.validate.gofile imports its own Go package, creating an import cycle that breaksgo build.Example:
entity_admin.proto(proto packagejasper.admin.entity.v0, go_packageadmin/pb)document_admin.proto(proto packagejasper.admin.document.v0, go_packageadmin/pb)When
entity_admin.protousesDocumentVisibility(defined indocument_admin.proto) in a field with validation rules, the generated code produces:This pattern is common in codebases that vendor proto files from other services and set
go_packageto the local service path.Root Cause
The
externalEnumsfunction intemplates/goshared/register.godetermines whether an enum needs an external import by comparing proto package names:This is incorrect because two proto files can have different proto package names but share the same
go_packageoption, placing them in the same Go package. The comparison should use Go import paths instead.This regression was introduced when the original dual check (
ProtoName+PackageName) was simplified to onlyProtoNameduring the enum package collision fix.Fix
Changed the comparison from proto package name to Go import path using
fns.ImportPath(), which is the same API that protoc-gen-star's ownimportableTypeNamefunction (intype_name.go:38) uses for the identical purpose:Testing
go build ./...passes on the protoc-gen-validate repo itselfgo vet ./...passesRelated Issues
ensure the imports are usedis broken #585 —ensure the imports are usedis broken🤖 Generated with Claude Code