Skip to content

Commit 1ea41ba

Browse files
committed
Add an option to include crate versions to the generated docs
Fixes #1681
1 parent d6fa260 commit 1ea41ba

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

src/bin/cargo/cli.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Available unstable (nightly-only) flags:
3838
-Z unstable-options -- Allow the usage of unstable options
3939
-Z timings -- Display concurrency information
4040
-Z doctest-xcompile -- Compile and run doctests for non-host target using runner config
41+
-Z crate-versions -- Add crate versions to generated docs
4142
4243
Run with 'cargo -Z [FLAG] [SUBCOMMAND]'"
4344
);

src/cargo/core/compiler/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult
558558
let mut rustdoc = cx.compilation.rustdoc_process(unit.pkg, unit.target)?;
559559
rustdoc.inherit_jobserver(&cx.jobserver);
560560
rustdoc.arg("--crate-name").arg(&unit.target.crate_name());
561+
add_crate_versions_if_requested(bcx, unit, &mut rustdoc);
561562
add_path_args(bcx, unit, &mut rustdoc);
562563
add_cap_lints(bcx, unit, &mut rustdoc);
563564

@@ -624,6 +625,21 @@ fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult
624625
}))
625626
}
626627

628+
fn add_crate_versions_if_requested(
629+
bcx: &BuildContext<'_, '_>,
630+
unit: &Unit<'_>,
631+
rustdoc: &mut ProcessBuilder,
632+
) {
633+
if !bcx.config.cli_unstable().crate_versions {
634+
return;
635+
}
636+
rustdoc
637+
.arg("-Z")
638+
.arg("unstable-options")
639+
.arg("--crate-version")
640+
.arg(&unit.pkg.version().to_string());
641+
}
642+
627643
// The path that we pass to rustc is actually fairly important because it will
628644
// show up in error messages (important for readability), debug information
629645
// (important for caching), etc. As a result we need to be pretty careful how we

src/cargo/core/features.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ pub struct CliUnstable {
342342
pub panic_abort_tests: bool,
343343
pub jobserver_per_rustc: bool,
344344
pub features: Option<Vec<String>>,
345+
pub crate_versions: bool,
345346
}
346347

347348
impl CliUnstable {
@@ -418,6 +419,7 @@ impl CliUnstable {
418419
"panic-abort-tests" => self.panic_abort_tests = parse_empty(k, v)?,
419420
"jobserver-per-rustc" => self.jobserver_per_rustc = parse_empty(k, v)?,
420421
"features" => self.features = Some(parse_features(v)),
422+
"crate-versions" => self.crate_versions = parse_empty(k, v)?,
421423
_ => bail!("unknown `-Z` flag specified: {}", k),
422424
}
423425

tests/testsuite/doc.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,3 +1510,36 @@ fn bin_private_items_deps() {
15101510
assert!(p.root().join("target/doc/bar/fn.bar_pub.html").is_file());
15111511
assert!(!p.root().join("target/doc/bar/fn.bar_priv.html").exists());
15121512
}
1513+
1514+
#[cargo_test]
1515+
fn crate_versions() {
1516+
// Testing unstable flag
1517+
if !is_nightly() {
1518+
return;
1519+
}
1520+
let p = project()
1521+
.file(
1522+
"Cargo.toml",
1523+
r#"
1524+
[package]
1525+
name = "foo"
1526+
version = "1.2.4"
1527+
authors = []
1528+
"#,
1529+
)
1530+
.file("src/lib.rs", "")
1531+
.build();
1532+
1533+
p.cargo("-Z crate-versions doc")
1534+
.masquerade_as_nightly_cargo()
1535+
.run();
1536+
1537+
let doc_file = p.root().join("target/doc/foo/index.html");
1538+
let mut doc_html = String::new();
1539+
File::open(&doc_file)
1540+
.unwrap()
1541+
.read_to_string(&mut doc_html)
1542+
.unwrap();
1543+
1544+
assert!(doc_html.contains("Version 1.2.4"));
1545+
}

0 commit comments

Comments
 (0)