codegen: add file_per_package option for BSR cargo-SDK compatibility#73
Merged
codegen: add file_per_package option for BSR cargo-SDK compatibility#73
Conversation
Inlines what the per-file stitcher would include!, producing the same __buffa:: module structure in a single file per package. Intended for BSR generated SDKs whose lib.rs synthesis builds the module tree from <dotted.package>.rs filenames.
- Richer parity test: fixture now has a oneof + nested type; the stitcher's include!s are spliced with content-file bodies and the brace-depth-tracked pub mod sequence is compared against the per-package output. - Doc updates: GeneratedFile/GeneratedFileKind note the per-package mode; field doc states the silent-partial-output failure mode under strategy:directory with split packages. - Rename package_filename -> package_to_filename for consistency with package_to_mod_filename. - Cross-feature test: file_per_package + generate_text + nested type, asserting register_types paths resolve.
|
All contributors have signed the CLA ✍️ ✅ |
kollektiv
approved these changes
Apr 29, 2026
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.
Adds a
file_per_packageoption toCodeGenConfig(and theprotoc-gen-buffaCLI) that emits one<dotted.package>.rsper proto package instead of the default per-proto-file split. The single file inlines what the<pkg>.mod.rsstitcher would otherwiseinclude!, producing the identical__buffa::module structure.Why
BSR's cargo-SDK pipeline assembles
lib.rsserver-side by splitting each output filename on.and building a nestedpub modtree — the same convention prost's per-package output follows. Our default per-file output (pet.v1.pet.rs,pet.v1.pet.__view.rs,pet.v1.mod.rs, ...) would synthesize to garbage under that algorithm. Withfile_per_package=trueset in the BSR plugin manifest'sregistry.opts, our output slots into the existing synthesis with no special-casing on Buf's side. See bufbuild/plugins#2333.This mode is correct under
strategy: directoryforPACKAGE_DIRECTORY_MATCH-clean modules (one directory == one package, so each invocation sees a complete package), so it does not introduce astrategy: allrequirement.Implementation
PackageSectionsintermediate holds per-sectionVec<TokenStream>s;generate_package_modnow consumes it uniformly. The per-file path builds sections frominclude!token streams; the per-package path inlines the content directly. Module structure is identical between modes.package_to_filename()helper for<pkg>.rsnaming.GeneratedFileKind::PackageModis reused for the single-file output.Deferred (Low review findings)
OutputLayoutenum instead ofbool— only one alternate layout exists; revisit if more emerge.__buffa.rs, which under BSR's synthesis would double-nest. Buf'sPACKAGE_DEFINEDlint (MINIMAL group) makes this a near-zero case for SDK gen, and it mirrors the existing per-file path's behavior.