Skip to content

fix: register descriptor dependencies before AddSerializedFile (#150) - #255

Open
drewfustin wants to merge 2 commits into
betterproto:mainfrom
drewfustin:fix/issue-150-descriptor-pool
Open

fix: register descriptor dependencies before AddSerializedFile (#150)#255
drewfustin wants to merge 2 commits into
betterproto:mainfrom
drewfustin:fix/issue-150-descriptor-pool

Conversation

@drewfustin

@drewfustin drewfustin commented Sep 9, 2026

Copy link
Copy Markdown

Description

Fixes #150.

When google_protobuf_descriptors is enabled, generated modules call default_google_proto_descriptor_pool.AddSerializedFile(...) at import time. That fails if a proto imports another file whose descriptor is not already in the pool, like google/protobuf/descriptor.proto for custom options:

TypeError: Couldn't build proto file into descriptor pool: Depends on file 'google/protobuf/descriptor.proto', but it has not been loaded

With this fix, the compiler:

  1. Emits relative imports of each foreign dependency package immediately before AddSerializedFile so those modules register first (the workaround suggested in Using options results in descriptor pool error in generated code at import time. #150).
  2. Topologically sorts same-package input files by FileDescriptorProto.dependency before emitting registrations.

Note:
Package-level import cycles are not handled. The proto file graph is expected to be a DAG. File-granular registration would be a follow-up if maintainers want it.

Minimal repro from #150 (now covered by tests)

syntax = "proto3";
package descriptor_options;
import "google/protobuf/descriptor.proto";
extend google.protobuf.MessageOptions {
  string example_option = 50000;
}
message MyMessage {
  option (example_option) = "bar";
  string baz = 1;
}

Compiled with --python_betterproto2_opt=google_protobuf_descriptors now imports without error.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation / tooling

Checklist

  • New behaviour is covered by tests
  • Documentation updated if needed

Made with Cursor (Claude Fable 5 high + Composer 2.5)
and me ;)

Generated modules with google_protobuf_descriptors called AddSerializedFile
at import without registering imported files first, which raised TypeError
when a proto used custom options (issue betterproto#150) or other cross-package
descriptor deps. Import those packages and topo-sort same-package files
before registration.

Co-authored-by: Cursor <cursoragent@cursor.com>
Rename nested visit helper to _visit and tighten docstrings/comments to
match surrounding compiler style.

Co-authored-by: Cursor <cursoragent@cursor.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using options results in descriptor pool error in generated code at import time.

1 participant