Skip to content

Commit

Permalink
fix: Avoid concurrent map access when evaluating multiple templates
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Aug 6, 2023
1 parent 10bd1ca commit 4631ac4
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions internal/chezmoi/sourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"time"

"github.com/coreos/go-semver/semver"
"github.com/mitchellh/copystructure"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
vfs "github.com/twpayne/go-vfs/v4"
Expand Down Expand Up @@ -764,11 +765,10 @@ func (s *SourceState) ExecuteTemplateData(options ExecuteTemplateDataOptions) ([
}
}

// Temporarily set .chezmoi.sourceFile to the name of the template.
// Set .chezmoi.sourceFile to the name of the template.
templateData := s.TemplateData()
if chezmoiTemplateData, ok := templateData["chezmoi"].(map[string]any); ok {
chezmoiTemplateData["sourceFile"] = options.Name
defer delete(chezmoiTemplateData, "sourceFile")
}

return tmpl.Execute(templateData)
Expand Down Expand Up @@ -1278,7 +1278,7 @@ func (s *SourceState) TargetRelPaths() []RelPath {
return targetRelPaths
}

// TemplateData returns s's template data.
// TemplateData returns a copy of s's template data.
func (s *SourceState) TemplateData() map[string]any {
if s.templateData == nil {
s.templateData = make(map[string]any)
Expand All @@ -1289,7 +1289,11 @@ func (s *SourceState) TemplateData() map[string]any {
RecursiveMerge(s.templateData, s.userTemplateData)
RecursiveMerge(s.templateData, s.priorityTemplateData)
}
return s.templateData
templateData, err := copystructure.Copy(s.templateData)
if err != nil {
panic(err)
}
return templateData.(map[string]any)
}

// addExternal adds external source entries to s.
Expand Down Expand Up @@ -1857,10 +1861,6 @@ func (s *SourceState) newModifyTargetStateEntryFunc(
if chezmoiTemplateData, ok := templateData["chezmoi"].(map[string]any); ok {
chezmoiTemplateData["stdin"] = string(currentContents)
chezmoiTemplateData["sourceFile"] = sourceFile
defer func() {
delete(chezmoiTemplateData, "stdin")
delete(chezmoiTemplateData, "sourceFile")
}()
}

contents, err = tmpl.Execute(templateData)
Expand Down

0 comments on commit 4631ac4

Please sign in to comment.