Skip to content

Commit 464eea2

Browse files
committed
Print out autofix-broken or non-converging code when debugging
Refs astral-sh#4196 (comment)
1 parent b14358f commit 464eea2

File tree

1 file changed

+67
-40
lines changed

1 file changed

+67
-40
lines changed

crates/ruff/src/linter.rs

Lines changed: 67 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ use crate::rules::pycodestyle;
3131
use crate::settings::{flags, Settings};
3232
use crate::{directives, fs};
3333

34-
const CARGO_PKG_NAME: &str = env!("CARGO_PKG_NAME");
35-
const CARGO_PKG_REPOSITORY: &str = env!("CARGO_PKG_REPOSITORY");
36-
3734
/// A [`Result`]-like type that returns both data and an error. Used to return
3835
/// diagnostics even in the face of parse errors, since many diagnostics can be
3936
/// generated without a full AST.
@@ -453,24 +450,7 @@ pub fn lint_fix<'a>(
453450
// longer parseable on a subsequent pass, then we've introduced a
454451
// syntax error. Return the original code.
455452
if parseable && result.error.is_some() {
456-
#[allow(clippy::print_stderr)]
457-
{
458-
eprintln!(
459-
r#"
460-
{}: Autofix introduced a syntax error. Reverting all changes.
461-
462-
This indicates a bug in `{}`. If you could open an issue at:
463-
464-
{}/issues/new?title=%5BAutofix%20error%5D
465-
466-
...quoting the contents of `{}`, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!
467-
"#,
468-
"error".red().bold(),
469-
CARGO_PKG_NAME,
470-
CARGO_PKG_REPOSITORY,
471-
fs::relativize_path(path),
472-
);
473-
}
453+
report_autofix_syntax_error(path, &transformed, &result.error.unwrap());
474454
return Err(anyhow!("Autofix introduced a syntax error"));
475455
}
476456
}
@@ -493,25 +473,7 @@ This indicates a bug in `{}`. If you could open an issue at:
493473
continue;
494474
}
495475

496-
#[allow(clippy::print_stderr)]
497-
{
498-
eprintln!(
499-
r#"
500-
{}: Failed to converge after {} iterations.
501-
502-
This indicates a bug in `{}`. If you could open an issue at:
503-
504-
{}/issues/new?title=%5BInfinite%20loop%5D
505-
506-
...quoting the contents of `{}`, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!
507-
"#,
508-
"error".red().bold(),
509-
MAX_ITERATIONS,
510-
CARGO_PKG_NAME,
511-
CARGO_PKG_REPOSITORY,
512-
fs::relativize_path(path),
513-
);
514-
}
476+
report_failed_to_converge_error(path, &transformed);
515477
}
516478

517479
return Ok(FixerResult {
@@ -526,3 +488,68 @@ This indicates a bug in `{}`. If you could open an issue at:
526488
});
527489
}
528490
}
491+
492+
#[allow(clippy::print_stderr)]
493+
fn report_failed_to_converge_error(path: &Path, transformed: &str) {
494+
#[cfg(debug_assertions)]
495+
{
496+
eprintln!(
497+
"{}: Failed to converge after {} iterations in `{}`:---\n{}\n---",
498+
"debug error".red().bold(),
499+
MAX_ITERATIONS,
500+
fs::relativize_path(path),
501+
transformed,
502+
);
503+
}
504+
#[cfg(not(debug_assertions))]
505+
{
506+
eprintln!(
507+
r#"
508+
{}: Failed to converge after {} iterations.
509+
510+
This indicates a bug in `{}`. If you could open an issue at:
511+
512+
{}/issues/new?title=%5BInfinite%20loop%5D
513+
514+
...quoting the contents of `{}`, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!
515+
"#,
516+
"error".red().bold(),
517+
MAX_ITERATIONS,
518+
env!("CARGO_PKG_NAME"),
519+
env!("CARGO_PKG_REPOSITORY"),
520+
fs::relativize_path(path),
521+
);
522+
}
523+
}
524+
525+
#[allow(clippy::print_stderr)]
526+
fn report_autofix_syntax_error(path: &Path, transformed: &str, error: &ParseError) {
527+
#[cfg(debug_assertions)]
528+
{
529+
eprintln!(
530+
"{}: Autofix introduced a syntax error in `{}`:\n\n{}\n\n---\n{}\n---",
531+
"debug error".red().bold(),
532+
fs::relativize_path(path),
533+
error,
534+
transformed,
535+
);
536+
}
537+
#[cfg(not(debug_assertions))]
538+
{
539+
eprintln!(
540+
r#"
541+
{}: Autofix introduced a syntax error. Reverting all changes.
542+
543+
This indicates a bug in `{}`. If you could open an issue at:
544+
545+
{}/issues/new?title=%5BAutofix%20error%5D
546+
547+
...quoting the contents of `{}`, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!
548+
"#,
549+
"error".red().bold(),
550+
env!("CARGO_PKG_NAME"),
551+
env!("CARGO_PKG_REPOSITORY"),
552+
fs::relativize_path(path),
553+
);
554+
}
555+
}

0 commit comments

Comments
 (0)