Skip to content

Commit

Permalink
Auto merge of #13325 - kanru:issue13303-output-generated-json, r=weih…
Browse files Browse the repository at this point in the history
…anglo

fix(cargo-rustdoc): use same path by output format logic everywhere
  • Loading branch information
bors committed Jan 19, 2024
2 parents c061bfb + 4160441 commit 63f4af9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/cargo/ops/cargo_doc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::core::compiler::{Compilation, CompileKind};
use crate::core::{Shell, Workspace};
use crate::ops;
use crate::util::config::{Config, PathAndArgs};
Expand Down Expand Up @@ -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 = {
Expand All @@ -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);
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 63f4af9

Please sign in to comment.