diff --git a/crates/ruff_dev/src/format_dev.rs b/crates/ruff_dev/src/format_dev.rs index 566efb8290..9daf4554c5 100644 --- a/crates/ruff_dev/src/format_dev.rs +++ b/crates/ruff_dev/src/format_dev.rs @@ -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 { 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)) }