Print files that are slow to format (#5681)

Co-authored-by: konsti <konstin@mailbox.org>
This commit is contained in:
Micha Reiser 2023-07-11 15:03:18 +02:00 committed by GitHub
parent 8665a1a19d
commit df15ad9696
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,6 +2,7 @@ use anyhow::{bail, Context};
use clap::{CommandFactory, FromArgMatches};
use ignore::DirEntry;
use indicatif::ProgressBar;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use ruff::resolver::python_files_in_path;
use ruff::settings::types::{FilePattern, FilePatternSet};
@ -531,6 +532,15 @@ Formatted twice:
CheckFileError::IoError(error) => {
writeln!(f, "Error reading {}: {}", file.display(), error)?;
}
#[cfg(not(debug_assertions))]
CheckFileError::Slow(duration) => {
writeln!(
f,
"Slow formatting {}: Formatting the file took {}ms",
file.display(),
duration.as_millis()
)?;
}
}
Ok(())
}
@ -558,6 +568,10 @@ enum CheckFileError {
IoError(io::Error),
/// From `catch_unwind`
Panic { message: String },
/// Formatting a file took too long
#[cfg(not(debug_assertions))]
Slow(Duration),
}
impl CheckFileError {
@ -570,6 +584,8 @@ impl CheckFileError {
| CheckFileError::FormatError(_)
| CheckFileError::PrintError(_)
| CheckFileError::Panic { .. } => false,
#[cfg(not(debug_assertions))]
CheckFileError::Slow(_) => false,
}
}
}
@ -586,6 +602,8 @@ fn format_dev_file(
write: bool,
) -> Result<Statistics, CheckFileError> {
let content = fs::read_to_string(input_path)?;
#[cfg(not(debug_assertions))]
let start = Instant::now();
let printed = match format_module(&content, PyFormatOptions::default()) {
Ok(printed) => printed,
Err(err @ (FormatModuleError::LexError(_) | FormatModuleError::ParseError(_))) => {
@ -599,6 +617,8 @@ fn format_dev_file(
}
};
let formatted = printed.as_code();
#[cfg(not(debug_assertions))]
let format_duration = Instant::now() - start;
if write && content != formatted {
// Simple atomic write.
@ -635,5 +655,10 @@ fn format_dev_file(
}
}
#[cfg(not(debug_assertions))]
if format_duration > Duration::from_millis(50) {
return Err(CheckFileError::Slow(format_duration));
}
Ok(Statistics::from_versions(&content, formatted))
}