Skip to content

Commit 950b63a

Browse files
macalinaoiainmcgin
andauthored
Add protobuf source comment support to generated Rust code (#7)
## Summary - Extract comments from protobuf `SourceCodeInfo` and emit them as Rust doc comments (`///`) on generated structs, fields, enums, enum variants, oneof enums, and view types - Add `comments.rs` module that translates index-based `SourceCodeInfo` paths into an FQN-keyed `HashMap` on `CodeGenContext`, so codegen call sites look up comments by the proto FQN they already have - Add `--include_source_info` to all `protoc` invocations (`buffa-build`, WKT generation, bootstrap generation) - Combine proto source comments with existing tag docs (e.g. `Field 1: \`seconds\``) using a blank-line separator - Regenerate all checked-in generated code (WKTs + bootstrap descriptor types) with full proto documentation ## Test plan - [x] All 1,244 existing tests pass - [x] Clippy clean (zero warnings) - [x] 17 new tests: 11 unit tests in `comments.rs` (edge cases: empty SCI, nested types, whitespace-only, detached comments, empty package) + 6 integration tests in `tests/comments.rs` (end-to-end through `generate()`) - [x] Verified generated WKT types have full proto documentation (e.g. `Timestamp` struct has 50+ line doc comment from proto source) - [x] Verified bootstrap descriptor types have proto comments on fields - [x] CI: lint-and-test, check-generated-code, conformance suite --------- Co-authored-by: Iain McGinniss <309153+iainmcgin@users.noreply.github.com>
1 parent a03474c commit 950b63a

24 files changed

+3214
-13
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ jobs:
128128
# /usr/local/include) so the checked-in code is pinned to a
129129
# specific proto source, not whatever protoc version ships.
130130
protoc --descriptor_set_out=/tmp/wkt.pb --include_imports \
131+
--include_source_info \
131132
-I buffa-types/protos \
132133
google/protobuf/any.proto \
133134
google/protobuf/duration.proto \

Taskfile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ tasks:
433433
Requires protoc on PATH.
434434
cmds:
435435
- >-
436-
protoc --descriptor_set_out=/tmp/wkt.pb --include_imports
436+
protoc --descriptor_set_out=/tmp/wkt.pb --include_imports --include_source_info
437437
-I buffa-types/protos
438438
google/protobuf/any.proto
439439
google/protobuf/duration.proto

buffa-build/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ fn invoke_protoc(
519519

520520
let mut cmd = Command::new(&protoc);
521521
cmd.arg("--include_imports");
522+
cmd.arg("--include_source_info");
522523
cmd.arg(format!(
523524
"--descriptor_set_out={}",
524525
descriptor_path.display()
@@ -555,6 +556,9 @@ fn invoke_protoc(
555556
/// module-root-relative; passing user paths to both would be a contradiction.
556557
/// Codegen filtering happens on our side via `files_to_generate` matching.
557558
fn invoke_buf() -> Result<Vec<u8>, Box<dyn std::error::Error>> {
559+
// buf build includes SourceCodeInfo by default (there's an
560+
// --exclude-source-info flag to disable it), so proto comments
561+
// propagate to generated code without an explicit opt-in here.
558562
let output = Command::new("buf")
559563
.arg("build")
560564
.arg("--as-file-descriptor-set")

0 commit comments

Comments
 (0)