{"record":{"id":"e3be8e65f98e4716","repo":"rust-lang/rust","slug":"rustfmt-failed-with-exit-status","errorCode":null,"errorMessage":"rustfmt failed with {exit_status}","messagePattern":"rustfmt failed with (.+?)","errorType":"exception","errorClass":"FormatDiffError","httpStatus":null,"severity":"error","filePath":"src/tools/rustfmt/src/format-diff/main.rs","lineNumber":112,"sourceCode":"\n    let ranges_as_json = json::to_string(ranges).unwrap();\n\n    debug!(\"Files: {:?}\", files);\n    debug!(\"Ranges: {:?}\", ranges);\n\n    let rustfmt_var = env::var_os(\"RUSTFMT\");\n    let rustfmt = match &rustfmt_var {\n        Some(rustfmt) => rustfmt,\n        None => OsStr::new(\"rustfmt\"),\n    };\n    let exit_status = process::Command::new(rustfmt)\n        .args(files)\n        .arg(\"--file-lines\")\n        .arg(ranges_as_json)\n        .status()?;\n\n    if !exit_status.success() {\n        return Err(FormatDiffError::IoError(io::Error::new(\n            io::ErrorKind::Other,\n            format!(\"rustfmt failed with {exit_status}\"),\n        )));\n    }\n    Ok(())\n}\n\n/// Scans a diff from `from`, and returns the set of files found, and the ranges\n/// in those files.\nfn scan_diff<R>(\n    from: R,\n    skip_prefix: u32,\n    file_filter: &str,\n) -> Result<(HashSet<String>, Vec<Range>), FormatDiffError>\nwhere\n    R: io::Read,\n{\n    let diff_pattern = format!(r\"^\\+\\+\\+\\s(?:.*?/){{{skip_prefix}}}(\\S*)\");","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/src/tools/rustfmt/src/format-diff/main.rs#L94-L130","documentation":"Returned by rustfmt's format-diff tool when the spawned rustfmt process exits with a non-success status. The exit status is interpolated into the message and wrapped as ErrorKind::Other (FormatDiffError::IoError). format-diff feeds git-diff ranges to rustfmt via --file-lines; a non-zero exit means rustfmt rejected the input or hit an internal error.","triggerScenarios":"Running `rustfmt --file-lines <json>` (typically via git diff integration / format-diff) where rustfmt exits non-zero - syntax error in the ranged file, invalid --file-lines JSON, rustfmt config error, or an internal rustfmt panic. format-diff checks exit_status.success() and raises this error.","commonSituations":"Unparsable Rust in the diff range; mismatched rustfmt version that does not support --file-lines; nightly-only config option used on stable rustfmt; malformed file-lines JSON; partial edits that confuse the range formatter.","solutions":["Run the exact rustfmt invocation from the error's context manually to see the real diagnostic.","Ensure the rustfmt version matches what format-diff expects (nightly for --file-lines).","Fix syntax errors in the files included in the diff ranges.","Validate the --file-lines JSON structure against the expected schema."],"exampleFix":"# before: rustfmt --file-lines fails opaquely\n\n# after: reproduce and read the real error\nrustfmt --file-lines file_lines.json --edition 2021 src/lib.rs\necho $?   # inspect rustfmt's own stderr for the cause","handlingStrategy":"try-catch","validationCode":"fn file_lines_json_valid(json: &str) -> bool {\n    serde_json::from_str::<Vec<serde_json::Value>>(json).is_ok()\n}\n// Before invoking rustfmt --file-lines, assert file_lines_json_valid(&ranges_as_json).","typeGuard":null,"tryCatchPattern":"let status = Command::new(rustfmt).args(files).arg(\"--file-lines\").arg(json).status()?;\nif !status.success() {\n    // capture rustfmt's own stderr to report the real diagnostic\n    return Err(format!(\"rustfmt failed with {status}\").into());\n}\nOk(())","preventionTips":["Reproduce the exact rustfmt invocation manually to read its real stderr.","Use a rustfmt version that supports --file-lines (typically nightly).","Validate the file-lines JSON and fix syntax errors in diffed files before formatting."],"tags":["rustfmt","format-diff","cli","exit-status","git-diff"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}