From 41604412454afd4a3ec9d6aed766a76e6778cb0a Mon Sep 17 00:00:00 2001 From: Kan-Ru Chen Date: Fri, 19 Jan 2024 08:42:48 +0800 Subject: [PATCH] fix(cargo-rustdoc): use same path by output format logic everywhere --- src/cargo/ops/cargo_doc.rs | 36 ++++++++++++++++++++++-------------- tests/testsuite/rustdoc.rs | 1 + 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/cargo/ops/cargo_doc.rs b/src/cargo/ops/cargo_doc.rs index 8bdef00d347..518819b2d00 100644 --- a/src/cargo/ops/cargo_doc.rs +++ b/src/cargo/ops/cargo_doc.rs @@ -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, diff --git a/tests/testsuite/rustdoc.rs b/tests/testsuite/rustdoc.rs index 9b7e092d38f..2e4e48e2b87 100644 --- a/tests/testsuite/rustdoc.rs +++ b/tests/testsuite/rustdoc.rs @@ -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();