@@ -49,6 +49,8 @@ use crate::util::machine_message::Message;
4949use  crate :: util:: { self ,  machine_message,  ProcessBuilder } ; 
5050use  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 
0 commit comments