Skip to content

Conversation

@unsuman
Copy link
Contributor

@unsuman unsuman commented Dec 4, 2025

This PR adds a --sync-host-workdir flag to the limactl shell command that allows users to safely run AI commands inside VMs by syncing the host's working directory to the guest and optionally syncing changes back to the host after confirmation. Also added cleanup logic to remove the guest's synced workdir after user decision, ensuring no leftover files in the VM.

How to test

$ limactl start --mount-none
$ cd ~/some-project
$ limactl shell --sync-host-workdir <AI Agent Command>
OR
$ limactl shell --sync-host-workdir # simply shell into the instance and make changes
INFO[0000] Syncing host current directory(/Users/ansumansahoo/temp) to guest instance... 
INFO[0000] Successfully synced host current directory to guest(~/synced-workdir/temp) instance. 
ansumansahoo@lima-default:~/synced-workdir/temp$ vi source_dir/initials.txt 
ansumansahoo@lima-default:~/synced-workdir/temp$ exit
logout
? ⚠️ Accept the changes?  [Use arrows to move, type to filter]
  Yes
  No
> View the changed contents

When the program prompts the user to view the changes, a rsync is performed. This copies the guest synced directory to a temporary directory on the host. This allows diff to be used and the user to see a detailed list of changes.
Fixes #3711

shellCmd.Flags().Bool("reconnect", false, "Reconnect to the SSH session")
shellCmd.Flags().Bool("preserve-env", false, "Propagate environment variables to the shell")
shellCmd.Flags().Bool("start", false, "Start the instance if it is not already running")
shellCmd.Flags().Bool("sync-host-workdir", false, "Copy the host working directory to the guest to run AI commands inside VMs (prevents AI agents from breaking the host files)")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • For consistency with --mount ., probably this should be like --sync .
  • The flag is not really specific to AI commands. Use case with AI should be mentioned in https://lima-vm.io/docs/examples/ai/ though.


const (
rsyncMinimumSrcDirDepth = 4 // Depth of "/Users/USER" is 3.
guestSyncedWorkdir = "~/synced-workdir"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason that we cannot just use the same path as the host dir?
Is that for avoiding conflicts with mounts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason that we cannot just use the same path as the host dir?

Yes, issue with rsync because it only tries to create the base of the path and not the full path so using for example ansumansahoo@127.0.0.1:~/Users/ansumansahoo/Documents/GOLANG/lima as a destination path will result into an error rsync: [Receiver] mkdir "/home/ansumansahoo.linux/Users/ansumansahoo/Documents/GOLANG/lima" failed: No such file or directory (2)

Or do you mean to use only ansumansahoo@127.0.0.1:~/lima regarding this context?

}

for {
ans, err := uiutil.Select(message, options)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rsync can be slow and may eat up the host disk space, so this prompt should be shown before running rsync to the tmp dir

return
case 2: // View the changed contents
diffCmd := exec.CommandContext(ctx, "diff", "-ru", "--color=always", hostCurrentDir, filepath.Join(hostTmpDest, filepath.Base(hostCurrentDir)))
lessCmd := exec.CommandContext(ctx, "less", "-R")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Respect $PAGER

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs bats tests and docs

if err != nil {
return fmt.Errorf("failed to get sync-host-workdir flag: %w", err)
} else if syncHostWorkdir && len(inst.Config.Mounts) > 0 {
return errors.New("cannot use `--sync-host-workdir` when the instance has host mounts configured, use `--mount-none` to disable mounts")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return errors.New("cannot use `--sync-host-workdir` when the instance has host mounts configured, use `--mount-none` to disable mounts")
return errors.New("cannot use `--sync-host-workdir` when the instance has host mounts configured, start the instance with `--mount-none` to disable mounts")

}
} else {
case syncHostWorkdir:
changeDirCmd = fmt.Sprintf("cd %s/%s", guestSyncedWorkdir, filepath.Base(hostCurrentDir))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to quote the path, as it may contain spaces and quote symbols

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Such paths should be tested in bats)

}
rsyncCmd := exec.CommandContext(ctx, "rsync", rsyncArgs...)
rsyncCmd.Stdin = os.Stdin
rsyncCmd.Stdout = os.Stdout
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

destination,
}
rsyncCmd := exec.CommandContext(ctx, "rsync", rsyncArgs...)
rsyncCmd.Stdin = os.Stdin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NVM, it was a mistake!

…g the host files

Signed-off-by: Ansuman Sahoo <anshumansahoo500@gmail.com>
@unsuman
Copy link
Contributor Author

unsuman commented Dec 5, 2025

Left with adding bats test and docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add limactl shell --sync-host-workdir (prevents AI agents from breaking the host files)

2 participants