@@ -448,6 +448,7 @@ func askUserForRsyncBack(ctx context.Context, cmd *cobra.Command, inst *limatype
448448 remoteSource := fmt .Sprintf ("%s:%s" , inst .Name , destRsyncDir )
449449 clean := filepath .Clean (hostCurrentDir )
450450 dirForCleanup := shellescape .Quote (filepath .Join (* inst .Config .User .Home , clean ))
451+ cleanupGuestWorkdir := false
451452
452453 rsyncBack := func () error {
453454 paths := []string {
@@ -459,10 +460,14 @@ func askUserForRsyncBack(ctx context.Context, cmd *cobra.Command, inst *limatype
459460 return fmt .Errorf ("failed to sync back the changes from guest instance to host: %w" , err )
460461 }
461462 logrus .Info ("Successfully synced back the changes to host." )
463+ cleanupGuestWorkdir = true
462464 return nil
463465 }
464466
465467 defer func () {
468+ if ! cleanupGuestWorkdir {
469+ return
470+ }
466471 // Clean up the guest synced workdir
467472 if err := executeSSHForRsync (ctx , * sshCmd , inst .SSHLocalPort , inst .SSHAddress , fmt .Sprintf ("rm -rf %s" , dirForCleanup )); err != nil {
468473 logrus .WithError (err ).Warn ("Failed to clean up guest synced workdir" )
@@ -479,6 +484,7 @@ func askUserForRsyncBack(ctx context.Context, cmd *cobra.Command, inst *limatype
479484 }
480485 if stats != nil && stats .String () == "" {
481486 logrus .Info ("No changes detected" )
487+ cleanupGuestWorkdir = true
482488 return nil
483489 }
484490 statsMsg := ""
@@ -497,7 +503,7 @@ func askUserForRsyncBack(ctx context.Context, cmd *cobra.Command, inst *limatype
497503
498504 baseDir , err := os .MkdirTemp ("" , "lima-guest-synced-*" )
499505 if err != nil {
500- return err
506+ return fmt . Errorf ( "failed to create temporary directory: %w; preserving guest synced workdir at %s" , err , destRsyncDir )
501507 }
502508 defer func () {
503509 if err := os .RemoveAll (baseDir ); err != nil {
@@ -507,21 +513,22 @@ func askUserForRsyncBack(ctx context.Context, cmd *cobra.Command, inst *limatype
507513 hostTmpDest := filepath .Join (baseDir , filepath .Base (hostCurrentDir ))
508514 err = os .MkdirAll (hostTmpDest , 0o755 )
509515 if err != nil {
510- return err
516+ return fmt . Errorf ( "failed to create temporary directory: %w; preserving guest synced workdir at %s" , err , destRsyncDir )
511517 }
512518
513519 rsyncToTempDir := false
514520
515521 for {
516522 ans , err := uiutil .Select (message , options )
517523 if err != nil {
518- return fmt .Errorf ("failed to open TUI: %w" , err )
524+ return fmt .Errorf ("failed to open TUI: %w; preserving guest synced workdir at %s " , err , destRsyncDir )
519525 }
520526
521527 switch ans {
522528 case 0 : // Yes
523529 return rsyncBack ()
524530 case 1 : // No
531+ cleanupGuestWorkdir = true
525532 logrus .Info ("Skipping syncing back the changes to host." )
526533 return nil
527534 case 2 : // View the changed contents
0 commit comments