Skip to content

Commit 4952979

Browse files
committed
Auto merge of #9531 - 5225225:cargo-doc-open-fix, r=ehuss
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
2 parents 01d059a + 950c415 commit 4952979

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

src/cargo/ops/cargo_doc.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::core::{Shell, Workspace};
22
use crate::ops;
33
use crate::util::config::PathAndArgs;
44
use crate::util::CargoResult;
5-
use serde::Deserialize;
65
use std::path::Path;
76
use std::path::PathBuf;
87
use std::process::Command;
@@ -16,13 +15,6 @@ pub struct DocOptions {
1615
pub compile_opts: ops::CompileOptions,
1716
}
1817

19-
#[derive(Deserialize)]
20-
struct CargoDocConfig {
21-
/// Browser to use to open docs. If this is unset, the value of the environment variable
22-
/// `BROWSER` will be used.
23-
browser: Option<PathAndArgs>,
24-
}
25-
2618
/// Main method for `cargo doc`.
2719
pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
2820
let compilation = ops::compile(ws, &options.compile_opts)?;
@@ -35,15 +27,14 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
3527
.join(&name)
3628
.join("index.html");
3729
if path.exists() {
30+
let config_browser = {
31+
let cfg: Option<PathAndArgs> = ws.config().get("doc.browser")?;
32+
cfg.map(|path_args| (path_args.path.resolve_program(ws.config()), path_args.args))
33+
};
34+
3835
let mut shell = ws.config().shell();
3936
shell.status("Opening", path.display())?;
40-
let cfg = ws.config().get::<CargoDocConfig>("doc")?;
41-
open_docs(
42-
&path,
43-
&mut shell,
44-
cfg.browser
45-
.map(|path_args| (path_args.path.resolve_program(ws.config()), path_args.args)),
46-
)?;
37+
open_docs(&path, &mut shell, config_browser)?;
4738
}
4839
}
4940

tests/testsuite/doc.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,41 @@ fn doc_workspace_open_help_message() {
11731173
.run();
11741174
}
11751175

1176+
#[cargo_test]
1177+
#[cfg(not(windows))] // `echo` may not be available
1178+
fn doc_extern_map_local() {
1179+
if !is_nightly() {
1180+
// -Zextern-html-root-url is unstable
1181+
return;
1182+
}
1183+
1184+
let p = project()
1185+
.file(
1186+
"Cargo.toml",
1187+
r#"
1188+
[package]
1189+
name = "foo"
1190+
version = "0.1.0"
1191+
"#,
1192+
)
1193+
.file("src/lib.rs", "")
1194+
.file(".cargo/config.toml", "doc.extern-map.std = 'local'")
1195+
.build();
1196+
1197+
p.cargo("doc -v --no-deps -Zrustdoc-map --open")
1198+
.env("BROWSER", "echo")
1199+
.masquerade_as_nightly_cargo()
1200+
.with_stderr(
1201+
"\
1202+
[DOCUMENTING] foo v0.1.0 [..]
1203+
[RUNNING] `rustdoc --crate-type lib --crate-name foo src/lib.rs [..]--crate-version 0.1.0`
1204+
[FINISHED] [..]
1205+
Opening [CWD]/target/doc/foo/index.html
1206+
",
1207+
)
1208+
.run();
1209+
}
1210+
11761211
#[cargo_test]
11771212
#[cfg(not(windows))] // `echo` may not be available
11781213
fn doc_workspace_open_different_library_and_package_names() {

0 commit comments

Comments
 (0)