Print out autofix-broken or non-converging code when debugging (#4201)

This commit is contained in:
Aarni Koskela 2023-05-03 14:50:03 +03:00 committed by GitHub
parent ccfc78e2d5
commit d0e3ca29d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -453,24 +453,7 @@ pub fn lint_fix<'a>(
// longer parseable on a subsequent pass, then we've introduced a // longer parseable on a subsequent pass, then we've introduced a
// syntax error. Return the original code. // syntax error. Return the original code.
if parseable && result.error.is_some() { if parseable && result.error.is_some() {
#[allow(clippy::print_stderr)] report_autofix_syntax_error(path, &transformed, &result.error.unwrap());
{
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),
);
}
return Err(anyhow!("Autofix introduced a syntax error")); return Err(anyhow!("Autofix introduced a syntax error"));
} }
} }
@ -493,8 +476,33 @@ This indicates a bug in `{}`. If you could open an issue at:
continue; continue;
} }
#[allow(clippy::print_stderr)] report_failed_to_converge_error(path, &transformed);
{ }
return Ok(FixerResult {
result: result.map(|(diagnostics, imports)| {
(
diagnostics_to_messages(diagnostics, path, &locator, &directives),
imports,
)
}),
transformed,
fixed,
});
}
}
#[allow(clippy::print_stderr)]
fn report_failed_to_converge_error(path: &Path, transformed: &str) {
if cfg!(debug_assertions) {
eprintln!(
"{}: Failed to converge after {} iterations in `{}`:---\n{}\n---",
"debug error".red().bold(),
MAX_ITERATIONS,
fs::relativize_path(path),
transformed,
);
} else {
eprintln!( eprintln!(
r#" r#"
{}: Failed to converge after {} iterations. {}: Failed to converge after {} iterations.
@ -512,17 +520,33 @@ This indicates a bug in `{}`. If you could open an issue at:
fs::relativize_path(path), fs::relativize_path(path),
); );
} }
} }
return Ok(FixerResult { #[allow(clippy::print_stderr)]
result: result.map(|(diagnostics, imports)| { fn report_autofix_syntax_error(path: &Path, transformed: &str, error: &ParseError) {
( if cfg!(debug_assertions) {
diagnostics_to_messages(diagnostics, path, &locator, &directives), eprintln!(
imports, "{}: Autofix introduced a syntax error in `{}`: {}\n---\n{}\n---",
) "debug error".red().bold(),
}), fs::relativize_path(path),
error,
transformed, transformed,
fixed, );
}); } else {
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),
);
} }
} }