twpayne / chezmoi

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

.chezmoi.cacheDir variable has mixed path separators on Windows #3473

Closed drwicid closed 8 months ago

drwicid commented 8 months ago

Describe the bug

On Windows, the .chezmoi.cacheDir variable uses mixed path separators. This may cause toml: non-hex character error if chezmoi reads the path string.

To reproduce

Quick repro of the mixed case:

> chezmoi execute-template "{{ .chezmoi.cacheDir }}"
C:\Users\drwic\.cache/chezmoi

To reproduce the error, create this config template:

encryption = "age"
[age]
   identity = "{{ .chezmoi.cacheDir }}/age.txt"

Execute chezmoi init

chezmoi: C:/Users/drwic/.local/share/chezmoi/home/.chezmoi.toml.tmpl: toml: non-hex character

Expected behavior

cacheDir should have consistent path separators.

Additional Info

Passing this config through execute-template:

encryption = "age"

[age]
   # Bug
   identity = "{{ .chezmoi.cacheDir }}/age.txt"

   # Workaround
   identity = "{{ .chezmoi.cacheDir | replaceAllRegex "\\\\" "/" }}/age.txt"

outputs:

encryption = "age"

[age]
   # Bug
   identity = "C:\Users\drwic\.cache/chezmoi/age.txt"

   # Workaround
   identity = "C:/Users/drwic/.cache/chezmoi/age.txt"
twpayne commented 8 months ago

Thanks for reporting this. It is fixed with #3475.

Note that a better work-around is to use the quote template function as this handles all characters that need to be escaped, not just backslashes.

[age]
    identity = {{ joinPath .chezmoi.cacheDir "age.txt" | quote }}
halostatue commented 8 months ago

You could also use | toToml instead of | quote.

twpayne commented 8 months ago

You could also use | toToml instead of | quote.

That's an even better solution, thank you.

drwicid commented 8 months ago

Thanks for the alternatives. I'm two weeks into my chezmoi noob journey and loving this tool. I'll take this as an assignment to RTFM and master all those functions ;-)