Skip to content

Commit

Permalink
feat(doc): Print the generated docs links
Browse files Browse the repository at this point in the history
I've wanted something like this myself.  I dislike using `--open`
because I tend to move up to re-run my `cargo doc` run but then have to
edit it to remove `--open`.
Also makes it annoying when opening docs when `cargo doc` is wrapped by
a tool like `make`.

This was previously attempted in rust-lang#5592:
- Unlike the request in rust-lang#5562, this aligns with rust-lang#5592 in always printing
  rather than using a flag as this seems generally useful
- Unlike rust-lang#5592, this prints as an alternative to "Opening" to keep
  things light
- Unlike rust-lang#5592, this prints afterwards as the link is only valid then

Fixes rust-lang#5562
  • Loading branch information
epage committed Oct 19, 2023
1 parent b227fe1 commit 1dac61e
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 33 deletions.
1 change: 1 addition & 0 deletions crates/cargo-test-support/src/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ fn substitute_macros(input: &str) -> String {
("[WAITING]", " Waiting"),
("[PUBLISHED]", " Published"),
("[BLOCKING]", " Blocking"),
("[GENERATED]", " Generated"),
];
let mut result = input.to_owned();
for &(pat, subst) in &macros {
Expand Down
46 changes: 28 additions & 18 deletions src/cargo/ops/cargo_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,37 @@ pub struct DocOptions {
pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
let compilation = ops::compile(ws, &options.compile_opts)?;

if options.open_result {
let name = &compilation
.root_crate_names
.get(0)
.ok_or_else(|| anyhow::anyhow!("no crates with documentation"))?;
let kind = options.compile_opts.build_config.single_requested_kind()?;
let path = compilation.root_output[&kind]
.with_file_name("doc")
.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 opened = false;
for name in &compilation.root_crate_names {
for kind in &options.compile_opts.build_config.requested_kinds {
let path = compilation.root_output[&kind]
.with_file_name("doc")
.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())?;
open_docs(&path, &mut shell, config_browser, ws.config())?;
let mut shell = ws.config().shell();
if options.open_result {
shell.status("Opening", path.display())?;
open_docs(&path, &mut shell, config_browser, ws.config())?;
opened = true;
// User can navigate to the other crates
break;
} else {
shell.status("Generated", path.display())?;
}
}
}
}
if options.open_result && !opened {
anyhow::bail!("no crates with documentation");
}

Ok(())
}
Expand Down
2 changes: 2 additions & 0 deletions tests/testsuite/artifact_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2152,6 +2152,7 @@ fn doc_lib_true() {
[DOCUMENTING] bar v0.0.1 ([CWD]/bar)
[DOCUMENTING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[GENERATED] [CWD]/target/doc/foo/index.html
",
)
.run();
Expand Down Expand Up @@ -2227,6 +2228,7 @@ fn rustdoc_works_on_libs_with_artifacts_and_lib_false() {
[COMPILING] bar v0.5.0 ([CWD]/bar)
[DOCUMENTING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[GENERATED] [CWD]/target/doc/foo/index.html
",
)
.run();
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,7 @@ fn testing_and_such() {
[DOCUMENTING] foo v0.5.0 ([CWD])
[RUNNING] `rustdoc [..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[GENERATED] [CWD]/target/doc/foo/index.html
",
)
.run();
Expand Down
4 changes: 3 additions & 1 deletion tests/testsuite/check_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ fn build_script_doc() {
[RUNNING] `[..]/build-script-build`
[DOCUMENTING] foo [..]
[RUNNING] `rustdoc [..] src/main.rs [..]
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[GENERATED] [CWD]/target/doc/foo/index.html
",
)
.masquerade_as_nightly_cargo(&["check-cfg"])
.run();
Expand Down
6 changes: 6 additions & 0 deletions tests/testsuite/collisions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ fn collision_doc_multiple_versions() {
[DOCUMENTING] bar v2.0.0
[FINISHED] [..]
[DOCUMENTING] foo v0.1.0 [..]
[GENERATED] [CWD]/target/doc/foo/index.html
",
)
.run();
Expand Down Expand Up @@ -384,6 +385,7 @@ fn collision_doc_profile_split() {
[DOCUMENTING] pm v0.1.0 [..]
[DOCUMENTING] foo v0.1.0 [..]
[FINISHED] [..]
[GENERATED] [CWD]/target/doc/foo/index.html
",
)
.run();
Expand Down Expand Up @@ -430,6 +432,7 @@ the same path; see <https://github.com/rust-lang/cargo/issues/6313>.
[CHECKING] bar v1.0.0
[DOCUMENTING] foo v0.1.0 [..]
[FINISHED] [..]
[GENERATED] [CWD]/target/doc/foo/index.html
",
)
.run();
Expand Down Expand Up @@ -478,6 +481,7 @@ fn collision_doc_target() {
[CHECKING] bar v1.0.0
[DOCUMENTING] foo v0.1.0 [..]
[FINISHED] [..]
[GENERATED] [CWD]/target/[..]/doc/foo/index.html
",
)
.run();
Expand Down Expand Up @@ -545,6 +549,8 @@ the same path; see <https://github.com/rust-lang/cargo/issues/6313>.
[DOCUMENTING] foo-macro v1.0.0 [..]
[DOCUMENTING] abc v1.0.0 [..]
[FINISHED] [..]
[GENERATED] [CWD]/target/doc/abc/index.html
[GENERATED] [CWD]/target/doc/foo_macro/index.html
")
.run();
}
Loading

0 comments on commit 1dac61e

Please sign in to comment.