Skip to content

Sanitize proto comments for rustdoc in generated code#25

Merged
iainmcgin merged 1 commit intomainfrom
fix/rustdoc-proto-comments
Apr 1, 2026
Merged

Sanitize proto comments for rustdoc in generated code#25
iainmcgin merged 1 commit intomainfrom
fix/rustdoc-proto-comments

Conversation

@iainmcgin
Copy link
Copy Markdown
Collaborator

Problem

PR #7 added proto-comment passthrough — proto // comments are emitted as rustdoc /// on generated types. Upstream WKT and descriptor proto comments contain markdown that rustdoc misparses:

  • [google.protobuf.Duration][] → treated as a broken intra-doc link; errors under #![deny(rustdoc::broken_intra_doc_links)] (4 errors in buffa-types/src/generated/google.protobuf.any.rs).
  • Bare https://…rustdoc::bare_urls warning (1 in buffa-descriptor).
  • Option<T> etc. → rustdoc::invalid_html_tags (48 warnings in buffa-test).

This broke cargo doc --workspace --no-deps on main and would break the docs.rs build on publish. Prerequisite for v0.3.0.

Fix

Adds sanitize_line in buffa-codegen/src/comments.rs, applied to prose lines only (indented/fenced code blocks are left untouched):

Input Output
[foo] / [foo][] / [foo][bar] \\[foo\\] etc.
[text](url) (single-line) unchanged
bare https://… <https://…>
existing <https://…> unchanged
Option<T> Option\\<T\\>
`code [x]` unchanged (CommonMark run-length backtick spans)
already-escaped \\[ unchanged

No new dependencies — hand-rolled byte scanner (all sentinels are ASCII so byte-level comparison is UTF-8-safe). Approach is similar in spirit to prost-build's sanitize_line, but additionally handles [foo][] collapsed reference links and <T> HTML-tag escaping, which prost handles only via its optional cleanup-markdown (pulldown-cmark) feature.

Known limitation: multi-line markdown links (e.g. [24-hour linear\nsmear](url) in timestamp.proto) are conservatively escaped — they degrade to literal text plus a clickable autolink. Cosmetic only; preferable to a docs.rs build failure.

Verification

  • cargo doc --workspace --no-deps — clean (0 errors, 0 warnings)
  • task lint — clean
  • task test — 1390 passed, 0 failed
  • 24 new unit-test cases for sanitize_line

Regenerated checked-in WKT (buffa-types/src/generated/) and bootstrap descriptor (buffa-descriptor/src/generated/) types. Also includes drive-by cargo fmt for two spots in buffa-codegen that had drifted on main.

Proto-comment passthrough (#7) emits proto source comments as rustdoc
`///` lines. Upstream WKT/descriptor proto comments contain markdown
that rustdoc misparses:

- `[google.protobuf.Duration][]` is treated as a broken intra-doc link
  and fails under `deny(rustdoc::broken_intra_doc_links)` (4 errors in
  buffa-types/src/generated/google.protobuf.any.rs).
- Bare URLs trigger `rustdoc::bare_urls` (1 warning in
  buffa-descriptor).
- `Option<T>` etc. trigger `rustdoc::invalid_html_tags` (48 warnings
  in buffa-test).

This broke `cargo doc --workspace --no-deps` and would break the
docs.rs build on publish.

Adds `sanitize_line` in buffa-codegen/src/comments.rs which is applied
to prose lines (code blocks left untouched):

- Escapes `[`/`]` to `\[`/`\]` unless part of a single-line
  inline link `[text](url)`.
- Wraps bare `http(s)://` URLs in `<...>`.
- Escapes `<`/`>` to `\<`/`\>` unless part of an existing
  `<http(s)://...>` autolink.
- Preserves backtick code spans (CommonMark run-length matching).
- Passes through already-escaped `\[` etc.

Regenerated checked-in WKT and bootstrap descriptor types.

Includes drive-by `cargo fmt` for two spots in buffa-codegen that had
drifted on main.
@iainmcgin iainmcgin marked this pull request as ready for review April 1, 2026 18:16
@iainmcgin iainmcgin requested a review from asacamano April 1, 2026 18:20
@iainmcgin iainmcgin mentioned this pull request Apr 1, 2026
5 tasks
@iainmcgin iainmcgin merged commit 48e9426 into main Apr 1, 2026
7 checks passed
@iainmcgin iainmcgin deleted the fix/rustdoc-proto-comments branch April 1, 2026 20:21
@github-actions github-actions bot locked and limited conversation to collaborators Apr 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants