Skip to content

THRIFT-6059: add crate_prefix option to Rust codegen for cross-file imports#3546

Open
santiagomed wants to merge 1 commit into
apache:masterfrom
santiagomed:THRIFT-6059-rs-crate-prefix-option
Open

THRIFT-6059: add crate_prefix option to Rust codegen for cross-file imports#3546
santiagomed wants to merge 1 commit into
apache:masterfrom
santiagomed:THRIFT-6059-rs-crate-prefix-option

Conversation

@santiagomed
Copy link
Copy Markdown
Contributor

@santiagomed santiagomed commented May 27, 2026

Problem

The Rust code generator hardcodes use crate:: for cross-file imports (e.g., fileA.thrift includes fileB.thrift generates use 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 is super::, and users must post-process generated files with sed.

Fix

Add a crate_prefix generator option that controls the path prefix. Default remains crate for backward compatibility.

# Files at crate root — existing behavior, unchanged
thrift --gen rs -out src/ schema.thrift

# Files in a submodule
thrift --gen rs:crate_prefix=super -out src/thrift/ schema.thrift

Changes

  • t_rs_generator constructor reads crate_prefix from parsed_options
  • crate_prefix_ member stores the value (default: "crate")
  • Import generation in render_program_includes uses crate_prefix_ instead of hardcoded "crate"
  • THRIFT_REGISTER_GENERATOR updated to document the option

…mports

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.
@mergeable mergeable Bot added rust Pull requests that update Rust code compiler labels May 27, 2026
@santiagomed
Copy link
Copy Markdown
Contributor Author

@mhlakhani @Jens-G could I get a review here please? Also open to hearing other solutions to this issue with submodules!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler rust Pull requests that update Rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant