Skip to content
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

fix(cargo-rustdoc): use same path by output format logic everywhere #13325

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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