Skip to content

ruff format --check outputs wrong line line/column numbers on syntax errors #24528

Closed

Description

@DetachHead

Summary

...
if asdf:

ruff format correctly says 2:9:

$ ruff format foo.py
error: Failed to parse foo.py:2:9: Expected an indented block after `if` statement

but adding --check causes it to incorrectly output 1:1:

$ ruff format --check foo.py
invalid-syntax: Expected an indented block after `if` statement
--> foo.py:1:1

Version

0.15.8

Activity

  1. dylwil3 commented on Apr 10, 2026

    @dylwil3
    Collaborator

    Hm, I'm not able to reproduce this. Moreover, the second snippet looks more like the beginning of the output for ruff check not ruff format --check (but when I tried that I still got the correct line number). Is there a missing step or configuration in the original example?

    ❯ cat ex.py
    ...
    if asdf:
    
    ❯ uvx [email protected] format --isolated ex.py
    error: Failed to parse ex.py:2:9: Expected an indented block after `if` statement
    
    ❯ uvx [email protected] format --isolated --check ex.py
    error: Failed to parse ex.py:2:9: Expected an indented block after `if` statement
  2. added
    needs-mre
    on Apr 10, 2026
  3. DetachHead commented on Apr 10, 2026

    @DetachHead
    ContributorAuthor

    hmm looks like it only happens in preview mode

    # ruff.toml
    preview = true
  4. self-assigned this
    on Apr 10, 2026
  5. added
    bug
    preview
    diagnostics
    and removed
    needs-mre
    on Apr 10, 2026
  6. dylwil3 commented on Apr 10, 2026

    @dylwil3
    Collaborator

    Thank you, that's what I was missing!

  7. ntBre commented on Apr 10, 2026

    @ntBre
    Contributor

    I looked into this a bit today (before I noticed you assigned yourself!), and there are a few problems with our current setup. First of all, we're not attaching a range to the diagnostic here:

    let annotation = error.path().map(|path| {
    let file = SourceFileBuilder::new(path.to_string_lossy(), "").finish();
    let span = Span::from(file);
    let mut annotation = Annotation::primary(span);
    annotation.hide_snippet(true);
    annotation
    });

    However, if we try to attach a range there, we'll immediately run into the empty source code passed to SourceFileBuilder::new (we try to render something like source[range] where range is past the end of the empty source).

    After that, we still run into the hide_snippet(true) part, which ends up marking the annotation as being "file-level" and rendered with a location of 1:1 anyway.

    I think we can address that last part by changing our behavior to build a real header here:

    let header = format_header(origin, main_range, &[], is_first, snippet.cell_index);
    return DisplaySet {
    display_lines: header.map_or_else(Vec::new, |header| vec![header]),
    margin: Margin::new(0, 0, 0, 0, term_width, 0),
    };

    either by passing a real body to format_header or by computing the header information separately (which seems preferable to avoid rendering the body just to process its lines).

  8. dylwil3 commented on Apr 11, 2026

    @dylwil3
    Collaborator

    Thanks @ntBre ! I noticed some of the same things.

    A minimal hack is to swoop in and re-read the source so you can provide a Span, like this:

    diff --git i/crates/ruff/src/commands/format.rs w/crates/ruff/src/commands/format.rs
    index 78cab1a17a..c12bde32fc 100644
    --- i/crates/ruff/src/commands/format.rs
    +++ w/crates/ruff/src/commands/format.rs
    @@ -893,11 +893,27 @@ impl From<&FormatCommandError> for Diagnostic {
                 FormatCommandError::Ignore(error) => {
                     Diagnostic::new(DiagnosticId::Io, Severity::Error, error)
                 }
    -            FormatCommandError::Parse(display_parse_error) => Diagnostic::new(
    -                DiagnosticId::InvalidSyntax,
    -                Severity::Error,
    -                &display_parse_error.error().error,
    -            ),
    +            FormatCommandError::Parse(display_parse_error) => {
    +                if let Some(path) = error.path() {
    +                    let file = SourceFileBuilder::new(
    +                        path.to_string_lossy(),
    +                        std::fs::read_to_string(path).unwrap(),
    +                    )
    +                    .finish();
    +                    let span = Span::from(file).with_range(display_parse_error.error().location);
    +                    return Diagnostic::invalid_syntax(
    +                        span,
    +                        display_parse_error.error().error.clone(),
    +                        display_parse_error.error().location,
    +                    );
    +                } else {
    +                    Diagnostic::new(
    +                        DiagnosticId::InvalidSyntax,
    +                        Severity::Error,
    +                        &display_parse_error.error().error,
    +                    )
    +                }
    +            }
                 FormatCommandError::Panic(path, panic_error) => {
                     return create_panic_diagnostic(panic_error, path.as_deref());
                 }
    

    This works but feels a little wonky... I wonder if there is a not so painful way I can pass something like the Span along with the ParseError all the way from the actual format invocation (where we already have the source code in hand)? I'll have to play around a bit more.

  9. MichaReiser commented on Apr 11, 2026

    @MichaReiser
    Member

    I haven't looked too closely at the implementation but what I'm wondering:

    • Do we need all these variants?
    • Could some of them be simple wrappers around Diagnostic? Or why is it that we defer constructing the Diagnostic?
  10. ntBre commented on Apr 13, 2026

    @ntBre
    Contributor

    I don't quite remember why we deferred the Diagnostic conversion. It may just be to support the stable behavior of not converting them, but that sounds like a better approach in the long term.

  11. added this to the v0.16 milestone on Apr 23, 2026
  12. dylwil3 commented on Apr 23, 2026

    @dylwil3
    Collaborator

    I think we should fix this at the same time that we stabilize the ruff format --check outputs (as in #20755). Once we don't have to support the current stable behavior, we can create the Diagnostic much earlier, which will automatically handle the span etc. (We can also remove the struct DisplayParseError entirely.)

    That does mean it will have to wait for the next minor release, but that's not too far off.

  13. self-assigned this
    on Jul 13, 2026
  14. added 3 commits that reference this issue on Jul 20, 2026
    56321b7
    da27e35
    e16d075
  15. ntBre commented on Jul 21, 2026

    @ntBre
    Contributor

    I'm going to consider the immediate issue here closed by #27045 but open a follow-up issue to track using Diagnostics in a better way, which I still think is the better solution.

  16. added 7 commits that reference this issue on Jul 21, 2026
    c240166
    a517be3
    afdfaa3
    2d89f5f
    62c1edc
    cad676a
    87e51e2
  17. added a commit that references this issue on Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugdiagnosticspreview

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions