You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ruff format --check outputs wrong line line/column numbers on syntax errors #24528
bugAn issue describing something that isn't working, or a PR that fixes a bugdiagnosticsDiagnostic messages, annotations, fixes & rendering infra--not for PRs that just add new diagnosticspreviewRelated to preview-mode features
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.pyerror: Failed to parse ex.py:2:9: Expected an indented block after `if` statement
❯ uvx [email protected] format --isolated --check ex.pyerror: Failed to parse ex.py:2:9: Expected an indented block after `if` statement
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 file = SourceFileBuilder::new(path.to_string_lossy(),"").finish();
let span = Span::from(file);
letmut 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:
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).
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.
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.
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.
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.
bugAn issue describing something that isn't working, or a PR that fixes a bugdiagnosticsDiagnostic messages, annotations, fixes & rendering infra--not for PRs that just add new diagnosticspreviewRelated to preview-mode features
Activity
dylwil3 commentedon Apr 10, 2026
Hm, I'm not able to reproduce this. Moreover, the second snippet looks more like the beginning of the output for
ruff checknotruff 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?DetachHead commentedon Apr 10, 2026
hmm looks like it only happens in preview mode
dylwil3 commentedon Apr 10, 2026
Thank you, that's what I was missing!
ntBre commentedon Apr 10, 2026
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:
ruff/crates/ruff/src/commands/format.rs
Lines 884 to 890 in 2ad94df
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 likesource[range]whererangeis past the end of the emptysource).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 of1:1anyway.I think we can address that last part by changing our behavior to build a real header here:
ruff/crates/ruff_annotate_snippets/src/renderer/display_list.rs
Lines 1237 to 1241 in e89f8ef
either by passing a real
bodytoformat_headeror by computing the header information separately (which seems preferable to avoid rendering the body just to process its lines).dylwil3 commentedon Apr 11, 2026
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:This works but feels a little wonky... I wonder if there is a not so painful way I can pass something like the
Spanalong with theParseErrorall 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.MichaReiser commentedon Apr 11, 2026
I haven't looked too closely at the implementation but what I'm wondering:
Diagnostic? Or why is it that we defer constructing theDiagnostic?ntBre commentedon Apr 13, 2026
I don't quite remember why we deferred the
Diagnosticconversion. It may just be to support the stable behavior of not converting them, but that sounds like a better approach in the long term.dylwil3 commentedon Apr 23, 2026
I think we should fix this at the same time that we stabilize the
ruff format --checkoutputs (as in #20755). Once we don't have to support the current stable behavior, we can create theDiagnosticmuch earlier, which will automatically handle the span etc. (We can also remove the structDisplayParseErrorentirely.)That does mean it will have to wait for the next minor release, but that's not too far off.
Stabilize `format --check` output formats (#26960)
Stabilize `format --check` output formats (#26960)
Fix `format --check` spans for syntax errors (#27045)
ntBre commentedon Jul 21, 2026
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.Stabilize `format --check` output formats (#26960)
Fix `format --check` spans for syntax errors (#27045)
Fix `format --check` spans for syntax errors (#27045)
Stabilize `format --check` output formats (#26960)
Fix `format --check` spans for syntax errors (#27045)
Stabilize `format --check` output formats (#26960)
Fix `format --check` spans for syntax errors (#27045)
Vendor latest annotate-snippets (#27033)