Skip to content

Commit 63f4af9

Browse files
committed
Auto merge of #13325 - kanru:issue13303-output-generated-json, r=weihanglo
fix(cargo-rustdoc): use same path by output format logic everywhere
2 parents c061bfb + 4160441 commit 63f4af9

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/cargo/ops/cargo_doc.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::core::compiler::{Compilation, CompileKind};
12
use crate::core::{Shell, Workspace};
23
use crate::ops;
34
use crate::util::config::{Config, PathAndArgs};
@@ -61,16 +62,7 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
6162
.ok_or_else(|| anyhow::anyhow!("no crates with documentation"))?;
6263
let kind = options.compile_opts.build_config.single_requested_kind()?;
6364

64-
let path = if matches!(options.output_format, OutputFormat::Json) {
65-
compilation.root_output[&kind]
66-
.with_file_name("doc")
67-
.join(format!("{}.json", &name))
68-
} else {
69-
compilation.root_output[&kind]
70-
.with_file_name("doc")
71-
.join(&name)
72-
.join("index.html")
73-
};
65+
let path = path_by_output_format(&compilation, &kind, &name, &options.output_format);
7466

7567
if path.exists() {
7668
let config_browser = {
@@ -88,10 +80,8 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
8880
} else {
8981
for name in &compilation.root_crate_names {
9082
for kind in &options.compile_opts.build_config.requested_kinds {
91-
let path = compilation.root_output[&kind]
92-
.with_file_name("doc")
93-
.join(&name)
94-
.join("index.html");
83+
let path =
84+
path_by_output_format(&compilation, &kind, &name, &options.output_format);
9585
if path.exists() {
9686
let mut shell = ws.config().shell();
9787
let link = shell.err_file_hyperlink(&path);
@@ -107,6 +97,24 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
10797
Ok(())
10898
}
10999

100+
fn path_by_output_format(
101+
compilation: &Compilation<'_>,
102+
kind: &CompileKind,
103+
name: &str,
104+
output_format: &OutputFormat,
105+
) -> PathBuf {
106+
if matches!(output_format, OutputFormat::Json) {
107+
compilation.root_output[kind]
108+
.with_file_name("doc")
109+
.join(format!("{}.json", name))
110+
} else {
111+
compilation.root_output[kind]
112+
.with_file_name("doc")
113+
.join(name)
114+
.join("index.html")
115+
}
116+
}
117+
110118
fn open_docs(
111119
path: &Path,
112120
shell: &mut Shell,

tests/testsuite/rustdoc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ fn rustdoc_simple_json() {
4848
[DOCUMENTING] foo v0.0.1 ([CWD])
4949
[RUNNING] `rustdoc [..]--crate-name foo [..]-o [CWD]/target/doc [..]--output-format=json[..]
5050
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
51+
[GENERATED] [CWD]/target/doc/foo.json
5152
",
5253
)
5354
.run();

0 commit comments

Comments
 (0)