{"record":{"id":"eb4e9c0757efc870","repo":"astral-sh/ruff","slug":"content-not-correctly-formatted","errorCode":null,"errorMessage":"Content not correctly formatted","messagePattern":"Content not correctly formatted","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ruff_python_formatter/src/main.rs","lineNumber":34,"sourceCode":"\nfn main() -> Result<()> {\n    let cli: Cli = Cli::parse();\n\n    if cli.files.is_empty() {\n        if !matches!(cli.emit, None | Some(Emit::Stdout)) {\n            bail!(\n                \"Can only write to stdout when formatting from stdin, but you asked for {:?}\",\n                cli.emit\n            );\n        }\n        let source = read_from_stdin()?;\n        // It seems reasonable to give this a dummy name\n        let formatted = format_and_debug_print(&source, &cli, Path::new(\"stdin.py\"))?;\n        if cli.check {\n            if formatted == source {\n                return Ok(());\n            }\n            bail!(\"Content not correctly formatted\")\n        }\n        stdout().lock().write_all(formatted.as_bytes())?;\n    } else {\n        for file in &cli.files {\n            let source = fs::read_to_string(file)\n                .with_context(|| format!(\"Could not read {}: \", file.display()))?;\n            let formatted = format_and_debug_print(&source, &cli, file)?;\n            match cli.emit {\n                Some(Emit::Stdout) => stdout().lock().write_all(formatted.as_bytes())?,\n                None | Some(Emit::Files) => {\n                    fs::write(file, formatted.as_bytes()).with_context(|| {\n                        format!(\"Could not write to {}, exiting\", file.display())\n                    })?;\n                }\n            }\n        }\n    }\n","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_python_formatter/src/main.rs#L16-L52","documentation":"When the formatter debug binary reads from stdin with `--check`, it formats the input and compares it to the original; if they differ it bails with \"Content not correctly formatted\" (anyhow) instead of writing output, mirroring `ruff format --check` semantics.","triggerScenarios":"Running the formatter binary with stdin input and `--check` when the input is not formatter-clean — e.g. inconsistent quoting, wrong indentation style, or missing trailing newline.","commonSituations":"CI checks verifying formatting of piped files; pre-commit style checks using the debug binary; any file that `ruff format` would rewrite.","solutions":["Run the binary without `--check` (or run `ruff format`) to actually rewrite the source to canonical form","Fix the formatting difference reported by running the formatter in write mode and diffing","Exclude generated or intentionally unformatted files from the check"],"exampleFix":"# before (fails: content not formatted)\ncat messy.py | ruff_python_formatter --check\n# after\ncat messy.py | ruff_python_formatter > messy.py && cat messy.py | ruff_python_formatter --check","handlingStrategy":"try-catch","validationCode":"# verify formatting before --check\nruff format --check file.py || ruff format file.py","typeGuard":null,"tryCatchPattern":"import subprocess\np = subprocess.run(['ruff_python_formatter', '--check'], input=src.encode(), capture_output=True)\nif p.returncode != 0 and b'not correctly formatted' in p.stderr:\n    formatted = subprocess.run(['ruff_python_formatter'], input=src.encode(), capture_output=True).stdout\n    src = formatted","preventionTips":["Run the formatter in write mode before using --check in CI","Exclude generated files from format checks","Keep sources formatter-clean by running format-on-save"],"tags":["ruff","formatter","cli","check-mode"],"backgroundTag":"content-not-formatted","analyzedSha":"26f38c119cac42e4d320ba08f09224fdec74af2c","analyzedAt":"2026-09-05T10:32:37.492Z","contentChangedAt":"2026-09-05T10:32:37.492Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}