@@ -34,7 +34,7 @@ use ruff_linter::source_kind::{SourceError, SourceKind};
3434use ruff_linter:: warn_user_once;
3535use ruff_python_ast:: { PySourceType , SourceType } ;
3636use ruff_python_formatter:: { FormatModuleError , QuoteStyle , format_module_source, format_range} ;
37- use ruff_source_file:: { LineIndex , LineRanges , OneIndexed , SourceFileBuilder } ;
37+ use ruff_source_file:: { LineIndex , LineRanges , OneIndexed , SourceFile , SourceFileBuilder } ;
3838use ruff_text_size:: { TextLen , TextRange , TextSize } ;
3939use ruff_workspace:: FormatterSettings ;
4040use ruff_workspace:: resolver:: {
@@ -381,12 +381,7 @@ pub(crate) fn format_source(
381381
382382 let formatted = formatted. map_err ( |err| {
383383 if let FormatModuleError :: ParseError ( err) = err {
384- DisplayParseError :: from_source_kind (
385- err,
386- path. map ( Path :: to_path_buf) ,
387- source_kind,
388- )
389- . into ( )
384+ FormatCommandError :: parse ( err, path, source_kind)
390385 } else {
391386 FormatCommandError :: Format ( path. map ( Path :: to_path_buf) , err)
392387 }
@@ -429,15 +424,14 @@ pub(crate) fn format_source(
429424 format_module_source ( unformatted, options. clone ( ) ) . map_err ( |err| {
430425 if let FormatModuleError :: ParseError ( err) = err {
431426 // Offset the error by the start of the cell
432- DisplayParseError :: from_source_kind (
427+ FormatCommandError :: parse (
433428 ParseError {
434429 error : err. error ,
435430 location : err. location . checked_add ( * start) . unwrap ( ) ,
436431 } ,
437- path. map ( Path :: to_path_buf ) ,
432+ path,
438433 source_kind,
439434 )
440- . into ( )
441435 } else {
442436 FormatCommandError :: Format ( path. map ( Path :: to_path_buf) , err)
443437 }
@@ -584,7 +578,21 @@ impl<'a> FormatResults<'a> {
584578 output_format : OutputFormat ,
585579 errors : & [ FormatCommandError ] ,
586580 ) -> io:: Result < ( ) > {
587- let mut notebook_index = FxHashMap :: default ( ) ;
581+ let mut notebook_index = errors
582+ . iter ( )
583+ . filter_map ( |error| {
584+ if let FormatCommandError :: Parse {
585+ source_file,
586+ notebook_index : Some ( notebook_index) ,
587+ ..
588+ } = error
589+ {
590+ Some ( ( source_file. name ( ) . to_string ( ) , notebook_index. clone ( ) ) )
591+ } else {
592+ None
593+ }
594+ } )
595+ . collect ( ) ;
588596 let diagnostics: Vec < _ > = errors
589597 . iter ( )
590598 . map ( Diagnostic :: from)
@@ -817,7 +825,11 @@ impl<'a> FormatResults<'a> {
817825#[ derive( Error , Debug ) ]
818826pub ( crate ) enum FormatCommandError {
819827 Ignore ( #[ from] ignore:: Error ) ,
820- Parse ( #[ from] DisplayParseError ) ,
828+ Parse {
829+ error : DisplayParseError ,
830+ source_file : SourceFile ,
831+ notebook_index : Option < NotebookIndex > ,
832+ } ,
821833 Panic ( Option < PathBuf > , Box < PanicError > ) ,
822834 Read ( Option < PathBuf > , SourceError ) ,
823835 Format ( Option < PathBuf > , FormatModuleError ) ,
@@ -826,6 +838,24 @@ pub(crate) enum FormatCommandError {
826838}
827839
828840impl FormatCommandError {
841+ fn parse ( error : ParseError , path : Option < & Path > , source_kind : & SourceKind ) -> Self {
842+ let name = path. map_or_else ( || "-" . into ( ) , Path :: to_string_lossy) ;
843+ let source_file = SourceFileBuilder :: new ( name, source_kind. source_code ( ) ) . finish ( ) ;
844+ let notebook_index = source_kind
845+ . as_ipy_notebook ( )
846+ . map ( |notebook| notebook. index ( ) . clone ( ) ) ;
847+
848+ Self :: Parse {
849+ error : DisplayParseError :: from_source_kind (
850+ error,
851+ path. map ( Path :: to_path_buf) ,
852+ source_kind,
853+ ) ,
854+ source_file,
855+ notebook_index,
856+ }
857+ }
858+
829859 fn path ( & self ) -> Option < & Path > {
830860 match self {
831861 Self :: Ignore ( err) => {
@@ -835,7 +865,7 @@ impl FormatCommandError {
835865 None
836866 }
837867 }
838- Self :: Parse ( err ) => err . path ( ) ,
868+ Self :: Parse { error , .. } => error . path ( ) ,
839869 Self :: Panic ( path, _)
840870 | Self :: Read ( path, _)
841871 | Self :: Format ( path, _)
@@ -859,11 +889,15 @@ impl From<&FormatCommandError> for Diagnostic {
859889 FormatCommandError :: Ignore ( error) => {
860890 Diagnostic :: new ( DiagnosticId :: Io , Severity :: Error , error)
861891 }
862- FormatCommandError :: Parse ( display_parse_error) => Diagnostic :: new (
863- DiagnosticId :: InvalidSyntax ,
864- Severity :: Error ,
865- & display_parse_error. error ( ) . error ,
866- ) ,
892+ FormatCommandError :: Parse {
893+ error, source_file, ..
894+ } => {
895+ return Diagnostic :: invalid_syntax (
896+ source_file. clone ( ) ,
897+ & error. error ( ) . error ,
898+ error. error ( ) ,
899+ ) ;
900+ }
867901 FormatCommandError :: Panic ( path, panic_error) => {
868902 return create_panic_diagnostic ( panic_error, path. as_deref ( ) ) ;
869903 }
@@ -912,8 +946,8 @@ impl Display for FormatCommandError {
912946 )
913947 }
914948 }
915- Self :: Parse ( err ) => {
916- write ! ( f, "{err }" )
949+ Self :: Parse { error , .. } => {
950+ write ! ( f, "{error }" )
917951 }
918952 Self :: Read ( path, err) => {
919953 if let Some ( path) = path {
@@ -1228,7 +1262,6 @@ mod tests {
12281262 use insta:: assert_snapshot;
12291263
12301264 use ruff_db:: panic:: catch_unwind;
1231- use ruff_linter:: logging:: DisplayParseError ;
12321265 use ruff_linter:: source_kind:: { SourceError , SourceKind } ;
12331266 use ruff_python_formatter:: FormatModuleError ;
12341267 use ruff_python_parser:: { ParseError , ParseErrorType } ;
@@ -1258,14 +1291,14 @@ mod tests {
12581291 "Permission denied" ,
12591292 ) ) ) ,
12601293 } ) ,
1261- FormatCommandError :: Parse ( DisplayParseError :: from_source_kind (
1294+ FormatCommandError :: parse (
12621295 ParseError {
12631296 error : ParseErrorType :: UnexpectedIndentation ,
12641297 location : TextRange :: default ( ) ,
12651298 } ,
1266- Some ( path. clone ( ) ) ,
1299+ Some ( & path) ,
12671300 & source_kind,
1268- ) ) ,
1301+ ) ,
12691302 FormatCommandError :: Panic ( Some ( path. clone ( ) ) , Box :: new ( panic_error) ) ,
12701303 FormatCommandError :: Read (
12711304 Some ( path. clone ( ) ) ,
@@ -1304,9 +1337,6 @@ mod tests {
13041337 io: test.py: Permission denied
13051338 --> test.py:1:1
13061339
1307- invalid-syntax: Unexpected indentation
1308- --> test.py:1:1
1309-
13101340 io: File not found
13111341 --> test.py:1:1
13121342
@@ -1319,6 +1349,13 @@ mod tests {
13191349 invalid-cli-option: Range formatting is only supported for Python files.
13201350 --> test.py:1:1
13211351
1352+ invalid-syntax: Unexpected indentation
1353+ --> test.py:1:1
1354+ |
1355+ 1 | 1
1356+ | ^
1357+ |
1358+
13221359 panic: Panicked at <location> when checking `test.py`: `Test panic for FormatCommandError`
13231360 --> test.py:1:1
13241361 info: This indicates a bug in Ruff.
0 commit comments