@@ -267,7 +267,12 @@ impl Metadata {
267
267
cargo_args. push ( "--no-default-features" . into ( ) ) ;
268
268
}
269
269
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 ) ;
271
276
all_rustdoc_args. extend_from_slice ( rustdoc_args) ;
272
277
273
278
// Pass `RUSTFLAGS` and `RUSTDOCFLAGS` using `cargo --config`, which handles whitespace correctly.
@@ -650,14 +655,20 @@ mod test_targets {
650
655
mod test_calculations {
651
656
use super :: * ;
652
657
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
655
666
}
656
667
657
668
#[ test]
658
669
fn test_defaults ( ) {
659
670
let metadata = Metadata :: default ( ) ;
660
- assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , default_cargo_args( ) ) ;
671
+ assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , default_cargo_args( & [ ] ) ) ;
661
672
let env = metadata. environment_variables ( ) ;
662
673
assert_eq ! ( env. get( "DOCS_RS" ) . map( String :: as_str) , Some ( "1" ) ) ;
663
674
assert ! ( env. get( "RUSTDOCFLAGS" ) . is_none( ) ) ;
@@ -671,17 +682,15 @@ mod test_calculations {
671
682
all_features : true ,
672
683
..Metadata :: default ( )
673
684
} ;
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 ( ) ] ) ;
676
686
assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
677
687
678
688
// no default features
679
689
let metadata = Metadata {
680
690
no_default_features : true ,
681
691
..Metadata :: default ( )
682
692
} ;
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 ( ) ] ) ;
685
694
assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
686
695
687
696
// allow passing both even though it's nonsense; cargo will give an error anyway
@@ -690,9 +699,8 @@ mod test_calculations {
690
699
no_default_features : true ,
691
700
..Metadata :: default ( )
692
701
} ;
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 ( ) ] ) ;
696
704
assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
697
705
698
706
// explicit empty vec
@@ -706,6 +714,8 @@ mod test_calculations {
706
714
"-Zrustdoc-map" . into( ) ,
707
715
"--features" . into( ) ,
708
716
String :: new( ) ,
717
+ "--config" . into( ) ,
718
+ r#"build.rustdocflags=["--cfg", "docsrs"]"# . into( ) ,
709
719
] ;
710
720
assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
711
721
@@ -720,6 +730,8 @@ mod test_calculations {
720
730
"-Zrustdoc-map" . into( ) ,
721
731
"--features" . into( ) ,
722
732
"some_feature" . into( ) ,
733
+ "--config" . into( ) ,
734
+ r#"build.rustdocflags=["--cfg", "docsrs"]"# . into( ) ,
723
735
] ;
724
736
assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
725
737
@@ -734,6 +746,8 @@ mod test_calculations {
734
746
"-Zrustdoc-map" . into( ) ,
735
747
"--features" . into( ) ,
736
748
"feature1 feature2" . into( ) ,
749
+ "--config" . into( ) ,
750
+ r#"build.rustdocflags=["--cfg", "docsrs"]"# . into( ) ,
737
751
] ;
738
752
assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
739
753
@@ -754,7 +768,7 @@ mod test_calculations {
754
768
"--lib" . into( ) ,
755
769
"-Zrustdoc-map" . into( ) ,
756
770
"--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( ) ,
758
772
] ;
759
773
assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
760
774
@@ -773,6 +787,8 @@ mod test_calculations {
773
787
"-Ztarget-applies-to-host" . into( ) ,
774
788
"--config" . into( ) ,
775
789
"host.rustflags=[\" --cfg\" , \" x\" ]" . into( ) ,
790
+ "--config" . into( ) ,
791
+ "build.rustdocflags=[\" --cfg\" , \" docsrs\" ]" . into( ) ,
776
792
] ;
777
793
assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
778
794
@@ -785,6 +801,8 @@ mod test_calculations {
785
801
String :: from( "rustdoc" ) ,
786
802
"--lib" . into( ) ,
787
803
"-Zrustdoc-map" . into( ) ,
804
+ "--config" . into( ) ,
805
+ "build.rustdocflags=[\" --cfg\" , \" docsrs\" ]" . into( ) ,
788
806
"-Zbuild-std" . into( ) ,
789
807
] ;
790
808
assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
0 commit comments