Skip to content

Commit

Permalink
Auto merge of #9531 - 5225225:cargo-doc-open-fix, r=ehuss
Browse files Browse the repository at this point in the history
Fix `BorrowMutError` when calling `cargo doc --open`

~~I'm not sure why the existing test suite didn't catch this, it definitely calls `cargo doc --open`.~~

I had

```toml
[doc.extern-map]
std = "local"
```

in my `.cargo/config.toml`. Will write a test case that sets that and then tries to run `cargo doc --open`.

Closes #9530
  • Loading branch information
bors committed Jul 1, 2021
2 parents 01d059a + 950c415 commit 4952979
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
21 changes: 6 additions & 15 deletions src/cargo/ops/cargo_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::core::{Shell, Workspace};
use crate::ops;
use crate::util::config::PathAndArgs;
use crate::util::CargoResult;
use serde::Deserialize;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
Expand All @@ -16,13 +15,6 @@ pub struct DocOptions {
pub compile_opts: ops::CompileOptions,
}

#[derive(Deserialize)]
struct CargoDocConfig {
/// Browser to use to open docs. If this is unset, the value of the environment variable
/// `BROWSER` will be used.
browser: Option<PathAndArgs>,
}

/// Main method for `cargo doc`.
pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
let compilation = ops::compile(ws, &options.compile_opts)?;
Expand All @@ -35,15 +27,14 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
.join(&name)
.join("index.html");
if path.exists() {
let config_browser = {
let cfg: Option<PathAndArgs> = ws.config().get("doc.browser")?;
cfg.map(|path_args| (path_args.path.resolve_program(ws.config()), path_args.args))
};

let mut shell = ws.config().shell();
shell.status("Opening", path.display())?;
let cfg = ws.config().get::<CargoDocConfig>("doc")?;
open_docs(
&path,
&mut shell,
cfg.browser
.map(|path_args| (path_args.path.resolve_program(ws.config()), path_args.args)),
)?;
open_docs(&path, &mut shell, config_browser)?;
}
}

Expand Down
35 changes: 35 additions & 0 deletions tests/testsuite/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,41 @@ fn doc_workspace_open_help_message() {
.run();
}

#[cargo_test]
#[cfg(not(windows))] // `echo` may not be available
fn doc_extern_map_local() {
if !is_nightly() {
// -Zextern-html-root-url is unstable
return;
}

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
"#,
)
.file("src/lib.rs", "")
.file(".cargo/config.toml", "doc.extern-map.std = 'local'")
.build();

p.cargo("doc -v --no-deps -Zrustdoc-map --open")
.env("BROWSER", "echo")
.masquerade_as_nightly_cargo()
.with_stderr(
"\
[DOCUMENTING] foo v0.1.0 [..]
[RUNNING] `rustdoc --crate-type lib --crate-name foo src/lib.rs [..]--crate-version 0.1.0`
[FINISHED] [..]
Opening [CWD]/target/doc/foo/index.html
",
)
.run();
}

#[cargo_test]
#[cfg(not(windows))] // `echo` may not be available
fn doc_workspace_open_different_library_and_package_names() {
Expand Down

0 comments on commit 4952979

Please sign in to comment.