@@ -551,6 +551,49 @@ fn doc_std(
551551 extra_args : & [ & OsStr ] ,
552552 requested_crates : & [ String ] ,
553553) {
554+ // `cargo` uses the same directory for both JSON docs and HTML docs.
555+ // This could lead to cross-contamination when copying files into the specified `out` directory.
556+ // For example:
557+ // ```bash
558+ // x doc std
559+ // x doc std --json
560+ // ```
561+ // could lead to HTML docs being copied into the JSON docs output directory.
562+ // To avoid this issue, we copy generated docs instead of whole directory by
563+ // checking doc format and generated files.
564+ fn cp_docs_by_doc_format (
565+ format : & DocumentationFormat ,
566+ builder : & Builder < ' _ > ,
567+ src : & Path ,
568+ dst : & Path ,
569+ ) {
570+ for f in builder. read_dir ( src) {
571+ let path = f. path ( ) ;
572+ let name = path. file_name ( ) . unwrap ( ) ;
573+ let dst = dst. join ( name) ;
574+
575+ if t ! ( f. file_type( ) ) . is_dir ( ) && format == & DocumentationFormat :: HTML {
576+ t ! ( fs:: create_dir_all( & dst) ) ;
577+ cp_docs_by_doc_format ( format, builder, & path, & dst) ;
578+ } else {
579+ let _ = fs:: remove_file ( & dst) ;
580+ let extension = path. extension ( ) . and_then ( OsStr :: to_str) ;
581+
582+ match format {
583+ DocumentationFormat :: HTML if extension != Some ( "json" ) => {
584+ builder. copy ( & path, & dst)
585+ }
586+ DocumentationFormat :: JSON
587+ if extension == Some ( "json" ) || name. to_str ( ) == Some ( ".stamp" ) =>
588+ {
589+ builder. copy ( & path, & dst)
590+ }
591+ _ => { }
592+ }
593+ }
594+ }
595+ }
596+
554597 builder. info ( & format ! (
555598 "Documenting stage{} std ({}) in {} format" ,
556599 stage,
@@ -568,18 +611,6 @@ fn doc_std(
568611 // We will then copy the files from this directory into the final `out` directory, the specified
569612 // as a function parameter.
570613 let out_dir = builder. stage_out ( compiler, Mode :: Std ) . join ( target. triple ) . join ( "doc" ) ;
571- // `cargo` uses the same directory for both JSON docs and HTML docs.
572- // This could lead to cross-contamination when copying files into the specified `out` directory.
573- // For example:
574- // ```bash
575- // x doc std
576- // x doc std --json
577- // ```
578- // could lead to HTML docs being copied into the JSON docs output directory.
579- // To avoid this issue, we clean the doc folder before invoking `cargo`.
580- if out_dir. exists ( ) {
581- builder. remove_dir ( & out_dir) ;
582- }
583614
584615 let run_cargo_rustdoc_for = |package : & str | {
585616 let mut cargo = builder. cargo ( compiler, Mode :: Std , SourceType :: InTree , target, "rustdoc" ) ;
@@ -605,7 +636,9 @@ fn doc_std(
605636 }
606637 }
607638
608- builder. cp_r ( & out_dir, & out) ;
639+ if !builder. config . dry_run ( ) {
640+ cp_docs_by_doc_format ( & format, builder, & out_dir, & out) ;
641+ }
609642}
610643
611644#[ derive( Debug , Copy , Clone , Hash , PartialEq , Eq ) ]
0 commit comments