Skip to content

Commit e58713e

Browse files
Make cache-write failures non-fatal (#12302)
## Summary Closes #12284.
1 parent aa5c53b commit e58713e

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

crates/ruff/src/cache.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,24 @@ impl Cache {
180180
.write_all(&serialized)
181181
.context("Failed to write serialized cache to temporary file.")?;
182182

183-
temp_file.persist(&self.path).with_context(|| {
184-
format!(
185-
"Failed to rename temporary cache file to {}",
186-
self.path.display()
187-
)
188-
})?;
183+
if let Err(err) = temp_file.persist(&self.path) {
184+
// On Windows, writing to the cache file can fail if the file is still open (e.g., if
185+
// the user is running Ruff from multiple processes over the same directory).
186+
if cfg!(windows) && err.error.kind() == io::ErrorKind::PermissionDenied {
187+
warn_user!(
188+
"Failed to write cache file '{}': {}",
189+
self.path.display(),
190+
err.error
191+
);
192+
} else {
193+
return Err(err).with_context(|| {
194+
format!(
195+
"Failed to rename temporary cache file to {}",
196+
self.path.display()
197+
)
198+
});
199+
}
200+
}
189201

190202
Ok(())
191203
}

crates/ruff/src/commands/format.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ pub(crate) fn format(
176176
duration
177177
);
178178

179+
// Store the caches.
179180
caches.persist()?;
180181

181182
// Report on any errors.

0 commit comments

Comments
 (0)