Skip to content

Commit 51fd48f

Browse files
authored
Rollup merge of #93099 - tomkris:rustdoc-fix, r=jsha
rustdoc: auto create output directory when "--output-format json" This PR allows rustdoc to automatically create output directory in case it does not exist (when run with `--output-format json`). This fixes rustdoc crash: ```` $ rustdoc --output-format json -Z unstable-options src/main.rs error: couldn't generate documentation: No such file or directory (os error 2) | = note: failed to create or modify "doc/main.json" error: aborting due to previous error ```` With this fix behavior of `rustdoc --output-format json` becomes consistent with `rustdoc --output-format html` (which already auto-creates output directory if it's missing)
2 parents e901b24 + 4e17170 commit 51fd48f

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/librustdoc/json/mod.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
mod conversions;
88

99
use std::cell::RefCell;
10-
use std::fs::File;
10+
use std::fs::{create_dir_all, File};
1111
use std::path::PathBuf;
1212
use std::rc::Rc;
1313

@@ -18,13 +18,14 @@ use rustc_session::Session;
1818

1919
use rustdoc_json_types as types;
2020

21-
use crate::clean;
2221
use crate::clean::types::{ExternalCrate, ExternalLocation};
2322
use crate::config::RenderOptions;
23+
use crate::docfs::PathError;
2424
use crate::error::Error;
2525
use crate::formats::cache::Cache;
2626
use crate::formats::FormatRenderer;
2727
use crate::json::conversions::{from_item_id, IntoWithTcx};
28+
use crate::{clean, try_err};
2829

2930
#[derive(Clone)]
3031
crate struct JsonRenderer<'tcx> {
@@ -269,10 +270,13 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
269270
.collect(),
270271
format_version: types::FORMAT_VERSION,
271272
};
272-
let mut p = self.out_path.clone();
273+
let out_dir = self.out_path.clone();
274+
try_err!(create_dir_all(&out_dir), out_dir);
275+
276+
let mut p = out_dir;
273277
p.push(output.index.get(&output.root).unwrap().name.clone().unwrap());
274278
p.set_extension("json");
275-
let file = File::create(&p).map_err(|error| Error { error: error.to_string(), file: p })?;
279+
let file = try_err!(File::create(&p), p);
276280
serde_json::ser::to_writer(&file, &output).unwrap();
277281
Ok(())
278282
}

0 commit comments

Comments
 (0)