Skip to content

Commit 87e51e2

Browse files
committed
Fix format --check spans for syntax errors (#27045)
Summary -- This PR fixes #24528 by attaching more information to `FormatCommandError::Parse` instances. I made a couple of attempts at Micha's suggestion ([1]) to convert to `Diagnostic`s much earlier, but doing this only for errors was pretty awkward while still producing a much larger diff. I think it would be better to use diagnostics more deeply both for the error and non-error cases eventually, which should also help with #8232, but I considered the limited fix here a better stopgap for the 0.16 release. [1]: #24528 (comment) Test Plan -- Updated snapshots for two existing tests, plus an additional notebook test
1 parent afe2723 commit 87e51e2

2 files changed

Lines changed: 107 additions & 32 deletions

File tree

‎crates/ruff/src/commands/format.rs‎

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use ruff_linter::source_kind::{SourceError, SourceKind};
3434
use ruff_linter::warn_user_once;
3535
use ruff_python_ast::{PySourceType, SourceType};
3636
use 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};
3838
use ruff_text_size::{TextLen, TextRange, TextSize};
3939
use ruff_workspace::FormatterSettings;
4040
use 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)]
818826
pub(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

828840
impl 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.

‎crates/ruff/tests/cli/format.rs‎

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,11 @@ from module import =
577577
exit_code: 2
578578
----- stdout -----
579579
invalid-syntax: Expected an import name
580-
--> main.py:1:1
580+
--> main.py:2:20
581+
|
582+
2 | from module import =
583+
| ^
584+
|
581585
582586
583587
----- stderr -----
@@ -1944,9 +1948,8 @@ fn test_notebook_trailing_semicolon() -> Result<()> {
19441948
Ok(())
19451949
}
19461950

1947-
#[test]
1948-
fn syntax_error_in_notebooks() -> Result<()> {
1949-
let test = CliTest::with_files([
1951+
fn notebook_with_syntax_error() -> Result<CliTest> {
1952+
CliTest::with_files([
19501953
(
19511954
"ruff.toml",
19521955
r#"
@@ -2004,7 +2007,12 @@ include = ["*.ipy"]
20042007
}
20052008
"#,
20062009
),
2007-
])?;
2010+
])
2011+
}
2012+
2013+
#[test]
2014+
fn syntax_error_in_notebooks() -> Result<()> {
2015+
let test = notebook_with_syntax_error()?;
20082016

20092017
assert_cmd_snapshot!(test.format_command()
20102018
.args(["--config", "ruff.toml"])
@@ -2020,6 +2028,36 @@ include = ["*.ipy"]
20202028
Ok(())
20212029
}
20222030

2031+
#[test]
2032+
fn syntax_error_in_notebooks_check() -> Result<()> {
2033+
let test = notebook_with_syntax_error()?;
2034+
2035+
assert_cmd_snapshot!(
2036+
test.format_command()
2037+
.args(["--config", "ruff.toml"])
2038+
.args(["--extension", "ipy:ipynb"])
2039+
.arg("--check")
2040+
.arg("."),
2041+
@"
2042+
success: false
2043+
exit_code: 2
2044+
----- stdout -----
2045+
invalid-syntax: Expected an expression
2046+
--> main.ipy:cell 2:3:24
2047+
|
2048+
1 | for i in range(iterations):
2049+
2 | # выберите случайный индекс в диапазон от 0 до len(X)-1 включительно при помощи функции random.randint
2050+
3 | j = # ваш код здесь
2051+
| ^
2052+
|
2053+
2054+
2055+
----- stderr -----
2056+
"
2057+
);
2058+
Ok(())
2059+
}
2060+
20232061
#[test]
20242062
fn extension() -> Result<()> {
20252063
let test = CliTest::with_files([

0 commit comments

Comments
 (0)