DioxusLabs/dioxus · error
fmt_file should only be called on valid syn::File files
Error message
fmt_file should only be called on valid syn::File files
What it means
Deprecated fmt_file panics when the input source cannot be parsed by syn — i.e. the caller passed Rust code that is not syntactically valid. The replacement try_fmt_file returns this as a Result instead.
Source
Thrown at packages/autofmt/src/lib.rs:45
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, PartialEq, Eq, Hash)]
pub struct FormattedBlock {
/// The new contents of the block
pub formatted: String,
/// The line number of the first line of the block.
pub start: usize,
/// The end of the block, exclusive.
pub end: usize,
}
/// Format a file into a list of `FormattedBlock`s to be applied by an IDE for autoformatting.
///
/// It accepts
#[deprecated(note = "Use try_fmt_file instead - this function panics on error.")]
pub fn fmt_file(contents: &str, indent: IndentOptions) -> Vec<FormattedBlock> {
let parsed =
syn::parse_file(contents).expect("fmt_file should only be called on valid syn::File files");
try_fmt_file(contents, &parsed, indent).expect("Failed to format file")
}
/// Format a file into a list of `FormattedBlock`s to be applied by an IDE for autoformatting.
///
/// This function expects a complete file, not just a block of code. To format individual rsx! blocks, use fmt_block instead.
///
/// The point here is to provide precise modifications of a source file so an accompanying IDE tool can map these changes
/// back to the file precisely.
///
/// Nested blocks of RSX will be handled automatically
///
/// This returns an error if the rsx itself is invalid.
///
/// Will early return if any of the expressions are not complete. Even though we *could* return the
/// expressions, eventually we'll want to pass off expression formatting to rustfmt which will reject
/// those.
pub fn try_fmt_file(View on GitHub (pinned to 24f6a829df)
Solutions
- Only call fmt_file with content that parses as a complete Rust file (syn::File); for fragments, format them within a full file or use the appropriate fragment API.
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at packages/autofmt/src/lib.rs:45 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of DioxusLabs/dioxus@24f6a829df (2026-08-23).
Data as JSON: /api/errors/27879de86173b762.
Report an issue: GitHub.