From aa317d9ce43a265158774746c79127ade2519bf4 Mon Sep 17 00:00:00 2001 From: kazk Date: Wed, 25 Aug 2021 23:38:08 -0700 Subject: [PATCH] Make sure the directory exists before writing a file --- src/main.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index dcae16e..8b5bae7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ use futures_util::{ SinkExt, StreamExt, }; use tokio::{ - fs::File, + fs::{self, File}, io::AsyncWriteExt, process::Command, time::{Duration, Instant}, @@ -137,10 +137,13 @@ async fn maybe_write_text_document(msg: &lsp::Message) -> Result<(), std::io::Er let uri = ¶ms.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?; + } } } }