Skip to content

Commit e840c8e

Browse files
committed
Add a cargo-doc.browser config option
The idea of this option is to allow cargo to use a separate browser from the rest of the system. My motivation in doing this is that I want to write a script that adds a symbolic link in some web root on my system such that I can access my docs via the http protocol to avoid the limitations of the file protocol that are accessibility problems for me. For instance, zoom is not retained across multiple pages and Stylus filters don't work well.
1 parent e51522a commit e840c8e

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

src/cargo/ops/cargo_doc.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use crate::core::resolver::HasDevUnits;
33
use crate::core::{Shell, Workspace};
44
use crate::ops;
55
use crate::util::CargoResult;
6+
use serde::Deserialize;
67
use std::collections::HashMap;
8+
use std::ffi::OsString;
79
use std::path::Path;
810
use std::process::Command;
911

@@ -16,6 +18,13 @@ pub struct DocOptions {
1618
pub compile_opts: ops::CompileOptions,
1719
}
1820

21+
#[derive(Deserialize)]
22+
struct CargoDocConfig {
23+
/// Browser to use to open docs. If this is unset, the value of the environment variable
24+
/// `BROWSER` will be used.
25+
browser: Option<String>,
26+
}
27+
1928
/// Main method for `cargo doc`.
2029
pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
2130
let specs = options.compile_opts.spec.to_package_id_specs(ws)?;
@@ -83,15 +92,17 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
8392
if path.exists() {
8493
let mut shell = ws.config().shell();
8594
shell.status("Opening", path.display())?;
86-
open_docs(&path, &mut shell)?;
95+
let cfg = ws.config().get::<CargoDocConfig>("cargo-doc")?;
96+
open_docs(&path, &mut shell, cfg.browser.map(|v| v.into()))?;
8797
}
8898
}
8999

90100
Ok(())
91101
}
92102

93-
fn open_docs(path: &Path, shell: &mut Shell) -> CargoResult<()> {
94-
match std::env::var_os("BROWSER") {
103+
fn open_docs(path: &Path, shell: &mut Shell, config_browser: Option<OsString>) -> CargoResult<()> {
104+
let browser = config_browser.or_else(|| std::env::var_os("BROWSER"));
105+
match browser {
95106
Some(browser) => {
96107
if let Err(e) = Command::new(&browser).arg(path).status() {
97108
shell.warn(format!(

src/doc/src/reference/config.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ incremental = true # whether or not to enable incremental compilation
6767
dep-info-basedir = "" # path for the base directory for targets in depfiles
6868
pipelining = true # rustc pipelining
6969

70+
[cargo-doc]
71+
browser = "chromium" # browser to use with `cargo doc --open`,
72+
# overrides the `BROWSER` environment variable
73+
7074
[cargo-new]
7175
vcs = "none" # VCS to use ('git', 'hg', 'pijul', 'fossil', 'none')
7276

@@ -396,6 +400,16 @@ directory.
396400
Controls whether or not build pipelining is used. This allows Cargo to
397401
schedule overlapping invocations of `rustc` in parallel when possible.
398402

403+
#### `[cargo-doc]`
404+
405+
The `[cargo-doc]` table defines options for the [`cargo doc`] command.
406+
407+
##### `cargo-doc.browser`
408+
409+
This option sets the browser to be used by [`cargo doc`], overriding the
410+
`BROWSER` environment variable when opening documentation with the `--open`
411+
option.
412+
399413
#### `[cargo-new]`
400414

401415
The `[cargo-new]` table defines defaults for the [`cargo new`] command.
@@ -928,6 +942,7 @@ Sets the width for progress bar.
928942

929943
[`cargo bench`]: ../commands/cargo-bench.md
930944
[`cargo login`]: ../commands/cargo-login.md
945+
[`cargo doc`]: ../commands/cargo-doc.md
931946
[`cargo new`]: ../commands/cargo-new.md
932947
[`cargo publish`]: ../commands/cargo-publish.md
933948
[`cargo run`]: ../commands/cargo-run.md

src/doc/src/reference/environment-variables.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ system:
5252
detail.
5353
* `TERM` — If this is set to `dumb`, it disables the progress bar.
5454
* `BROWSER` — The web browser to execute to open documentation with [`cargo
55-
doc`]'s' `--open` flag.
55+
doc`]'s' `--open` flag, see [`cargo-doc.browser`] for more details.
5656
* `RUSTFMT` — Instead of running `rustfmt`,
5757
[`cargo fmt`](https://github.com/rust-lang/rustfmt) will execute this specified
5858
`rustfmt` instance instead.
@@ -134,6 +134,7 @@ supported environment variables are:
134134
[`build.incremental`]: config.md#buildincremental
135135
[`build.dep-info-basedir`]: config.md#builddep-info-basedir
136136
[`build.pipelining`]: config.md#buildpipelining
137+
[`cargo-doc.browser`]: config.md#cargo-docbrowser
137138
[`cargo-new.name`]: config.md#cargo-newname
138139
[`cargo-new.email`]: config.md#cargo-newemail
139140
[`cargo-new.vcs`]: config.md#cargo-newvcs

tests/testsuite/doc.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,20 @@ fn doc_workspace_open_different_library_and_package_names() {
11851185
.with_stderr_contains("[..] Documenting foo v0.1.0 ([..])")
11861186
.with_stderr_contains("[..] [CWD]/target/doc/foolib/index.html")
11871187
.run();
1188+
1189+
p.change_file(
1190+
".cargo/config.toml",
1191+
r#"
1192+
[cargo-doc]
1193+
browser = "echo"
1194+
"#,
1195+
);
1196+
1197+
// check that the cargo config overrides the browser env var
1198+
p.cargo("doc --open")
1199+
.env("BROWSER", "true")
1200+
.with_stderr_contains("[..] [CWD]/target/doc/foolib/index.html")
1201+
.run();
11881202
}
11891203

11901204
#[cargo_test]

0 commit comments

Comments
 (0)