Skip to content

Commit 240d3e8

Browse files
Urgausyphar
authored andcommitted
Add "--cfg docsrs" to all rustdoc invocations
1 parent a29f0af commit 240d3e8

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

crates/metadata/lib.rs

+30-12
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,12 @@ impl Metadata {
267267
cargo_args.push("--no-default-features".into());
268268
}
269269

270-
let mut all_rustdoc_args = self.rustdoc_args.clone();
270+
// Unconditionnaly set `--cfg docsrs` as it has become a de-facto way to
271+
// distinguish docs.rs.
272+
//
273+
// See https://github.com/rust-lang/docs.rs/issues/2389.
274+
let mut all_rustdoc_args = vec!["--cfg".into(), "docsrs".into()];
275+
all_rustdoc_args.extend_from_slice(&self.rustdoc_args);
271276
all_rustdoc_args.extend_from_slice(rustdoc_args);
272277

273278
// Pass `RUSTFLAGS` and `RUSTDOCFLAGS` using `cargo --config`, which handles whitespace correctly.
@@ -650,14 +655,20 @@ mod test_targets {
650655
mod test_calculations {
651656
use super::*;
652657

653-
fn default_cargo_args() -> Vec<String> {
654-
vec!["rustdoc".into(), "--lib".into(), "-Zrustdoc-map".into()]
658+
fn default_cargo_args(extra_args: &[String]) -> Vec<String> {
659+
let mut args = vec!["rustdoc".into(), "--lib".into(), "-Zrustdoc-map".into()];
660+
args.extend_from_slice(extra_args);
661+
args.extend_from_slice(&[
662+
"--config".into(),
663+
r#"build.rustdocflags=["--cfg", "docsrs"]"#.into(),
664+
]);
665+
args
655666
}
656667

657668
#[test]
658669
fn test_defaults() {
659670
let metadata = Metadata::default();
660-
assert_eq!(metadata.cargo_args(&[], &[]), default_cargo_args());
671+
assert_eq!(metadata.cargo_args(&[], &[]), default_cargo_args(&[]));
661672
let env = metadata.environment_variables();
662673
assert_eq!(env.get("DOCS_RS").map(String::as_str), Some("1"));
663674
assert!(env.get("RUSTDOCFLAGS").is_none());
@@ -671,17 +682,15 @@ mod test_calculations {
671682
all_features: true,
672683
..Metadata::default()
673684
};
674-
let mut expected_args = default_cargo_args();
675-
expected_args.push("--all-features".into());
685+
let expected_args = default_cargo_args(&["--all-features".into()]);
676686
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);
677687

678688
// no default features
679689
let metadata = Metadata {
680690
no_default_features: true,
681691
..Metadata::default()
682692
};
683-
let mut expected_args = default_cargo_args();
684-
expected_args.push("--no-default-features".into());
693+
let expected_args = default_cargo_args(&["--no-default-features".into()]);
685694
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);
686695

687696
// allow passing both even though it's nonsense; cargo will give an error anyway
@@ -690,9 +699,8 @@ mod test_calculations {
690699
no_default_features: true,
691700
..Metadata::default()
692701
};
693-
let mut expected_args = default_cargo_args();
694-
expected_args.push("--all-features".into());
695-
expected_args.push("--no-default-features".into());
702+
let expected_args =
703+
default_cargo_args(&["--all-features".into(), "--no-default-features".into()]);
696704
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);
697705

698706
// explicit empty vec
@@ -706,6 +714,8 @@ mod test_calculations {
706714
"-Zrustdoc-map".into(),
707715
"--features".into(),
708716
String::new(),
717+
"--config".into(),
718+
r#"build.rustdocflags=["--cfg", "docsrs"]"#.into(),
709719
];
710720
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);
711721

@@ -720,6 +730,8 @@ mod test_calculations {
720730
"-Zrustdoc-map".into(),
721731
"--features".into(),
722732
"some_feature".into(),
733+
"--config".into(),
734+
r#"build.rustdocflags=["--cfg", "docsrs"]"#.into(),
723735
];
724736
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);
725737

@@ -734,6 +746,8 @@ mod test_calculations {
734746
"-Zrustdoc-map".into(),
735747
"--features".into(),
736748
"feature1 feature2".into(),
749+
"--config".into(),
750+
r#"build.rustdocflags=["--cfg", "docsrs"]"#.into(),
737751
];
738752
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);
739753

@@ -754,7 +768,7 @@ mod test_calculations {
754768
"--lib".into(),
755769
"-Zrustdoc-map".into(),
756770
"--config".into(),
757-
r#"build.rustdocflags=["-Z", "unstable-options", "--static-root-path", "/", "--cap-lints", "warn"]"#.into(),
771+
r#"build.rustdocflags=["--cfg", "docsrs", "-Z", "unstable-options", "--static-root-path", "/", "--cap-lints", "warn"]"#.into(),
758772
];
759773
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);
760774

@@ -773,6 +787,8 @@ mod test_calculations {
773787
"-Ztarget-applies-to-host".into(),
774788
"--config".into(),
775789
"host.rustflags=[\"--cfg\", \"x\"]".into(),
790+
"--config".into(),
791+
"build.rustdocflags=[\"--cfg\", \"docsrs\"]".into(),
776792
];
777793
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);
778794

@@ -785,6 +801,8 @@ mod test_calculations {
785801
String::from("rustdoc"),
786802
"--lib".into(),
787803
"-Zrustdoc-map".into(),
804+
"--config".into(),
805+
"build.rustdocflags=[\"--cfg\", \"docsrs\"]".into(),
788806
"-Zbuild-std".into(),
789807
];
790808
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);

templates/core/about/builds.html

+11-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ <h4 id="setting-a-readme"> <a href="#setting-a-readme">Setting a README</a> </h4
2929
</p>
3030

3131
<h4 id="detecting-docsrs"> <a href="#detecting-docsrs">Detecting Docs.rs</a> </h4>
32+
<p>
33+
To recognize Docs.rs from your Rust code, you can test for the <code>docsrs</code> cfg, e.g.:
34+
{% filter highlight(lang="rust") %}{% filter dedent(levels=3) -%}
35+
#[cfg(docsrs)]
36+
mod documentation;
37+
{%- endfilter %}{% endfilter %}
38+
The `docsrs` cfg only applies to the final rustdoc invocation (i.e. the crate currently
39+
being documented). It does not apply to dependencies (including workspace ones).
40+
</p>
3241
<p>
3342
To recognize Docs.rs from <code>build.rs</code> files, you can test for the environment variable <code>DOCS_RS</code>, e.g.:
3443
{% filter highlight(lang="rust") %}{% filter dedent(levels=3) -%}
@@ -48,9 +57,9 @@ <h4 id="cross-compiling"> <a href="#cross-compiling">Cross-compiling</a> </h4>
4857
You can configure how your crate is built by adding <a href="metadata">package metadata</a> to your <code>Cargo.toml</code>, e.g.:
4958
{% filter highlight(lang="toml") %}{% filter dedent -%}
5059
[package.metadata.docs.rs]
51-
rustc-args = ["--cfg", "docsrs"]
60+
rustc-args = ["--cfg", "my_cfg"]
5261
{%- endfilter %}{% endfilter %}
53-
Here, the compiler arguments are set so that <code>#[cfg(docsrs)]</code> (not to be confused with <code>#[cfg(doc)]</code>) can be used for conditional compilation.
62+
Here, the compiler arguments are set so that <code>#[cfg(my_cfg)]</code> (not to be confused with <code>#[cfg(doc)]</code>) can be used for conditional compilation.
5463
This approach is also useful for setting <a href="https://doc.rust-lang.org/cargo/reference/features.html">cargo features</a>.
5564
</p>
5665

0 commit comments

Comments
 (0)