Skip to content

Commit

Permalink
Make sure the directory exists before writing a file
Browse files Browse the repository at this point in the history
  • Loading branch information
kazk committed Aug 26, 2021
1 parent f16ba8d commit aa317d9
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use futures_util::{
SinkExt, StreamExt,
};
use tokio::{
fs::File,
fs::{self, File},
io::AsyncWriteExt,
process::Command,
time::{Duration, Instant},
Expand Down Expand Up @@ -137,10 +137,13 @@ async fn maybe_write_text_document(msg: &lsp::Message) -> Result<(), std::io::Er
let uri = &params.text_document.uri;
if uri.scheme() == "file" {
if let Ok(path) = uri.to_file_path() {
tracing::debug!("writing to {:?}", path);
let mut file = File::create(&path).await?;
file.write_all(text.as_bytes()).await?;
file.flush().await?;
if let Some(parent) = path.parent() {
tracing::debug!("writing to {:?}", path);
fs::create_dir_all(parent).await?;
let mut file = File::create(&path).await?;
file.write_all(text.as_bytes()).await?;
file.flush().await?;
}
}
}
}
Expand Down

0 comments on commit aa317d9

Please sign in to comment.