Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid caching files with unsupported syntax errors #16425

Merged
merged 3 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should have a separate version of is_valid method that checks both ParseError and UnsupportedSyntaxError which can then be used here. I'm not sure what to name the method though as both are syntax errors at the end 😅

}
}

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
Loading