Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Set CHEZMOI_ environment variables for modify_ scripts #2935

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/chezmoi/sourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ type SourceState struct {
readTemplateData bool
userTemplateData map[string]any
priorityTemplateData map[string]any
scriptEnv []string
templateData map[string]any
templateFuncs template.FuncMap
templateOptions []string
Expand Down Expand Up @@ -199,6 +200,13 @@ func WithReadTemplateData(readTemplateData bool) SourceStateOption {
}
}

// WithScriptEnv sets the script environment variables.
func WithScriptEnv(scriptEnv []string) SourceStateOption {
return func(s *SourceState) {
s.scriptEnv = scriptEnv
}
}

// WithSourceDir sets the source directory.
func WithSourceDir(sourceDirAbsPath AbsPath) SourceStateOption {
return func(s *SourceState) {
Expand Down Expand Up @@ -1673,6 +1681,7 @@ func (s *SourceState) newModifyTargetStateEntryFunc(

// Run the modifier on the current contents.
cmd := interpreter.ExecCommand(tempFile.Name())
cmd.Env = s.scriptEnv
cmd.Stdin = bytes.NewReader(currentContents)
cmd.Stderr = os.Stderr
contents, err = chezmoilog.LogCmdOutput(cmd)
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,7 @@ func (c *Config) newSourceState(
chezmoi.WithLogger(&sourceStateLogger),
chezmoi.WithMode(c.Mode),
chezmoi.WithPriorityTemplateData(c.Data),
chezmoi.WithScriptEnv(c.runEnv),
chezmoi.WithSourceDir(c.SourceDirAbsPath),
chezmoi.WithSystem(c.sourceSystem),
chezmoi.WithTemplateFuncs(c.templateFuncs),
Expand Down
10 changes: 10 additions & 0 deletions pkg/cmd/testdata/scripts/issue2934.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[windows] skip

# test that chezmoi sets environment variables for modify_ scripts
exec chezmoi apply
grep ^${CHEZMOISOURCEDIR@R}$ $HOME/.modify

-- home/user/.local/share/chezmoi/modify_dot_modify --
#!/bin/sh

echo ${CHEZMOI_SOURCE_DIR}