twpayne / chezmoi

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

execute-template: joinPath does not work with .chezmoi.sourceDir #1105

Closed MunifTanjim closed 3 years ago

MunifTanjim commented 3 years ago

Describe the bug

joinPath does not work with .chezmoi.sourceDir

To reproduce

$ echo '{{ joinPath .chezmoi.sourceDir "example" }}' | chezmoi execute-template
chezmoi: template: stdin:1:20: executing "stdin" at <.chezmoi.sourceDir>: wrong type for value; expected string; got chezmoi.AbsPath

Expected behavior

Work similar to this:

$ echo '{{ joinPath .chezmoi.homeDir "example" }}' | chezmoi execute-template
/Users/shaikat/example

Output of command with --verbose

$ echo '{{ joinPath .chezmoi.sourceDir "example" }}' | chezmoi execute-template --verbose
chezmoi: template: stdin:1:20: executing "stdin" at <.chezmoi.sourceDir>: wrong type for value; expected string; got chezmoi.AbsPath

Output of chezmoi doctor

$ chezmoi doctor
RESULT   CHECK                MESSAGE
ok       version              v2.0.4, commit 9cc8184cc466a87c44006218e02365046e5d5f8b, built at 2021-03-24T18:55:30Z, built by goreleaser
ok       os-arch              darwin/amd64
info     config-file          /Users/shaikat/.config/chezmoi/chezmoi.toml does not exist
ok       source-dir           /Users/shaikat/.local/share/chezmoi is a directory
ok       suspicious-entries   no suspicious entries found in /Users/shaikat/.local/share/chezmoi
ok       dest-dir             /Users/shaikat is a directory
ok       shell                found /usr/local/bin/zsh
ok       editor               found /usr/local/bin/nvim
ok       git-cli              found /usr/local/bin/git, version 2.31.0
ok       merge-cli            found /usr/bin/vimdiff
info     age-cli              age not found in $PATH
ok       gnupg-cli            found /usr/local/bin/gpg, version 2.2.27
info     1password-cli        op not found in $PATH
ok       bitwarden-cli        found /usr/local/bin/bw, version 1.15.1
info     gopass-cli           gopass not found in $PATH
info     keepassxc-cli        keepassxc-cli not found in $PATH
info     keepassxc-db         not set
info     lastpass-cli         lpass not found in $PATH
info     pass-cli             pass not found in $PATH
info     vault-cli            vault not found in $PATH
info     secret-cli           not set

Additional context

N/A

zb140 commented 3 years ago

joinPath might need some updating to be able to take either strings or AbsPath or RelPath. In the meantime, you can work around this by doing something to cause .chezmoi.sourceDir to get converted to a string. One easy way to do that is with the cat function from Sprig:

$  echo '{{ joinPath (cat .chezmoi.sourceDir) "example" }}' | chezmoi execute-template
/home/zb/.local/share/chezmoi/example

(Incidentally, I'm not sure why cat can coerce .chezmoi.sourceDir into a string but joinPath can't. That might be an interesting avenue of investigation. Edit: ah, it's because cat takes interface{}s rather than strings. For joinPath it's probably best to limit it to strings or *Path types if possible.)

zb140 commented 3 years ago

Oops, hit the wrong button :smile:

In the interest of adding something new to the discussion, you can also use the built-in printf if you find that seems easier to reason about. For example:

$  echo '{{ joinPath (printf "%v" .chezmoi.sourceDir) "example" }}' | chezmoi execute-template
/home/zb/.local/share/chezmoi/example
twpayne commented 3 years ago

Many thanks for reporting this @MunifTanjim and for the quick work-around @zb140 :)