Skip to content

Commit

Permalink
Print files that are slow to format (#5681)
Browse files Browse the repository at this point in the history
Co-authored-by: konsti <konstin@mailbox.org>
  • Loading branch information
MichaReiser and konstin authored Jul 11, 2023
1 parent 8665a1a commit df15ad9
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions crates/ruff_dev/src/format_dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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 {
Expand All @@ -570,6 +584,8 @@ impl CheckFileError {
| CheckFileError::FormatError(_)
| CheckFileError::PrintError(_)
| CheckFileError::Panic { .. } => false,
#[cfg(not(debug_assertions))]
CheckFileError::Slow(_) => false,
}
}
}
Expand All @@ -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(_))) => {
Expand All @@ -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.
Expand Down Expand Up @@ -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))
}

0 comments on commit df15ad9

Please sign in to comment.