@@ -297,7 +297,7 @@ impl<'gctx> InstallablePackage<'gctx> {
297297 Ok ( duplicates)
298298 }
299299
300- fn install_one ( mut self ) -> CargoResult < bool > {
300+ fn install_one ( mut self , dry_run : bool ) -> CargoResult < bool > {
301301 self . gctx . shell ( ) . status ( "Installing" , & self . pkg ) ?;
302302
303303 let dst = self . root . join ( "bin" ) . into_path_unlocked ( ) ;
@@ -321,6 +321,7 @@ impl<'gctx> InstallablePackage<'gctx> {
321321 self . check_yanked_install ( ) ?;
322322
323323 let exec: Arc < dyn Executor > = Arc :: new ( DefaultExecutor ) ;
324+ self . opts . build_config . dry_run = dry_run;
324325 let compile = ops:: compile_ws ( & self . ws , & self . opts , & exec) . with_context ( || {
325326 if let Some ( td) = td_opt. take ( ) {
326327 // preserve the temporary directory, so the user can inspect it
@@ -422,7 +423,7 @@ impl<'gctx> InstallablePackage<'gctx> {
422423 for & ( bin, src) in binaries. iter ( ) {
423424 let dst = staging_dir. path ( ) . join ( bin) ;
424425 // Try to move if `target_dir` is transient.
425- if !self . source_id . is_path ( ) && fs:: rename ( src, & dst) . is_ok ( ) {
426+ if dry_run || ( !self . source_id . is_path ( ) && fs:: rename ( src, & dst) . is_ok ( ) ) {
426427 continue ;
427428 }
428429 paths:: copy ( src, & dst) ?;
@@ -441,11 +442,13 @@ impl<'gctx> InstallablePackage<'gctx> {
441442 let src = staging_dir. path ( ) . join ( bin) ;
442443 let dst = dst. join ( bin) ;
443444 self . gctx . shell ( ) . status ( "Installing" , dst. display ( ) ) ?;
444- fs:: rename ( & src, & dst) . with_context ( || {
445- format ! ( "failed to move `{}` to `{}`" , src. display( ) , dst. display( ) )
446- } ) ?;
447- installed. bins . push ( dst) ;
448- successful_bins. insert ( bin. to_string ( ) ) ;
445+ if !dry_run {
446+ fs:: rename ( & src, & dst) . with_context ( || {
447+ format ! ( "failed to move `{}` to `{}`" , src. display( ) , dst. display( ) )
448+ } ) ?;
449+ installed. bins . push ( dst) ;
450+ successful_bins. insert ( bin. to_string ( ) ) ;
451+ }
449452 }
450453
451454 // Repeat for binaries which replace existing ones but don't pop the error
@@ -456,10 +459,12 @@ impl<'gctx> InstallablePackage<'gctx> {
456459 let src = staging_dir. path ( ) . join ( bin) ;
457460 let dst = dst. join ( bin) ;
458461 self . gctx . shell ( ) . status ( "Replacing" , dst. display ( ) ) ?;
459- fs:: rename ( & src, & dst) . with_context ( || {
460- format ! ( "failed to move `{}` to `{}`" , src. display( ) , dst. display( ) )
461- } ) ?;
462- successful_bins. insert ( bin. to_string ( ) ) ;
462+ if !dry_run {
463+ fs:: rename ( & src, & dst) . with_context ( || {
464+ format ! ( "failed to move `{}` to `{}`" , src. display( ) , dst. display( ) )
465+ } ) ?;
466+ successful_bins. insert ( bin. to_string ( ) ) ;
467+ }
463468 }
464469 Ok ( ( ) )
465470 } ;
@@ -476,9 +481,14 @@ impl<'gctx> InstallablePackage<'gctx> {
476481 & self . rustc . verbose_version ,
477482 ) ;
478483
479- if let Err ( e) =
480- remove_orphaned_bins ( & self . ws , & mut tracker, & duplicates, & self . pkg , & dst)
481- {
484+ if let Err ( e) = remove_orphaned_bins (
485+ & self . ws ,
486+ & mut tracker,
487+ & duplicates,
488+ & self . pkg ,
489+ & dst,
490+ dry_run,
491+ ) {
482492 // Don't hard error on remove.
483493 self . gctx
484494 . shell ( )
@@ -515,7 +525,10 @@ impl<'gctx> InstallablePackage<'gctx> {
515525 }
516526 }
517527
518- if duplicates. is_empty ( ) {
528+ if dry_run {
529+ self . gctx . shell ( ) . warn ( "aborting install due to dry run" ) ?;
530+ Ok ( true )
531+ } else if duplicates. is_empty ( ) {
519532 self . gctx . shell ( ) . status (
520533 "Installed" ,
521534 format ! (
@@ -620,6 +633,7 @@ pub fn install(
620633 opts : & ops:: CompileOptions ,
621634 force : bool ,
622635 no_track : bool ,
636+ dry_run : bool ,
623637) -> CargoResult < ( ) > {
624638 let root = resolve_root ( root, gctx) ?;
625639 let dst = root. join ( "bin" ) . into_path_unlocked ( ) ;
@@ -654,7 +668,7 @@ pub fn install(
654668 ) ?;
655669 let mut installed_anything = true ;
656670 if let Some ( installable_pkg) = installable_pkg {
657- installed_anything = installable_pkg. install_one ( ) ?;
671+ installed_anything = installable_pkg. install_one ( dry_run ) ?;
658672 }
659673 ( installed_anything, false )
660674 } else {
@@ -705,7 +719,7 @@ pub fn install(
705719
706720 let install_results: Vec < _ > = pkgs_to_install
707721 . into_iter ( )
708- . map ( |( krate, installable_pkg) | ( krate, installable_pkg. install_one ( ) ) )
722+ . map ( |( krate, installable_pkg) | ( krate, installable_pkg. install_one ( dry_run ) ) )
709723 . collect ( ) ;
710724
711725 for ( krate, result) in install_results {
@@ -857,6 +871,7 @@ fn remove_orphaned_bins(
857871 duplicates : & BTreeMap < String , Option < PackageId > > ,
858872 pkg : & Package ,
859873 dst : & Path ,
874+ dry_run : bool ,
860875) -> CargoResult < ( ) > {
861876 let filter = ops:: CompileFilter :: new_all_targets ( ) ;
862877 let all_self_names = exe_names ( pkg, & filter) ;
@@ -894,8 +909,10 @@ fn remove_orphaned_bins(
894909 old_pkg
895910 ) ,
896911 ) ?;
897- paths:: remove_file ( & full_path)
898- . with_context ( || format ! ( "failed to remove {:?}" , full_path) ) ?;
912+ if !dry_run {
913+ paths:: remove_file ( & full_path)
914+ . with_context ( || format ! ( "failed to remove {:?}" , full_path) ) ?;
915+ }
899916 }
900917 }
901918 }
0 commit comments