Skip to content

Commit

Permalink
Create parent directories for each source file.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ObsidianMinor committed Aug 6, 2024
1 parent f70d2d3 commit b213f36
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion recapnc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,12 @@ pub fn generate_from_request(request: &ReaderOf<CodeGeneratorRequest>, 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);
}
Expand Down

0 comments on commit b213f36

Please sign in to comment.