{"record":{"id":"a8efc5f5f1c3d479","repo":"twpayne/chezmoi","slug":"s-w-a8efc5","errorCode":null,"errorMessage":"%s: %w","messagePattern":"%s: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/chezmoi/targetstateentry.go","lineNumber":103,"sourceCode":"\tName  RelPath   `json:\"name\"  yaml:\"name\"`\n\tRunAt time.Time `json:\"runAt\" yaml:\"runAt\"`\n}\n\n// Apply updates actualStateEntry to match t.\nfunc (t *TargetStateModifyDirWithCmd) Apply(\n\tsystem System,\n\tpersistentState PersistentState,\n\tactualStateEntry ActualStateEntry,\n) (bool, error) {\n\tif _, ok := actualStateEntry.(*ActualStateDir); !ok {\n\t\tif err := actualStateEntry.Remove(system); err != nil {\n\t\t\treturn false, err\n\t\t}\n\t}\n\n\trunAt := time.Now().UTC()\n\tif err := system.RunCmd(t.cmdFunc()); err != nil {\n\t\treturn false, fmt.Errorf(\"%s: %w\", actualStateEntry.Path(), err)\n\t}\n\n\tmodifyDirWithCmdStateKey := []byte(actualStateEntry.Path().String())\n\tif err := PersistentStateSet(\n\t\tpersistentState, GitRepoExternalStateBucket, modifyDirWithCmdStateKey, &ModifyDirWithCmdState{\n\t\t\tName:  actualStateEntry.Path(),\n\t\t\tRunAt: runAt,\n\t\t},\n\t); err != nil {\n\t\treturn false, err\n\t}\n\n\treturn true, nil\n}\n\n// EntryState returns t's entry state.\nfunc (t *TargetStateModifyDirWithCmd) EntryState(umask fs.FileMode) (*EntryState, error) {\n\treturn &EntryState{","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/twpayne/chezmoi/blob/f901167e4685db90da56d6a2a19df642cb3e0247/internal/chezmoi/targetstateentry.go#L85-L121","documentation":"When applying a modify-script (ModifyDirWithCmd / script that mutates a directory), chezmoi runs the command via system.RunCmd and wraps any failure as '<path>: <underlying error>'. The wrapped error comes from the command's non-zero exit or execution failure.","triggerScenarios":"A modify_ script (runCmd on a directory-type target, e.g. git repo or managed-by-tool dir) exits non-zero or cannot be executed during `chezmoi apply`.","commonSituations":"Modify script depends on a tool not installed on the new machine; script fails due to network/auth when refreshing external content; script not executable or has a bug that only manifests under apply.","solutions":["Read the wrapped underlying error and fix the modify script itself (run it manually to reproduce)","Ensure the script is executable and its interpreter exists on the target machine","Make the script idempotent and tolerant of missing tooling/network (check command availability first)"],"exampleFix":"// before (modify script)\ngit -C $CHEZMOI_WORKING_TREE pull --rebase\n// after\n#!/bin/sh\ncommand -v git >/dev/null || exit 0\ngit -C \"$CHEZMOI_WORKING_TREE\" pull --rebase || true","handlingStrategy":"try-catch","validationCode":"// pre-flight: run the modify command yourself before apply\ncmd := exec.Command(\"sh\", \"-c\", scriptContents)\nif out, err := cmd.CombinedOutput(); err != nil {\n  return fmt.Errorf(\"modify script would fail: %v: %s\", err, out)\n}","typeGuard":null,"tryCatchPattern":"if err := t.Apply(system, persistentState); err != nil {\n  var wrapped = err // formatted as \"<path>: <cause>\"\n  log.Printf(\"apply failed on %s: %v — run the modify script manually to debug\", path, wrapped)\n  return wrapped\n}","preventionTips":["Make modify scripts idempotent and non-fatal where safe","Guard on tool availability (command -v) inside modify scripts","Test modify scripts on a clean machine to catch missing dependencies","Ensure scripts are executable and use absolute interpreter paths"],"tags":["chezmoi","script","apply"],"backgroundTag":"modify-script-failed","analyzedSha":"f901167e4685db90da56d6a2a19df642cb3e0247","analyzedAt":"2026-09-01T18:16:41.508Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T01:17:15.007Z"}