From b56e8ad69610ca017ee338249ef5e637a161a097 Mon Sep 17 00:00:00 2001 From: konsti Date: Fri, 21 Jul 2023 11:32:12 +0200 Subject: [PATCH] Document formatter error shrinking (#5915) ## Summary **Don't minimize files that don't match in the first place** This adds a sanity check to the minimizer script that the input matches the condition (e.g. unstable formatting). Otherwise we run through all checks with the whole file, which is extremely slow. It's more reasonable for downstream usage to write an empty string to the output file instead. --- crates/ruff_shrinking/src/main.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/crates/ruff_shrinking/src/main.rs b/crates/ruff_shrinking/src/main.rs index 541fab33e1..0d8c8cd6af 100644 --- a/crates/ruff_shrinking/src/main.rs +++ b/crates/ruff_shrinking/src/main.rs @@ -422,9 +422,18 @@ fn run() -> Result<()> { let loop_start = Instant::now(); let mut stats = HashMap::new(); - let mut num_iterations = 0; - // normalize line endings for the remove newline dependent rules + // Normalize line endings for the remove newline dependent rules let mut input = fs::read_to_string(args.input_file)?.replace('\r', ""); + + // This can happen e.g. when main changed between collecting the errors list and running this + // script + if !is_failing(&input, &args.output_file, &command_args, &pattern)? { + println!("Input doesn't match"); + fs::write(&args.output_file, "")?; + return Ok(()); + } + + let mut num_iterations = 0; let mut last_strategy_and_idx = None; loop { let start = Instant::now(); @@ -461,9 +470,10 @@ fn run() -> Result<()> { println!("Strategies taken: {stats:?}"); println!( - "Done with {num_iterations} iterations in {:.2}s. Find your minimized example in {}", + "Done with {num_iterations} iterations in {:.2}s. Find your minimized example in {}:\n---\n{}\n---\n", loop_start.elapsed().as_secs_f32(), - args.output_file.display() + args.output_file.display(), + input ); Ok(())