@@ -267,10 +267,15 @@ 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
- if !self . rustc_args . is_empty ( ) || ! all_rustdoc_args. is_empty ( ) {
278
+ if !self . rustc_args . is_empty ( ) || all_rustdoc_args. len ( ) > 2 {
274
279
cargo_args. push ( "-Z" . into ( ) ) ;
275
280
cargo_args. push ( "unstable-options" . into ( ) ) ;
276
281
}
@@ -655,14 +660,20 @@ mod test_targets {
655
660
mod test_calculations {
656
661
use super :: * ;
657
662
658
- fn default_cargo_args ( ) -> Vec < String > {
659
- vec ! [ "rustdoc" . into( ) , "--lib" . into( ) , "-Zrustdoc-map" . into( ) ]
663
+ fn default_cargo_args ( extra_args : & [ String ] ) -> Vec < String > {
664
+ let mut args = vec ! [ "rustdoc" . into( ) , "--lib" . into( ) , "-Zrustdoc-map" . into( ) ] ;
665
+ args. extend_from_slice ( extra_args) ;
666
+ args. extend_from_slice ( & [
667
+ "--config" . into ( ) ,
668
+ r#"build.rustdocflags=["--cfg", "docsrs"]"# . into ( ) ,
669
+ ] ) ;
670
+ args
660
671
}
661
672
662
673
#[ test]
663
674
fn test_defaults ( ) {
664
675
let metadata = Metadata :: default ( ) ;
665
- assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , default_cargo_args( ) ) ;
676
+ assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , default_cargo_args( & [ ] ) ) ;
666
677
let env = metadata. environment_variables ( ) ;
667
678
assert_eq ! ( env. get( "DOCS_RS" ) . map( String :: as_str) , Some ( "1" ) ) ;
668
679
assert ! ( env. get( "RUSTDOCFLAGS" ) . is_none( ) ) ;
@@ -676,17 +687,15 @@ mod test_calculations {
676
687
all_features : true ,
677
688
..Metadata :: default ( )
678
689
} ;
679
- let mut expected_args = default_cargo_args ( ) ;
680
- expected_args. push ( "--all-features" . into ( ) ) ;
690
+ let expected_args = default_cargo_args ( & [ "--all-features" . into ( ) ] ) ;
681
691
assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
682
692
683
693
// no default features
684
694
let metadata = Metadata {
685
695
no_default_features : true ,
686
696
..Metadata :: default ( )
687
697
} ;
688
- let mut expected_args = default_cargo_args ( ) ;
689
- expected_args. push ( "--no-default-features" . into ( ) ) ;
698
+ let expected_args = default_cargo_args ( & [ "--no-default-features" . into ( ) ] ) ;
690
699
assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
691
700
692
701
// allow passing both even though it's nonsense; cargo will give an error anyway
@@ -695,9 +704,8 @@ mod test_calculations {
695
704
no_default_features : true ,
696
705
..Metadata :: default ( )
697
706
} ;
698
- let mut expected_args = default_cargo_args ( ) ;
699
- expected_args. push ( "--all-features" . into ( ) ) ;
700
- expected_args. push ( "--no-default-features" . into ( ) ) ;
707
+ let expected_args =
708
+ default_cargo_args ( & [ "--all-features" . into ( ) , "--no-default-features" . into ( ) ] ) ;
701
709
assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
702
710
703
711
// explicit empty vec
@@ -711,6 +719,8 @@ mod test_calculations {
711
719
"-Zrustdoc-map" . into( ) ,
712
720
"--features" . into( ) ,
713
721
String :: new( ) ,
722
+ "--config" . into( ) ,
723
+ r#"build.rustdocflags=["--cfg", "docsrs"]"# . into( ) ,
714
724
] ;
715
725
assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
716
726
@@ -725,6 +735,8 @@ mod test_calculations {
725
735
"-Zrustdoc-map" . into( ) ,
726
736
"--features" . into( ) ,
727
737
"some_feature" . into( ) ,
738
+ "--config" . into( ) ,
739
+ r#"build.rustdocflags=["--cfg", "docsrs"]"# . into( ) ,
728
740
] ;
729
741
assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
730
742
@@ -739,6 +751,8 @@ mod test_calculations {
739
751
"-Zrustdoc-map" . into( ) ,
740
752
"--features" . into( ) ,
741
753
"feature1 feature2" . into( ) ,
754
+ "--config" . into( ) ,
755
+ r#"build.rustdocflags=["--cfg", "docsrs"]"# . into( ) ,
742
756
] ;
743
757
assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
744
758
@@ -761,7 +775,7 @@ mod test_calculations {
761
775
"-Z" . into( ) ,
762
776
"unstable-options" . into( ) ,
763
777
"--config" . into( ) ,
764
- r#"build.rustdocflags=["-Z", "unstable-options", "--static-root-path", "/", "--cap-lints", "warn"]"# . into( ) ,
778
+ r#"build.rustdocflags=["--cfg", "docsrs", "- Z", "unstable-options", "--static-root-path", "/", "--cap-lints", "warn"]"# . into( ) ,
765
779
] ;
766
780
assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
767
781
@@ -782,6 +796,8 @@ mod test_calculations {
782
796
"-Ztarget-applies-to-host" . into( ) ,
783
797
"--config" . into( ) ,
784
798
"host.rustflags=[\" --cfg\" , \" x\" ]" . into( ) ,
799
+ "--config" . into( ) ,
800
+ "build.rustdocflags=[\" --cfg\" , \" docsrs\" ]" . into( ) ,
785
801
] ;
786
802
assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
787
803
@@ -794,6 +810,8 @@ mod test_calculations {
794
810
String :: from( "rustdoc" ) ,
795
811
"--lib" . into( ) ,
796
812
"-Zrustdoc-map" . into( ) ,
813
+ "--config" . into( ) ,
814
+ "build.rustdocflags=[\" --cfg\" , \" docsrs\" ]" . into( ) ,
797
815
"-Zbuild-std" . into( ) ,
798
816
] ;
799
817
assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
0 commit comments