Skip to content

Commit f898eff

Browse files
committed
Auto merge of #9473 - lf-:meow, r=ehuss
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.
2 parents 07340c9 + 6128b54 commit f898eff

File tree

12 files changed

+84
-16
lines changed

12 files changed

+84
-16
lines changed

src/cargo/ops/cargo_doc.rs

+28-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
use crate::core::compiler::RustcTargetData;
21
use crate::core::resolver::HasDevUnits;
32
use crate::core::{Shell, Workspace};
43
use crate::ops;
54
use crate::util::CargoResult;
6-
use std::collections::HashMap;
5+
use crate::{core::compiler::RustcTargetData, util::config::PathAndArgs};
6+
use serde::Deserialize;
77
use std::path::Path;
88
use std::process::Command;
9+
use std::{collections::HashMap, path::PathBuf};
910

1011
/// Strongly typed options for the `cargo doc` command.
1112
#[derive(Debug)]
@@ -16,6 +17,13 @@ pub struct DocOptions {
1617
pub compile_opts: ops::CompileOptions,
1718
}
1819

20+
#[derive(Deserialize)]
21+
struct CargoDocConfig {
22+
/// Browser to use to open docs. If this is unset, the value of the environment variable
23+
/// `BROWSER` will be used.
24+
browser: Option<PathAndArgs>,
25+
}
26+
1927
/// Main method for `cargo doc`.
2028
pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
2129
let specs = options.compile_opts.spec.to_package_id_specs(ws)?;
@@ -83,17 +91,30 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
8391
if path.exists() {
8492
let mut shell = ws.config().shell();
8593
shell.status("Opening", path.display())?;
86-
open_docs(&path, &mut shell)?;
94+
let cfg = ws.config().get::<CargoDocConfig>("doc")?;
95+
open_docs(
96+
&path,
97+
&mut shell,
98+
cfg.browser
99+
.map(|path_args| (path_args.path.resolve_program(ws.config()), path_args.args)),
100+
)?;
87101
}
88102
}
89103

90104
Ok(())
91105
}
92106

93-
fn open_docs(path: &Path, shell: &mut Shell) -> CargoResult<()> {
94-
match std::env::var_os("BROWSER") {
95-
Some(browser) => {
96-
if let Err(e) = Command::new(&browser).arg(path).status() {
107+
fn open_docs(
108+
path: &Path,
109+
shell: &mut Shell,
110+
config_browser: Option<(PathBuf, Vec<String>)>,
111+
) -> CargoResult<()> {
112+
let browser =
113+
config_browser.or_else(|| Some((PathBuf::from(std::env::var_os("BROWSER")?), Vec::new())));
114+
115+
match browser {
116+
Some((browser, initial_args)) => {
117+
if let Err(e) = Command::new(&browser).args(initial_args).arg(path).status() {
97118
shell.warn(format!(
98119
"Couldn't open docs with {}: {}",
99120
browser.to_string_lossy(),

src/doc/man/cargo-doc.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ is placed in `target/doc` in rustdoc's usual format.
2222

2323
{{#option "`--open`" }}
2424
Open the docs in a browser after building them. This will use your default
25-
browser unless you define another one in the `BROWSER` environment variable.
25+
browser unless you define another one in the `BROWSER` environment variable
26+
or use the [`doc.browser`](../reference/config.html#docbrowser) configuration
27+
option.
2628
{{/option}}
2729

2830
{{#option "`--no-deps`" }}

src/doc/man/cargo-rustdoc.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ or the `build.rustdocflags` [config value](../reference/config.html).
3434

3535
{{#option "`--open`" }}
3636
Open the docs in a browser after building them. This will use your default
37-
browser unless you define another one in the `BROWSER` environment variable.
37+
browser unless you define another one in the `BROWSER` environment variable
38+
or use the [`doc.browser`](../reference/config.html#docbrowser) configuration
39+
option.
3840
{{/option}}
3941

4042
{{/options}}

src/doc/man/generated_txt/cargo-doc.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ OPTIONS
1515
--open
1616
Open the docs in a browser after building them. This will use your
1717
default browser unless you define another one in the BROWSER
18-
environment variable.
18+
environment variable or use the doc.browser
19+
<https://doc.rust-lang.org/cargo/reference/config.html#docbrowser>
20+
configuration option.
1921

2022
--no-deps
2123
Do not build documentation for dependencies.

src/doc/man/generated_txt/cargo-rustdoc.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ OPTIONS
3434
--open
3535
Open the docs in a browser after building them. This will use your
3636
default browser unless you define another one in the BROWSER
37-
environment variable.
37+
environment variable or use the doc.browser
38+
<https://doc.rust-lang.org/cargo/reference/config.html#docbrowser>
39+
configuration option.
3840

3941
Package Selection
4042
By default, the package in the current working directory is selected.

src/doc/src/commands/cargo-doc.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ is placed in `target/doc` in rustdoc's usual format.
2222

2323
<dt class="option-term" id="option-cargo-doc---open"><a class="option-anchor" href="#option-cargo-doc---open"></a><code>--open</code></dt>
2424
<dd class="option-desc">Open the docs in a browser after building them. This will use your default
25-
browser unless you define another one in the <code>BROWSER</code> environment variable.</dd>
25+
browser unless you define another one in the <code>BROWSER</code> environment variable
26+
or use the <a href="../reference/config.html#docbrowser"><code>doc.browser</code></a> configuration
27+
option.</dd>
2628

2729

2830
<dt class="option-term" id="option-cargo-doc---no-deps"><a class="option-anchor" href="#option-cargo-doc---no-deps"></a><code>--no-deps</code></dt>

src/doc/src/commands/cargo-rustdoc.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ or the `build.rustdocflags` [config value](../reference/config.html).
3838

3939
<dt class="option-term" id="option-cargo-rustdoc---open"><a class="option-anchor" href="#option-cargo-rustdoc---open"></a><code>--open</code></dt>
4040
<dd class="option-desc">Open the docs in a browser after building them. This will use your default
41-
browser unless you define another one in the <code>BROWSER</code> environment variable.</dd>
41+
browser unless you define another one in the <code>BROWSER</code> environment variable
42+
or use the <a href="../reference/config.html#docbrowser"><code>doc.browser</code></a> configuration
43+
option.</dd>
4244

4345

4446
</dl>

src/doc/src/reference/config.md

+15
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+
[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+
#### `[doc]`
404+
405+
The `[doc]` table defines options for the [`cargo doc`] command.
406+
407+
##### `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

+2-1
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 [`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+
[`doc.browser`]: config.md#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

src/etc/man/cargo-doc.1

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ is placed in \fBtarget/doc\fR in rustdoc's usual format.
1616
\fB\-\-open\fR
1717
.RS 4
1818
Open the docs in a browser after building them. This will use your default
19-
browser unless you define another one in the \fBBROWSER\fR environment variable.
19+
browser unless you define another one in the \fBBROWSER\fR environment variable
20+
or use the \fI\f(BIdoc.browser\fI\fR <https://doc.rust\-lang.org/cargo/reference/config.html#docbrowser> configuration
21+
option.
2022
.RE
2123
.sp
2224
\fB\-\-no\-deps\fR

src/etc/man/cargo-rustdoc.1

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ or the \fBbuild.rustdocflags\fR \fIconfig value\fR <https://doc.rust\-lang.org/c
3232
\fB\-\-open\fR
3333
.RS 4
3434
Open the docs in a browser after building them. This will use your default
35-
browser unless you define another one in the \fBBROWSER\fR environment variable.
35+
browser unless you define another one in the \fBBROWSER\fR environment variable
36+
or use the \fI\f(BIdoc.browser\fI\fR <https://doc.rust\-lang.org/cargo/reference/config.html#docbrowser> configuration
37+
option.
3638
.RE
3739
.SS "Package Selection"
3840
By default, the package in the current working directory is selected. The \fB\-p\fR

tests/testsuite/doc.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,21 @@ fn doc_workspace_open_different_library_and_package_names() {
11841184
.env("BROWSER", "echo")
11851185
.with_stderr_contains("[..] Documenting foo v0.1.0 ([..])")
11861186
.with_stderr_contains("[..] [CWD]/target/doc/foolib/index.html")
1187+
.with_stdout_contains("[CWD]/target/doc/foolib/index.html")
1188+
.run();
1189+
1190+
p.change_file(
1191+
".cargo/config.toml",
1192+
r#"
1193+
[doc]
1194+
browser = ["echo", "a"]
1195+
"#,
1196+
);
1197+
1198+
// check that the cargo config overrides the browser env var
1199+
p.cargo("doc --open")
1200+
.env("BROWSER", "true")
1201+
.with_stdout_contains("a [CWD]/target/doc/foolib/index.html")
11871202
.run();
11881203
}
11891204

0 commit comments

Comments
 (0)