astral-sh/ruff · error

Content not correctly formatted

Error message

Content not correctly formatted

What it means

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.

Source

Thrown at crates/ruff_python_formatter/src/main.rs:34

fn main() -> Result<()> {
    let cli: Cli = Cli::parse();

    if cli.files.is_empty() {
        if !matches!(cli.emit, None | Some(Emit::Stdout)) {
            bail!(
                "Can only write to stdout when formatting from stdin, but you asked for {:?}",
                cli.emit
            );
        }
        let source = read_from_stdin()?;
        // It seems reasonable to give this a dummy name
        let formatted = format_and_debug_print(&source, &cli, Path::new("stdin.py"))?;
        if cli.check {
            if formatted == source {
                return Ok(());
            }
            bail!("Content not correctly formatted")
        }
        stdout().lock().write_all(formatted.as_bytes())?;
    } else {
        for file in &cli.files {
            let source = fs::read_to_string(file)
                .with_context(|| format!("Could not read {}: ", file.display()))?;
            let formatted = format_and_debug_print(&source, &cli, file)?;
            match cli.emit {
                Some(Emit::Stdout) => stdout().lock().write_all(formatted.as_bytes())?,
                None | Some(Emit::Files) => {
                    fs::write(file, formatted.as_bytes()).with_context(|| {
                        format!("Could not write to {}, exiting", file.display())
                    })?;
                }
            }
        }
    }

View on GitHub (pinned to 26f38c119c)

Solutions

  1. Run the binary without `--check` (or run `ruff format`) to actually rewrite the source to canonical form
  2. Fix the formatting difference reported by running the formatter in write mode and diffing
  3. Exclude generated or intentionally unformatted files from the check

Example fix

# before (fails: content not formatted)
cat messy.py | ruff_python_formatter --check
# after
cat messy.py | ruff_python_formatter > messy.py && cat messy.py | ruff_python_formatter --check
Defensive patterns

Strategy: try-catch

Validate before calling

# verify formatting before --check
ruff format --check file.py || ruff format file.py

Try / catch

import subprocess
p = subprocess.run(['ruff_python_formatter', '--check'], input=src.encode(), capture_output=True)
if p.returncode != 0 and b'not correctly formatted' in p.stderr:
    formatted = subprocess.run(['ruff_python_formatter'], input=src.encode(), capture_output=True).stdout
    src = formatted

Prevention

When it happens

Trigger: 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.

Common situations: CI checks verifying formatting of piped files; pre-commit style checks using the debug binary; any file that `ruff format` would rewrite.

Related errors


AI-assisted analysis of astral-sh/ruff@26f38c119c (2026-09-05). Data as JSON: /api/errors/eb4e9c0757efc870. Report an issue: GitHub.