Skip to content

Commit

Permalink
Avoid caching files with unsupported syntax errors (#16425)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntBre authored Feb 28, 2025
1 parent af62f79 commit 4a23756
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
42 changes: 42 additions & 0 deletions crates/ruff/tests/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2701,3 +2701,45 @@ match 2:
"
);
}

/// Regression test for <https://github.com/astral-sh/ruff/issues/16417>
#[test]
fn cache_syntax_errors() -> Result<()> {
let tempdir = TempDir::new()?;
fs::write(tempdir.path().join("main.py"), "match 2:\n case 1: ...")?;

let mut cmd = Command::new(get_cargo_bin(BIN_NAME));
// inline STDIN_BASE_OPTIONS to remove --no-cache
cmd.args(["check", "--output-format", "concise"])
.arg("--target-version=py39")
.arg("--preview")
.arg("--quiet") // suppress `debug build without --no-cache` warnings
.current_dir(&tempdir);

assert_cmd_snapshot!(
cmd,
@r"
success: false
exit_code: 1
----- stdout -----
main.py:1:1: SyntaxError: Cannot use `match` statement on Python 3.9 (syntax was added in Python 3.10)
----- stderr -----
"
);

// this should *not* be cached, like normal parse errors
assert_cmd_snapshot!(
cmd,
@r"
success: false
exit_code: 1
----- stdout -----
main.py:1:1: SyntaxError: Cannot use `match` statement on Python 3.9 (syntax was added in Python 3.10)
----- stderr -----
"
);

Ok(())
}
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ pub fn lint_only(
&locator,
&directives,
),
has_syntax_error: !parsed.is_valid(),
has_syntax_error: !parsed.is_valid() || !parsed.unsupported_syntax_errors().is_empty(),
}
}

Expand Down Expand Up @@ -546,7 +546,7 @@ pub fn lint_fix<'a>(
);

if iterations == 0 {
is_valid_syntax = parsed.is_valid();
is_valid_syntax = parsed.is_valid() && parsed.unsupported_syntax_errors().is_empty();
} else {
// If the source code was parseable on the first pass, but is no
// longer parseable on a subsequent pass, then we've introduced a
Expand Down

0 comments on commit 4a23756

Please sign in to comment.