mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 05:25:17 +00:00
Print files that are slow to format (#5681)
Co-authored-by: konsti <konstin@mailbox.org>
This commit is contained in:
parent
8665a1a19d
commit
df15ad9696
1 changed files with 25 additions and 0 deletions
|
@ -2,6 +2,7 @@ use anyhow::{bail, Context};
|
||||||
use clap::{CommandFactory, FromArgMatches};
|
use clap::{CommandFactory, FromArgMatches};
|
||||||
use ignore::DirEntry;
|
use ignore::DirEntry;
|
||||||
use indicatif::ProgressBar;
|
use indicatif::ProgressBar;
|
||||||
|
|
||||||
use rayon::iter::{IntoParallelIterator, ParallelIterator};
|
use rayon::iter::{IntoParallelIterator, ParallelIterator};
|
||||||
use ruff::resolver::python_files_in_path;
|
use ruff::resolver::python_files_in_path;
|
||||||
use ruff::settings::types::{FilePattern, FilePatternSet};
|
use ruff::settings::types::{FilePattern, FilePatternSet};
|
||||||
|
@ -531,6 +532,15 @@ Formatted twice:
|
||||||
CheckFileError::IoError(error) => {
|
CheckFileError::IoError(error) => {
|
||||||
writeln!(f, "Error reading {}: {}", file.display(), 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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -558,6 +568,10 @@ enum CheckFileError {
|
||||||
IoError(io::Error),
|
IoError(io::Error),
|
||||||
/// From `catch_unwind`
|
/// From `catch_unwind`
|
||||||
Panic { message: String },
|
Panic { message: String },
|
||||||
|
|
||||||
|
/// Formatting a file took too long
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
Slow(Duration),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CheckFileError {
|
impl CheckFileError {
|
||||||
|
@ -570,6 +584,8 @@ impl CheckFileError {
|
||||||
| CheckFileError::FormatError(_)
|
| CheckFileError::FormatError(_)
|
||||||
| CheckFileError::PrintError(_)
|
| CheckFileError::PrintError(_)
|
||||||
| CheckFileError::Panic { .. } => false,
|
| CheckFileError::Panic { .. } => false,
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
CheckFileError::Slow(_) => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -586,6 +602,8 @@ fn format_dev_file(
|
||||||
write: bool,
|
write: bool,
|
||||||
) -> Result<Statistics, CheckFileError> {
|
) -> Result<Statistics, CheckFileError> {
|
||||||
let content = fs::read_to_string(input_path)?;
|
let content = fs::read_to_string(input_path)?;
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
let start = Instant::now();
|
||||||
let printed = match format_module(&content, PyFormatOptions::default()) {
|
let printed = match format_module(&content, PyFormatOptions::default()) {
|
||||||
Ok(printed) => printed,
|
Ok(printed) => printed,
|
||||||
Err(err @ (FormatModuleError::LexError(_) | FormatModuleError::ParseError(_))) => {
|
Err(err @ (FormatModuleError::LexError(_) | FormatModuleError::ParseError(_))) => {
|
||||||
|
@ -599,6 +617,8 @@ fn format_dev_file(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let formatted = printed.as_code();
|
let formatted = printed.as_code();
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
let format_duration = Instant::now() - start;
|
||||||
|
|
||||||
if write && content != formatted {
|
if write && content != formatted {
|
||||||
// Simple atomic write.
|
// 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))
|
Ok(Statistics::from_versions(&content, formatted))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue