Skip to content

Commit 4a23756

Browse files
authored
Avoid caching files with unsupported syntax errors (#16425)
1 parent af62f79 commit 4a23756

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

crates/ruff/tests/lint.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2701,3 +2701,45 @@ match 2:
27012701
"
27022702
);
27032703
}
2704+
2705+
/// Regression test for <https://github.com/astral-sh/ruff/issues/16417>
2706+
#[test]
2707+
fn cache_syntax_errors() -> Result<()> {
2708+
let tempdir = TempDir::new()?;
2709+
fs::write(tempdir.path().join("main.py"), "match 2:\n case 1: ...")?;
2710+
2711+
let mut cmd = Command::new(get_cargo_bin(BIN_NAME));
2712+
// inline STDIN_BASE_OPTIONS to remove --no-cache
2713+
cmd.args(["check", "--output-format", "concise"])
2714+
.arg("--target-version=py39")
2715+
.arg("--preview")
2716+
.arg("--quiet") // suppress `debug build without --no-cache` warnings
2717+
.current_dir(&tempdir);
2718+
2719+
assert_cmd_snapshot!(
2720+
cmd,
2721+
@r"
2722+
success: false
2723+
exit_code: 1
2724+
----- stdout -----
2725+
main.py:1:1: SyntaxError: Cannot use `match` statement on Python 3.9 (syntax was added in Python 3.10)
2726+
2727+
----- stderr -----
2728+
"
2729+
);
2730+
2731+
// this should *not* be cached, like normal parse errors
2732+
assert_cmd_snapshot!(
2733+
cmd,
2734+
@r"
2735+
success: false
2736+
exit_code: 1
2737+
----- stdout -----
2738+
main.py:1:1: SyntaxError: Cannot use `match` statement on Python 3.9 (syntax was added in Python 3.10)
2739+
2740+
----- stderr -----
2741+
"
2742+
);
2743+
2744+
Ok(())
2745+
}

crates/ruff_linter/src/linter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ pub fn lint_only(
445445
&locator,
446446
&directives,
447447
),
448-
has_syntax_error: !parsed.is_valid(),
448+
has_syntax_error: !parsed.is_valid() || !parsed.unsupported_syntax_errors().is_empty(),
449449
}
450450
}
451451

@@ -546,7 +546,7 @@ pub fn lint_fix<'a>(
546546
);
547547

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

0 commit comments

Comments
 (0)