Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for Large File Corruption During Save #4276

Closed
24 changes: 22 additions & 2 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,28 @@ impl Document {
}
}

let mut file = File::create(path).await?;
to_writer(&mut file, encoding, &text).await?;
// TODO Temporary file creation is a workaround to solve large file corruption
// that occurs when crashing during a file save
let tmp_path = PathBuf::from(std::env::var("HOME").unwrap())
ngraham20 marked this conversation as resolved.
Show resolved Hide resolved
.join(".cache/")
.join("helix/")
.join(format!("~{}", path.file_name().unwrap().to_str().unwrap()));
ngraham20 marked this conversation as resolved.
Show resolved Hide resolved

if let Some(parent) = tmp_path.parent() {
// TODO: display a prompt asking the user if the directories should be created
if !parent.exists() {
if force {
ngraham20 marked this conversation as resolved.
Show resolved Hide resolved
std::fs::DirBuilder::new().recursive(true).create(parent)?;
} else {
bail!("can't save file, parent directory does not exist");
}
}
}

let mut tmp_file = File::create(&tmp_path).await?;
to_writer(&mut tmp_file, encoding, &text).await?;

tokio::fs::rename(&tmp_path, path).await?;

if let Some(language_server) = language_server {
if !language_server.is_initialized() {
Expand Down