diff --git a/internal/cmd/config.go b/internal/cmd/config.go index 1cb2ce08ed6..606cf611c90 100644 --- a/internal/cmd/config.go +++ b/internal/cmd/config.go @@ -2783,8 +2783,16 @@ func (f *ConfigFile) toMap() map[string]any { configFile.Diff.Pager = "" } + // This is a horrible hack. We want the returned map to contain only simple + // types because they are used with masterminds/sprig template functions + // which don't accept fmt.Stringers in place of strings. As a work-around, + // round-trip via JSON. + data, err := json.Marshal(configFile) + if err != nil { + return nil + } var result map[string]any - if err := mapstructure.Decode(configFile, &result); err != nil { + if err := json.Unmarshal(data, &result); err != nil { panic(err) } return result diff --git a/internal/cmd/testdata/scripts/addautotemplate.txtar b/internal/cmd/testdata/scripts/addautotemplate.txtar index e7b477c42e8..24095e0a67c 100644 --- a/internal/cmd/testdata/scripts/addautotemplate.txtar +++ b/internal/cmd/testdata/scripts/addautotemplate.txtar @@ -8,15 +8,15 @@ exec chezmoi add --autotemplate $HOME${/}.symlink cmp $CHEZMOISOURCEDIR/symlink_dot_symlink.tmpl golden/symlink_dot_symlink.tmpl # test that chezmoi add --autotemplate does not create a template if no replacements occurred -exec chezmoi add --autotemplate $HOME${/}.file -cmp $CHEZMOISOURCEDIR/dot_file golden/dot_file +exec chezmoi add --autotemplate $HOME${/}.notatemplate +cmp $CHEZMOISOURCEDIR/dot_notatemplate golden/dot_notatemplate # test that chezmoi add --autotemplate escapes brackets exec chezmoi add --autotemplate $HOME${/}.vimrc cmp $CHEZMOISOURCEDIR/dot_vimrc.tmpl golden/dot_vimrc.tmpl --- golden/dot_file -- -# contents of .file +-- golden/dot_notatemplate -- +# contents of .notatemplate -- golden/dot_template.tmpl -- key = {{ .variable }} -- golden/dot_vimrc.tmpl -- @@ -26,8 +26,8 @@ set foldmarker={{ "{{" }},{{ "}}" }} -- home/user/.config/chezmoi/chezmoi.toml -- [data] variable = "value" --- home/user/.file -- -# contents of .file +-- home/user/.notatemplate -- +# contents of .notatemplate -- home/user/.template -- key = value -- home/user/.vimrc -- diff --git a/internal/cmd/testdata/scripts/issue3602.txtar b/internal/cmd/testdata/scripts/issue3602.txtar new file mode 100644 index 00000000000..1d1d7df77bd --- /dev/null +++ b/internal/cmd/testdata/scripts/issue3602.txtar @@ -0,0 +1,2 @@ +# test that the .chezmoi.config.destDir template variable is compatible with the replace template function +exec chezmoi execute-template '{{ .chezmoi.config.destDir | replace "abc" "def "}}'