-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add possibility to generate rustdoc JSON output to stdout #128963
Changes from all commits
18d6200
e2fd0c0
7882575
63ab7b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ mod import_finder; | |
|
||
use std::cell::RefCell; | ||
use std::fs::{create_dir_all, File}; | ||
use std::io::{BufWriter, Write}; | ||
use std::io::{stdout, BufWriter, Write}; | ||
use std::path::PathBuf; | ||
use std::rc::Rc; | ||
|
||
|
@@ -37,7 +37,7 @@ pub(crate) struct JsonRenderer<'tcx> { | |
/// level field of the JSON blob. | ||
index: Rc<RefCell<FxHashMap<types::Id, types::Item>>>, | ||
/// The directory where the blob will be written to. | ||
out_path: PathBuf, | ||
out_path: Option<PathBuf>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be renamed to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sadly, as we discovered, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remark (peanut gallery): that is very surprising,
(rustc's cli docs is kinda inaccurate here because it's actually output path for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
cache: Rc<Cache>, | ||
imported_items: DefIdSet, | ||
} | ||
|
@@ -97,6 +97,20 @@ impl<'tcx> JsonRenderer<'tcx> { | |
}) | ||
.unwrap_or_default() | ||
} | ||
|
||
fn write<T: Write>( | ||
&self, | ||
output: types::Crate, | ||
mut writer: BufWriter<T>, | ||
path: &str, | ||
) -> Result<(), Error> { | ||
self.tcx | ||
.sess | ||
.time("rustdoc_json_serialization", || serde_json::ser::to_writer(&mut writer, &output)) | ||
.unwrap(); | ||
try_err!(writer.flush(), path); | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { | ||
|
@@ -120,7 +134,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { | |
JsonRenderer { | ||
tcx, | ||
index: Rc::new(RefCell::new(FxHashMap::default())), | ||
out_path: options.output, | ||
out_path: if options.output_to_stdout { None } else { Some(options.output) }, | ||
cache: Rc::new(cache), | ||
imported_items, | ||
}, | ||
|
@@ -264,20 +278,21 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { | |
.collect(), | ||
format_version: types::FORMAT_VERSION, | ||
}; | ||
let out_dir = self.out_path.clone(); | ||
try_err!(create_dir_all(&out_dir), out_dir); | ||
if let Some(ref out_path) = self.out_path { | ||
let out_dir = out_path.clone(); | ||
try_err!(create_dir_all(&out_dir), out_dir); | ||
|
||
let mut p = out_dir; | ||
p.push(output.index.get(&output.root).unwrap().name.clone().unwrap()); | ||
p.set_extension("json"); | ||
let mut file = BufWriter::new(try_err!(File::create(&p), p)); | ||
self.tcx | ||
.sess | ||
.time("rustdoc_json_serialization", || serde_json::ser::to_writer(&mut file, &output)) | ||
.unwrap(); | ||
try_err!(file.flush(), p); | ||
|
||
Ok(()) | ||
let mut p = out_dir; | ||
p.push(output.index.get(&output.root).unwrap().name.clone().unwrap()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also respect the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, from what I can see, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remark (peanut gallery): I would've thought There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it's the whole problem here. The |
||
p.set_extension("json"); | ||
self.write( | ||
output, | ||
BufWriter::new(try_err!(File::create(&p), p)), | ||
&p.display().to_string(), | ||
) | ||
} else { | ||
self.write(output, BufWriter::new(stdout()), "<stdout>") | ||
} | ||
} | ||
|
||
fn cache(&self) -> &Cache { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub struct Foo; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// This test verifies that rustdoc `-o -` prints JSON on stdout and doesn't generate | ||
// a JSON file. | ||
|
||
use std::path::PathBuf; | ||
|
||
use run_make_support::path_helpers::{cwd, has_extension, read_dir_entries_recursive}; | ||
use run_make_support::rustdoc; | ||
|
||
fn main() { | ||
// First we check that we generate the JSON in the stdout. | ||
rustdoc() | ||
.input("foo.rs") | ||
.output("-") | ||
.arg("-Zunstable-options") | ||
.output_format("json") | ||
.run() | ||
.assert_stdout_contains("{\""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remark: I suppose this is a reasonable heuristic. |
||
|
||
// Then we check it didn't generate any JSON file. | ||
read_dir_entries_recursive(cwd(), |path| { | ||
if path.is_file() && has_extension(path, "json") { | ||
panic!("Found a JSON file {path:?}"); | ||
} | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we discovered they're the same option, it simplifies a lot the handling of this new option.