twpayne / chezmoi

Manage your dotfiles across multiple diverse machines, securely.
https://www.chezmoi.io/
MIT License
12.9k stars 478 forks source link

vim autocommand for automatically applying changes does not cover "chezmoi edit" #3570

Closed terminalmage closed 7 months ago

terminalmage commented 7 months ago

Describe the bug

In the docs, the following autocommand is recommended to automatically chezmoi apply a file when you save it.

autocmd BufWritePost ~/.local/share/chezmoi/* ! chezmoi apply --source-path "%"

However, chezmoi edit does not open the corresponding file from its position in ~/.local/share/chezmoi/. It creates a tempdir, adds the directory path relative to your homedir, and opens the file underneath that. For example, when I run chezmoi edit ~/.config/tmux/tmux.conf (which is templated), the file that chezmoi opens is:

/tmp/chezmoi-edit227261993/.config/tmux/tmux.conf.tmpl

This does not match the path from my autocommand, so of course it does not properly apply changes.

As a workaround, I have added the following autocommand:

autocmd BufWritePost /tmp/chezmoi-edit* silent ! chezmoi apply

There are a couple downsides to this though:

  1. I don't have the benefit of using chezmoi apply --source-path, since the path opened by chezmoi is not within ~/.local/share/chezmoi.
  2. Since I can't use --source-path, I have to run chezmoi apply, which applies to all. But there's a problem with this approach. If there are local changes, chezmoi will ask if you want to skip/overwrite/etc., but when run via an autocommand, even with silent removed, I don't get that skip/overwrite/etc. prompt in vim. Instead, it just fails silently (again, even with silent removed). No changes can be automatically applied until I overwrite the file or revert the local changes.

Neither of these options are great. Ideally, the files would just be opened in ~/.local/share/chezmoi (and they seem like they were at one time, or else the docs wouldn't have been written that way).

To reproduce

With EDITOR=vim (or EDITOR=nvim), run chezmoi edit on any file managed by chezmoi.

Expected behavior

File is opened under ~/.local/share/chezmoi, as suggested in the docs

Output of command with the --verbose flag

No output

Output of chezmoi doctor

RESULT    CHECK                       MESSAGE
ok        version                     v2.45.0, commit 371703e93f0c54dad81f7cfbea2906ece15a7428, built at 2024-01-13T20:01:02Z, built by goreleaser
warning   latest-version              v2.46.1
ok        os-arch                     linux/amd64 (Ubuntu 20.04.6 LTS (Focal Fossa))
ok        uname                       Linux chi-lps2kmx1 5.15.0-92-generic #102~20.04.1-Ubuntu SMP Mon Jan 15 13:09:14 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
ok        go-version                  go1.21.6 (gc)
ok        executable                  ~/.local/bin/chezmoi
ok        upgrade-method              replace-executable
ok        config-file                 no config file found
ok        source-dir                  ~/.local/share/chezmoi is a git working tree (clean)
ok        suspicious-entries          no suspicious entries
ok        working-tree                ~/.local/share/chezmoi is a git working tree (clean)
ok        dest-dir                    ~ is a directory
ok        umask                       022
ok        cd-command                  found /usr/bin/zsh
ok        cd-args                     /usr/bin/zsh
info      diff-command                not set
ok        edit-command                found ~/neovim/current/bin/nvim
ok        edit-args                   ~/neovim/current/bin/nvim
ok        git-command                 found /usr/bin/git, version 2.25.1
ok        merge-command               found /usr/bin/vimdiff
ok        shell-command               found /usr/bin/zsh
ok        shell-args                  /usr/bin/zsh
info      age-command                 age not found in $PATH
ok        gpg-command                 found /usr/bin/gpg, version 2.2.19
info      pinentry-command            not set
info      1password-command           op not found in $PATH
info      bitwarden-command           bw not found in $PATH
info      bitwarden-secrets-command   bws not found in $PATH
info      dashlane-command            dcli not found in $PATH
info      doppler-command             doppler not found in $PATH
info      gopass-command              gopass not found in $PATH
info      keepassxc-command           keepassxc-cli not found in $PATH
info      keepassxc-db                not set
info      keeper-command              keeper not found in $PATH
info      lastpass-command            lpass not found in $PATH
info      pass-command                pass not found in $PATH
info      passhole-command            ph not found in $PATH
info      rbw-command                 rbw not found in $PATH
info      vault-command               vault not found in $PATH
info      vlt-command                 vlt not found in $PATH
info      secret-command              not set
twpayne commented 7 months ago

Thanks for spotting this. The documentation is indeed out of date. #3573 fixes it.

tl;dr you can either use a VIM plugin or set edit.hardlink to false.

terminalmage commented 7 months ago

Thanks! I prefer to keep plugins to a minimum, so I chose to disable hardlinking. However, I noticed a separate issue upon doing so, which I have reported in https://github.com/twpayne/chezmoi/issues/3574.