Skip to content

Commit

Permalink
Auto merge of #6803 - ehuss:doc-open-multi, r=alexcrichton
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Apr 1, 2019
2 parents 5ba4ef3 + 59af340 commit d338c49
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 26 deletions.
20 changes: 5 additions & 15 deletions src/cargo/ops/cargo_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions<'_>) -> CargoResult<()> {

let mut lib_names = HashMap::new();
let mut bin_names = HashMap::new();
let mut names = Vec::new();
for package in &pkgs {
for target in package.targets().iter().filter(|t| t.documented()) {
if target.is_lib() {
Expand All @@ -62,27 +63,16 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions<'_>) -> CargoResult<()> {
package
);
}
names.push(target.crate_name());
}
}

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

if options.open_result {
let name = if pkgs.len() > 1 {
failure::bail!(
"Passing multiple packages and `open` is not supported.\n\
Please re-run this command with `-p <spec>` where `<spec>` \
is one of the following:\n {}",
pkgs.iter()
.map(|p| p.name().as_str())
.collect::<Vec<_>>()
.join("\n ")
);
} else {
match lib_names.keys().chain(bin_names.keys()).nth(0) {
Some(s) => s.to_string(),
None => return Ok(()),
}
let name = match names.first() {
Some(s) => s.to_string(),
None => return Ok(()),
};

// Don't bother locking here as if this is getting deleted there's
Expand Down
14 changes: 3 additions & 11 deletions tests/testsuite/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,7 @@ fn doc_all_member_dependency_same_name() {
}

#[test]
#[cfg(not(any(target_os = "windows", target_os = "macos")))]
fn doc_workspace_open_help_message() {
let p = project()
.file(
Expand All @@ -1029,19 +1030,10 @@ fn doc_workspace_open_help_message() {

// The order in which bar is compiled or documented is not deterministic
p.cargo("doc --all --open")
.with_status(101)
.env("BROWSER", "echo")
.with_stderr_contains("[..] Documenting bar v0.1.0 ([..])")
.with_stderr_contains("[..] Documenting foo v0.1.0 ([..])")
.with_stderr_contains(
"error: Passing multiple packages and `open` \
is not supported.",
)
.with_stderr_contains(
"Please re-run this command with `-p <spec>` \
where `<spec>` is one of the following:",
)
.with_stderr_contains(" foo")
.with_stderr_contains(" bar")
.with_stderr_contains("[..] Opening [..]/foo/index.html")
.run();
}

Expand Down

0 comments on commit d338c49

Please sign in to comment.