Skip to content

Commit c9e3141

Browse files
committed
Do not supply --crate-version flag to rustdoc if present in RUSTDOCFLAGS or extra compiler arguments
1 parent 92d0ce8 commit c9e3141

File tree

2 files changed

+68
-15
lines changed

2 files changed

+68
-15
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ use crate::util::machine_message::Message;
4949
use crate::util::{self, machine_message, ProcessBuilder};
5050
use crate::util::{internal, join_paths, paths, profile};
5151

52+
const RUSTDOC_CRATE_VERSION_FLAG: &str = "--crate-version";
53+
5254
/// A glorified callback for executing calls to rustc. Rather than calling rustc
5355
/// directly, we'll use an `Executor`, giving clients an opportunity to intercept
5456
/// the build calls.
@@ -562,7 +564,6 @@ fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult
562564
let mut rustdoc = cx.compilation.rustdoc_process(unit.pkg, unit.target)?;
563565
rustdoc.inherit_jobserver(&cx.jobserver);
564566
rustdoc.arg("--crate-name").arg(&unit.target.crate_name());
565-
add_crate_versions_if_requested(bcx, unit, &mut rustdoc);
566567
add_path_args(bcx, unit, &mut rustdoc);
567568
add_cap_lints(bcx, unit, &mut rustdoc);
568569

@@ -593,6 +594,8 @@ fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult
593594

594595
rustdoc.args(bcx.rustdocflags_args(unit));
595596

597+
add_crate_versions_if_requested(bcx, unit, &mut rustdoc);
598+
596599
let name = unit.pkg.name().to_string();
597600
let build_script_outputs = Arc::clone(&cx.build_script_outputs);
598601
let package_id = unit.pkg.package_id();
@@ -629,19 +632,31 @@ fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult
629632
}))
630633
}
631634

632-
fn add_crate_versions_if_requested(
633-
bcx: &BuildContext<'_, '_>,
634-
unit: &Unit<'_>,
635+
fn add_crate_versions_if_requested<'a>(
636+
bcx: &BuildContext<'a, '_>,
637+
unit: &Unit<'a>,
635638
rustdoc: &mut ProcessBuilder,
636639
) {
637-
if !bcx.config.cli_unstable().crate_versions {
638-
return;
640+
if bcx.config.cli_unstable().crate_versions && !crate_version_flag_already_present(rustdoc) {
641+
append_crate_version_flag(unit, rustdoc);
639642
}
643+
}
644+
645+
// The --crate-version flag could have already been passed in RUSTDOCFLAGS
646+
// or as an extra compiler argument for rustdoc
647+
fn crate_version_flag_already_present(rustdoc: &ProcessBuilder) -> bool {
648+
rustdoc.get_args().iter().any(|flag| {
649+
flag.to_str()
650+
.map_or(false, |flag| flag.starts_with(RUSTDOC_CRATE_VERSION_FLAG))
651+
})
652+
}
653+
654+
fn append_crate_version_flag(unit: &Unit<'_>, rustdoc: &mut ProcessBuilder) {
640655
rustdoc
641656
.arg("-Z")
642657
.arg("unstable-options")
643-
.arg("--crate-version")
644-
.arg(&unit.pkg.version().to_string());
658+
.arg(RUSTDOC_CRATE_VERSION_FLAG)
659+
.arg(unit.pkg.version().to_string());
645660
}
646661

647662
// The path that we pass to rustc is actually fairly important because it will

tests/testsuite/doc.rs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,12 +1534,50 @@ fn crate_versions() {
15341534
.masquerade_as_nightly_cargo()
15351535
.run();
15361536

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();
1537+
let output_path = p.root().join("target/doc/foo/index.html");
1538+
let output_documentation = fs::read_to_string(&output_path).unwrap();
1539+
1540+
assert!(output_documentation.contains("Version 1.2.4"));
1541+
}
1542+
1543+
#[cargo_test]
1544+
fn crate_versions_flag_is_overridden() {
1545+
// Testing unstable flag
1546+
if !is_nightly() {
1547+
return;
1548+
}
1549+
let p = project()
1550+
.file(
1551+
"Cargo.toml",
1552+
r#"
1553+
[package]
1554+
name = "foo"
1555+
version = "1.2.4"
1556+
authors = []
1557+
"#,
1558+
)
1559+
.file("src/lib.rs", "")
1560+
.build();
15431561

1544-
assert!(doc_html.contains("Version 1.2.4"));
1562+
let output_documentation = || {
1563+
let output_path = p.root().join("target/doc/foo/index.html");
1564+
fs::read_to_string(&output_path).unwrap()
1565+
};
1566+
let asserts = |html: String| {
1567+
assert!(!html.contains("1.2.4"));
1568+
assert!(html.contains("Version 2.0.3"));
1569+
};
1570+
1571+
p.cargo("-Z crate-versions doc")
1572+
.masquerade_as_nightly_cargo()
1573+
.env("RUSTDOCFLAGS", "-Z unstable-options --crate-version 2.0.3")
1574+
.run();
1575+
asserts(output_documentation());
1576+
1577+
p.build_dir().rm_rf();
1578+
1579+
p.cargo("-Z crate-versions rustdoc -- -Z unstable-options --crate-version 2.0.3")
1580+
.masquerade_as_nightly_cargo()
1581+
.run();
1582+
asserts(output_documentation());
15451583
}

0 commit comments

Comments
 (0)