Skip to content

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

@chriselion

Description

@chriselion

Summary

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

Reproduction Steps

I'm trying out betterproto2, and running to an exception in the generated code when trying to add options to some of my files. Here's a minimal example:

# in example.proto
syntax = "proto3";

package betterproto.repro;

import "google/protobuf/descriptor.proto";

message ExampleOptions {
  string foo = 1 [targets = TARGET_TYPE_MESSAGE];
}

extend google.protobuf.MessageOptions {
  optional ExampleOptions example_options = 50000;
}

message MyMessage {
    option (example_options).foo = "bar";
    string baz = 1;
}

compiled with

uv run python -m grpc_tools.protoc --python_betterproto2_out generated/ --python_betterproto2_opt=google_protobuf_descriptors -I. example.proto

and then try to import the generated code with

uv run python -c "from generated.betterproto.repro import MyMessage"

Expected Results

An exception is raised

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/[snip]/generated/betterproto/repro/__init__.py", line 52, in <module>
    EXAMPLE_PROTO_DESCRIPTOR = default_google_proto_descriptor_pool.AddSerializedFile(
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Couldn't build proto file into descriptor pool: Depends on file 'google/protobuf/descriptor.proto', but it has not been loaded

Actual Results

The generated code should import without errors.

Potential fix/workaround: If I add

from ...google import protobuf as ___google__protobuf__

in the generated file (near the top), that resolves the error.

System Information

I'm using uv instead of pip. I'm fairly certain that's not the issue.

$ uv run python -m grpc_tools.protoc --version                                                                                                   
libprotoc 31.1

$ uv run python --version                     
Python 3.12.11

$ uv pip show betterproto2 
Name: betterproto2
Version: 0.9.0
Location: [snip]/.venv/lib/python3.12/site-packages
Requires: python-dateutil, typing-extensions
Required-by: betterproto2-compiler

$ uv pip show betterproto2_compiler
Name: betterproto2-compiler
Version: 0.9.0
Location: [snip]/.venv/lib/python3.12/site-packages
Requires: betterproto2, jinja2, ruff, typing-extensions
Required-by:

Checklist

  • I have searched the issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have verified this issue occurs on the latest prelease of betterproto which can be installed using pip install -U --pre betterproto, if possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions