Add protobuf source comment support to generated Rust code#7
Merged
iainmcgin merged 6 commits intoanthropics:mainfrom Mar 24, 2026
Merged
Conversation
Extract comments from SourceCodeInfo in .proto files and emit them as Rust doc comments on generated structs, fields, enums, enum variants, oneof enums, and view types. Comments are keyed by fully-qualified protobuf name on CodeGenContext for simple lookup at codegen call sites. - Add comments.rs module with FQN-keyed comment extraction from SourceCodeInfo index-based paths - Add --include_source_info to protoc invocations (buffa-build, gen-wkt-types, gen-bootstrap-types) - Combine proto source comments with existing tag docs using blank line separator via doc_attrs_with_tag() - Regenerate WKT and bootstrap types with full proto documentation
|
All contributors have signed the CLA ✍️ ✅ |
Contributor
Author
|
I have read the CLA Document and I hereby sign the CLA |
Proto source comments (especially WKTs like Timestamp, Duration, Any) contain C++/Java/Python code examples indented with 4 spaces. Rustdoc interprets these as Rust code blocks and tries to compile them as doc tests, causing 56 failures. Detect indented blocks in doc_lines_to_tokens and wrap them in ```text fences so rustdoc renders them as plain text code blocks. Regenerate all checked-in generated code.
- Strip the 4-space indent from lines inside ```text fences since the fence already denotes a code block - Close code blocks before trailing blank lines by looking ahead to see if the next non-empty line is still indented - Keep blank lines within multi-part examples (e.g. Win32 example with an internal comment block)
Collaborator
|
similar to #5 , I think this just needs to regenerate the bootstrap/WKT/example code to pass the CI check: |
Contributor
Author
Thanks, will do shortly. |
# Conflicts: # buffa-codegen/src/generated/google.protobuf.compiler.plugin.rs # buffa-codegen/src/generated/google.protobuf.descriptor.rs # buffa-codegen/src/message.rs # buffa-codegen/src/view.rs # buffa-types/src/generated/google.protobuf.any.rs # buffa-types/src/generated/google.protobuf.duration.rs # buffa-types/src/generated/google.protobuf.field_mask.rs # buffa-types/src/generated/google.protobuf.struct.rs # buffa-types/src/generated/google.protobuf.timestamp.rs # buffa-types/src/generated/google.protobuf.wrappers.rs # examples/logging/src/gen/context.v1.context.rs # examples/logging/src/gen/log.v1.log.rs
The check-generated-code job was regenerating WKT types without proto source comments because the inline protoc invocation lacked --include_source_info. The Taskfile and gen-bootstrap-types.sh already have the flag; this brings CI into alignment.
iainmcgin
approved these changes
Mar 24, 2026
Collaborator
|
Thank you so much for this! |
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
SourceCodeInfoand emit them as Rust doc comments (///) on generated structs, fields, enums, enum variants, oneof enums, and view typescomments.rsmodule that translates index-basedSourceCodeInfopaths into an FQN-keyedHashMaponCodeGenContext, so codegen call sites look up comments by the proto FQN they already have--include_source_infoto allprotocinvocations (buffa-build, WKT generation, bootstrap generation)Field 1: \seconds``) using a blank-line separatorTest plan
comments.rs(edge cases: empty SCI, nested types, whitespace-only, detached comments, empty package) + 6 integration tests intests/comments.rs(end-to-end throughgenerate())Timestampstruct has 50+ line doc comment from proto source)