THRIFT-6059: add crate_prefix option to Rust codegen for cross-file imports#3546
Open
santiagomed wants to merge 1 commit into
Open
THRIFT-6059: add crate_prefix option to Rust codegen for cross-file imports#3546santiagomed wants to merge 1 commit into
santiagomed wants to merge 1 commit into
Conversation
Contributor
Author
|
@mhlakhani @Jens-G could I get a review here please? Also open to hearing other solutions to this issue with submodules! |
Member
Code reviewFound 1 issue:
thrift/compiler/cpp/src/thrift/generate/t_rs_generator.cc Lines 61 to 67 in 4ce4898 🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
…mports
Client: rs
The Rust code generator hardcodes 'use crate::' for cross-file imports
(e.g., when event.thrift includes trees.thrift). This assumes the
generated files are placed at the crate root (src/). When files are
placed in a submodule (e.g., src/thrift/), the correct prefix is
'super', and users must post-process with sed.
Add a crate_prefix generator option: --gen rs:crate_prefix=super
Default remains 'crate' for backward compatibility. An empty value is
rejected since it would silently emit 'use ::module;' (an external
crate path in Rust 2018+). Unknown options are reported with the
generator prefix ('unknown option rs:') per compiler convention.
Co-Authored-By: Grok 4.3 <noreply@x.ai>
4ce4898 to
320c6bd
Compare
Contributor
Author
Thanks for the review @Jens-G. Addressed this + added some additional fixes specified in the summary. |
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
The Rust code generator hardcodes
use crate::for cross-file imports (e.g.,fileA.thriftincludesfileB.thriftgeneratesuse crate::fileB;). This assumes the generated files are placed at the crate root (src/). When files are placed in a submodule (e.g.,src/thrift/), the correct prefix issuper::, and users must post-process generated files with sed.Fix
Add a
crate_prefixgenerator option that controls the path prefix. Default remainscratefor backward compatibility.Changes
t_rs_generatorconstructor readscrate_prefixfromparsed_optionscrate_prefix_member stores the value (default:"crate")render_attributes_and_includesusescrate_prefix_instead of hardcoded"crate"THRIFT_REGISTER_GENERATORupdated to document the optionrs:crate_prefix=) is rejected: it would silently emituse ::module;, which is an external-crate path in Rust 2018+unknown option rs:<name>, matching the convention used by other generatorsTesting
Verified manually with a two-file schema (
event.thriftincludingtrees.thrift):--gen rsuse crate::trees;(unchanged)--gen rs:crate_prefix=superuse super::trees;--gen rs:crate_prefix=crate::generateduse crate::generated::trees;--gen rs:crate_prefix=crate_prefix requires a non-empty value--gen rs:bogus=1unknown option rs:bogusThere is no existing test harness for rs generator options (
lib/rs/test_recursiveexercises cross-file imports with the default prefix only), so no automated test is included; happy to add one if reviewers have a preferred location.Portions of this change were AI-assisted (Grok 4.3, xAI) and reviewed by the author.