From b213f3680476a554fbbbe2e625b1a2116335257d Mon Sep 17 00:00:00 2001 From: Aaron Loyd Date: Tue, 6 Aug 2024 17:11:21 -0500 Subject: [PATCH] Create parent directories for each source file. On some platforms, fs::write won't create parent direcotries and will fail if the parent directories don't exist. Now we make the parent directories before writing. This also adds context to write errors indicating what file didn't get written. --- recapnc/src/lib.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/recapnc/src/lib.rs b/recapnc/src/lib.rs index 7064bdf..7e88985 100644 --- a/recapnc/src/lib.rs +++ b/recapnc/src/lib.rs @@ -413,7 +413,12 @@ pub fn generate_from_request(request: &ReaderOf, out: impl .context("parsing generated file")?; let printable = prettyplease::unparse(&parsed); - fs::write(out.join(Path::new(&root.path)), printable)?; + let out_path = out.join(Path::new(&root.path)); + if let Some(parent) = out_path.parent() { + let _ = fs::create_dir_all(parent); + } + fs::write(&out_path, printable) + .with_context(|| format!("failed to write to path: {}", out_path.display()))?; root_mod.files.push(root); }