From f4a06fc8d2eef5d66f9f30c7fd4855a350602ef6 Mon Sep 17 00:00:00 2001 From: Christopher Harrison Date: Mon, 23 Dec 2024 11:11:47 +0000 Subject: [PATCH] Drop the input before writing to the output --- topiary-cli/src/main.rs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/topiary-cli/src/main.rs b/topiary-cli/src/main.rs index aef62e71..40ee5970 100644 --- a/topiary-cli/src/main.rs +++ b/topiary-cli/src/main.rs @@ -66,18 +66,26 @@ async fn run() -> CLIResult<()> { output ); - let mut buf_input = BufReader::new(input); let mut buf_output = BufWriter::new(output); - formatter( - &mut buf_input, - &mut buf_output, - &language, - Operation::Format { - skip_idempotence, - tolerate_parsing_errors, - }, - )?; + { + // NOTE This newly opened scope is important! `buf_input` takes + // ownership of `input`, which -- upon reading -- contains an + // open file handle. We need to close this file, by dropping + // `buf_input`, before we attempt to persist our output. + // Otherwise, we get an exclusive lock problem on Windows. + let mut buf_input = BufReader::new(input); + + formatter( + &mut buf_input, + &mut buf_output, + &language, + Operation::Format { + skip_idempotence, + tolerate_parsing_errors, + }, + )?; + } buf_output.into_inner()?.persist()?;