Skip to content

Commit d338c49

Browse files
committed
Auto merge of #6803 - ehuss:doc-open-multi, r=alexcrichton
Allow `cargo doc --open` with multiple packages. If `cargo doc --open` builds multiple packages, open the first one. This seems pretty natural to me (the first one on the command line, or the first default member if `default-members` is specified, or the root of a workspace). Rustdoc shows a list of crates in the sidebar, so if it doesn't open the one the user wants, it's a trivial matter of clicking on the crate name. @alexcrichton specifically asked for an error [here](#1828 (diff)). However, at the time I don't think rustdoc dynamically generated the "Crates" listing in the sidebar. Alex, I wonder if your stance still holds? Closes #5145
2 parents 5ba4ef3 + 59af340 commit d338c49

File tree

2 files changed

+8
-26
lines changed

2 files changed

+8
-26
lines changed

src/cargo/ops/cargo_doc.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions<'_>) -> CargoResult<()> {
3939

4040
let mut lib_names = HashMap::new();
4141
let mut bin_names = HashMap::new();
42+
let mut names = Vec::new();
4243
for package in &pkgs {
4344
for target in package.targets().iter().filter(|t| t.documented()) {
4445
if target.is_lib() {
@@ -62,27 +63,16 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions<'_>) -> CargoResult<()> {
6263
package
6364
);
6465
}
66+
names.push(target.crate_name());
6567
}
6668
}
6769

6870
ops::compile(ws, &options.compile_opts)?;
6971

7072
if options.open_result {
71-
let name = if pkgs.len() > 1 {
72-
failure::bail!(
73-
"Passing multiple packages and `open` is not supported.\n\
74-
Please re-run this command with `-p <spec>` where `<spec>` \
75-
is one of the following:\n {}",
76-
pkgs.iter()
77-
.map(|p| p.name().as_str())
78-
.collect::<Vec<_>>()
79-
.join("\n ")
80-
);
81-
} else {
82-
match lib_names.keys().chain(bin_names.keys()).nth(0) {
83-
Some(s) => s.to_string(),
84-
None => return Ok(()),
85-
}
73+
let name = match names.first() {
74+
Some(s) => s.to_string(),
75+
None => return Ok(()),
8676
};
8777

8878
// Don't bother locking here as if this is getting deleted there's

tests/testsuite/doc.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,7 @@ fn doc_all_member_dependency_same_name() {
10121012
}
10131013

10141014
#[test]
1015+
#[cfg(not(any(target_os = "windows", target_os = "macos")))]
10151016
fn doc_workspace_open_help_message() {
10161017
let p = project()
10171018
.file(
@@ -1029,19 +1030,10 @@ fn doc_workspace_open_help_message() {
10291030

10301031
// The order in which bar is compiled or documented is not deterministic
10311032
p.cargo("doc --all --open")
1032-
.with_status(101)
1033+
.env("BROWSER", "echo")
10331034
.with_stderr_contains("[..] Documenting bar v0.1.0 ([..])")
10341035
.with_stderr_contains("[..] Documenting foo v0.1.0 ([..])")
1035-
.with_stderr_contains(
1036-
"error: Passing multiple packages and `open` \
1037-
is not supported.",
1038-
)
1039-
.with_stderr_contains(
1040-
"Please re-run this command with `-p <spec>` \
1041-
where `<spec>` is one of the following:",
1042-
)
1043-
.with_stderr_contains(" foo")
1044-
.with_stderr_contains(" bar")
1036+
.with_stderr_contains("[..] Opening [..]/foo/index.html")
10451037
.run();
10461038
}
10471039

0 commit comments

Comments
 (0)