Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

fs: Fall back from atomic write to regular fs write when file handle is in use on Windows (#30222)

Smit Barmasefcf066aff5eb
diff --git a/crates/fs/src/fs.rs b/crates/fs/src/fs.rs
index 93cd1eb5939..d1c59a2b58c 100644
--- a/crates/fs/src/fs.rs
+++ b/crates/fs/src/fs.rs
@@ -546,7 +546,24 @@ impl Fs for RealFs {
NamedTempFile::new()
}?;
tmp_file.write_all(data.as_bytes())?;
- tmp_file.persist(path)?;
+
+ let result = tmp_file.persist(&path);
+ if cfg!(target_os = "windows") {
+ // If file handle is already in used we receive error:
+ //
+ // failed to persist temporary file:
+ // Access is denied. (os error 5)
+ //
+ // So we use direct fs write instead to avoid it.
+ // https://github.com/zed-industries/zed/issues/30054
+ if let Err(persist_err) = &result {
+ if persist_err.error.raw_os_error() == Some(5) {
+ return std::fs::write(&path, data.as_bytes()).map_err(Into::into);
+ }
+ }
+ }
+ result?;
+
Ok::<(), anyhow::Error>(())
})
.await?;