twpayne / chezmoi

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

Newlines in template symlinks #604

Closed renemarc closed 4 years ago

renemarc commented 4 years ago

Describe the bug

When using a symlink_xxxx.tmpl file to auto-create a symbolic link, any newline characters are included in the resulting target name. The link then ends up pointing to a non-existing file. Since many GUI code editors automatically add an empty line at the end of the file upon saving, the name of the resulting target may end with an unwelcome \n.

A template-based workaround is to include a space-trimming comment at the end: {{- /* eof */ -}}

To reproduce

  1. Add regular file to chezmoi root directory: touch iterm.plist
  2. Exclude file from being copied over to the user home directory: echo iterm.plist >> .chezmoiignore
  3. Create a template symlink pointing to .example, including a trailing newline: touch symlink_iterm.plist.tmpl
  4. Edit the template to point to the regular file, but include an empty line at the end:

    {{ .chezmoi.sourceDir }}/iterm.plist
    
  5. Perform chezmoi apply.

The resulting link is:

iterm.plist@ -> /Users/renemarc/.local/share/chezmoi/iterm.plist\n

Expected behavior

Ideally, the template results should be trimmed on both ends to remove any heading and trailing spaces.

iterm.plist@ -> /Users/renemarc/.local/share/chezmoi/iterm.plist

Output of command with --verbose

$ chezmoi command --verbose
ln -sf '/Users/renemarc/.local/share/chezmoi/iterm.plist
' /Users/renemarc/iterm.plist

Output of chezmoi doctor

$ chezmoi doctor
     ok: version 1.7.13, commit ed3584c00b85ef96b0314824c74c335f138a459b, built at 2020-02-13T01:36:27Z
     ok: runtime.GOOS darwin, runtime.GOARCH amd64
     ok: /Users/renemarc/.local/share/chezmoi (source directory, perm 700)
     ok: /Users/renemarc (destination directory, perm 700)
     ok: /Users/renemarc/.config/chezmoi/chezmoi.toml (configuration file)
     ok: /bin/bash (shell)
     ok: /usr/local/bin/mvim (editor)
     ok: /usr/local/bin/vimdiff (merge command)
     ok: /usr/bin/git (source VCS command, version 2.21.1)
warning: gpg (GnuPG, not found)
warning: op (1Password CLI, not found)
warning: bw (Bitwarden CLI, not found)
warning: gopass (gopass CLI, not found)
warning: keepassxc-cli (KeePassXC CLI, not found)
warning: lpass (LastPass CLI, not found)
warning: pass (pass CLI, not found)
warning: vault (Vault CLI, not found)

Additional context

N/A.

twpayne commented 4 years ago

Thank you for the very clear and complete report!

I agree that chezmoi's current behavior here is undesirable. In practice, symlink targets rarely should contain either leading or trailing whitespace, so it makes sense to unconditionally strip any.

605 implements this.