Skip to content

Commit

Permalink
Print out autofix-broken or non-converging code when debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed May 3, 2023
1 parent b14358f commit 2b61f22
Showing 1 changed file with 59 additions and 37 deletions.
96 changes: 59 additions & 37 deletions crates/ruff/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,24 +453,7 @@ pub fn lint_fix<'a>(
// longer parseable on a subsequent pass, then we've introduced a
// syntax error. Return the original code.
if parseable && result.error.is_some() {
#[allow(clippy::print_stderr)]
{
eprintln!(
r#"
{}: Autofix introduced a syntax error. Reverting all changes.
This indicates a bug in `{}`. If you could open an issue at:
{}/issues/new?title=%5BAutofix%20error%5D
...quoting the contents of `{}`, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!
"#,
"error".red().bold(),
CARGO_PKG_NAME,
CARGO_PKG_REPOSITORY,
fs::relativize_path(path),
);
}
report_autofix_syntax_error(path, &transformed, &result.error.unwrap());
return Err(anyhow!("Autofix introduced a syntax error"));
}
}
Expand All @@ -493,25 +476,7 @@ This indicates a bug in `{}`. If you could open an issue at:
continue;
}

#[allow(clippy::print_stderr)]
{
eprintln!(
r#"
{}: Failed to converge after {} iterations.
This indicates a bug in `{}`. If you could open an issue at:
{}/issues/new?title=%5BInfinite%20loop%5D
...quoting the contents of `{}`, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!
"#,
"error".red().bold(),
MAX_ITERATIONS,
CARGO_PKG_NAME,
CARGO_PKG_REPOSITORY,
fs::relativize_path(path),
);
}
report_failed_to_converge_error(path, &transformed);
}

return Ok(FixerResult {
Expand All @@ -526,3 +491,60 @@ This indicates a bug in `{}`. If you could open an issue at:
});
}
}

#[allow(clippy::print_stderr)]
fn report_failed_to_converge_error(path: &Path, transformed: &str) {
if cfg!(debug_assertions) {
eprintln!(
"(Debug) Failed to converge after {} iterations in `{}`:---\n{}\n---",
MAX_ITERATIONS,
fs::relativize_path(path),
transformed,
);
return;
}
eprintln!(
r#"
{}: Failed to converge after {} iterations.
This indicates a bug in `{}`. If you could open an issue at:
{}/issues/new?title=%5BInfinite%20loop%5D
...quoting the contents of `{}`, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!
"#,
"error".red().bold(),
MAX_ITERATIONS,
CARGO_PKG_NAME,
CARGO_PKG_REPOSITORY,
fs::relativize_path(path),
);
}

#[allow(clippy::print_stderr)]
fn report_autofix_syntax_error(path: &Path, transformed: &str, error: &ParseError) {
if cfg!(debug_assertions) {
eprintln!(
"(Debug) Autofix introduced a syntax error in `{}`:\n\n{}\n\n---\n{}\n---",
fs::relativize_path(path),
error,
transformed,
);
return;
}
eprintln!(
r#"
{}: Autofix introduced a syntax error. Reverting all changes.
This indicates a bug in `{}`. If you could open an issue at:
{}/issues/new?title=%5BAutofix%20error%5D
...quoting the contents of `{}`, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!
"#,
"error".red().bold(),
CARGO_PKG_NAME,
CARGO_PKG_REPOSITORY,
fs::relativize_path(path),
);
}

0 comments on commit 2b61f22

Please sign in to comment.