diff --git a/crates/ruff/src/cache.rs b/crates/ruff/src/cache.rs index 6c126e8a97ed4..53087316ba8d1 100644 --- a/crates/ruff/src/cache.rs +++ b/crates/ruff/src/cache.rs @@ -180,12 +180,24 @@ impl Cache { .write_all(&serialized) .context("Failed to write serialized cache to temporary file.")?; - temp_file.persist(&self.path).with_context(|| { - format!( - "Failed to rename temporary cache file to {}", - self.path.display() - ) - })?; + if let Err(err) = temp_file.persist(&self.path) { + // On Windows, writing to the cache file can fail if the file is still open (e.g., if + // the user is running Ruff from multiple processes over the same directory). + if cfg!(windows) && err.error.kind() == io::ErrorKind::PermissionDenied { + warn_user!( + "Failed to write cache file '{}': {}", + self.path.display(), + err.error + ); + } else { + return Err(err).with_context(|| { + format!( + "Failed to rename temporary cache file to {}", + self.path.display() + ) + }); + } + } Ok(()) } diff --git a/crates/ruff/src/commands/format.rs b/crates/ruff/src/commands/format.rs index e4cb5bf00f27d..c38da8146f8e4 100644 --- a/crates/ruff/src/commands/format.rs +++ b/crates/ruff/src/commands/format.rs @@ -176,6 +176,7 @@ pub(crate) fn format( duration ); + // Store the caches. caches.persist()?; // Report on any errors.