Auto merge of #13325 - kanru:issue13303-output-generated-json, r=weihanglo

fix(cargo-rustdoc): use same path by output format logic everywhere
This commit is contained in:
bors 2024-01-19 01:54:10 +00:00
commit 63f4af9c51
2 changed files with 23 additions and 14 deletions

View file

@ -1,3 +1,4 @@
use crate::core::compiler::{Compilation, CompileKind};
use crate::core::{Shell, Workspace};
use crate::ops;
use crate::util::config::{Config, PathAndArgs};
@ -61,16 +62,7 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
.ok_or_else(|| anyhow::anyhow!("no crates with documentation"))?;
let kind = options.compile_opts.build_config.single_requested_kind()?;
let path = if matches!(options.output_format, OutputFormat::Json) {
compilation.root_output[&kind]
.with_file_name("doc")
.join(format!("{}.json", &name))
} else {
compilation.root_output[&kind]
.with_file_name("doc")
.join(&name)
.join("index.html")
};
let path = path_by_output_format(&compilation, &kind, &name, &options.output_format);
if path.exists() {
let config_browser = {
@ -88,10 +80,8 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
} else {
for name in &compilation.root_crate_names {
for kind in &options.compile_opts.build_config.requested_kinds {
let path = compilation.root_output[&kind]
.with_file_name("doc")
.join(&name)
.join("index.html");
let path =
path_by_output_format(&compilation, &kind, &name, &options.output_format);
if path.exists() {
let mut shell = ws.config().shell();
let link = shell.err_file_hyperlink(&path);
@ -107,6 +97,24 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
Ok(())
}
fn path_by_output_format(
compilation: &Compilation<'_>,
kind: &CompileKind,
name: &str,
output_format: &OutputFormat,
) -> PathBuf {
if matches!(output_format, OutputFormat::Json) {
compilation.root_output[kind]
.with_file_name("doc")
.join(format!("{}.json", name))
} else {
compilation.root_output[kind]
.with_file_name("doc")
.join(name)
.join("index.html")
}
}
fn open_docs(
path: &Path,
shell: &mut Shell,

View file

@ -48,6 +48,7 @@ fn rustdoc_simple_json() {
[DOCUMENTING] foo v0.0.1 ([CWD])
[RUNNING] `rustdoc [..]--crate-name foo [..]-o [CWD]/target/doc [..]--output-format=json[..]
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[GENERATED] [CWD]/target/doc/foo.json
",
)
.run();