{"record":{"id":"b2ebbdf4fb3ae33e","repo":"twpayne/chezmoi","slug":"d-invalid-format-indent-width","errorCode":null,"errorMessage":"%d: invalid format-indent-width","messagePattern":"(.+?): invalid format-indent-width","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/chezmoi/template.go","lineNumber":176,"sourceCode":"\t\t\t\tcase \"utf-16-be\":\n\t\t\t\t\to.Encoding = unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)\n\t\t\t\tcase \"utf-16-be-bom\":\n\t\t\t\t\to.Encoding = unicode.UTF16(unicode.BigEndian, unicode.UseBOM)\n\t\t\t\tcase \"utf-16-le\":\n\t\t\t\t\to.Encoding = unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)\n\t\t\t\tcase \"utf-16-le-bom\":\n\t\t\t\t\to.Encoding = unicode.UTF16(unicode.LittleEndian, unicode.UseBOM)\n\t\t\t\tdefault:\n\t\t\t\t\treturn nil, fmt.Errorf(\"%s: unknown encoding\", value)\n\t\t\t\t}\n\t\t\tcase \"format-indent\":\n\t\t\t\to.FormatIndent = value\n\t\t\tcase \"format-indent-width\":\n\t\t\t\tswitch width, err := strconv.Atoi(value); {\n\t\t\t\tcase err != nil:\n\t\t\t\t\treturn nil, err\n\t\t\t\tcase width < 0:\n\t\t\t\t\treturn nil, fmt.Errorf(\"%d: invalid format-indent-width\", width)\n\t\t\t\tdefault:\n\t\t\t\t\to.FormatIndent = strings.Repeat(\" \", width)\n\t\t\t\t}\n\t\t\tcase \"left-delimiter\":\n\t\t\t\to.LeftDelimiter = value\n\t\t\tcase \"line-ending\", \"line-endings\":\n\t\t\t\tswitch string(keyValuePairMatch[2]) {\n\t\t\t\tcase \"crlf\":\n\t\t\t\t\to.LineEnding = \"\\r\\n\"\n\t\t\t\tcase \"lf\":\n\t\t\t\t\to.LineEnding = \"\\n\"\n\t\t\t\tcase \"native\":\n\t\t\t\t\to.LineEnding = nativeLineEnding\n\t\t\t\tdefault:\n\t\t\t\t\to.LineEnding = value\n\t\t\t\t}\n\t\t\tcase \"right-delimiter\":\n\t\t\t\to.RightDelimiter = value","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/twpayne/chezmoi/blob/f901167e4685db90da56d6a2a19df642cb3e0247/internal/chezmoi/template.go#L158-L194","documentation":"chezmoi's template engine accepts a `format-indent-width` template directive that controls the indentation string used when re-formatting template output (e.g. JSON). The directive's value is parsed with strconv.Atoi and must be a non-negative integer; if the parsed number is negative, parseAndRemoveDirectives returns this error and template parsing fails. It is thrown to reject nonsensical indentation values at parse time rather than producing misformatted output.","triggerScenarios":"Calling chezmoi.ParseTemplate (or executing a template via parseAndRemoveDirectives) with a directive `format-indent-width` whose value parses to a negative integer, e.g. `{{ template \"x\" format-indent-width=-1 }}`-style option parsing where value is \"-1\", \"-4\", etc.","commonSituations":"Users hand-editing .chezmoitemplates or config template options and typo a minus sign; programmatically generating template options from config where a default of -1 means 'unset' and is passed through; copy-pasting examples that use negative sentinel values.","solutions":["Change the directive value to a non-negative integer, e.g. format-indent-width=2","Check the template/config source for an accidental leading minus sign or a negative default leaking in","If the value comes from code, clamp or validate it before building the option string","If indentation should be disabled, use format-indent=false or a different directive instead of a negative width"],"exampleFix":"// before\noptions = append(options, \"format-indent-width=-1\")\n// after\nif width >= 0 {\n\toptions = append(options, fmt.Sprintf(\"format-indent-width=%d\", width))\n}","handlingStrategy":"validation","validationCode":"func validIndentWidth(v string) bool {\n\tn, err := strconv.Atoi(v)\n\treturn err == nil && n >= 0\n}\n// only add \"format-indent-width=N\" when validIndentWidth(N-string)","typeGuard":null,"tryCatchPattern":"tpl, err := chezmoi.ParseTemplate(name, data, options)\nif err != nil {\n\tvar nf *strconv.NumError\n\tif strings.Contains(err.Error(), \"invalid format-indent-width\") || errors.As(err, &nf) {\n\t\t// fall back to default options without the directive\n\t}\n\treturn err\n}","preventionTips":["Validate integer option values (>= 0) before adding them to template options","Use named constants/enums for indentation instead of raw ints that may hold negative sentinels","Add a unit test that parses every generated directive combination","Prefer format-indent-width=0 over negative values when indentation should collapse"],"tags":["template","config","validation","chezmoi"],"backgroundTag":"invalid-template-directive-value","analyzedSha":"f901167e4685db90da56d6a2a19df642cb3e0247","analyzedAt":"2026-09-01T18:16:41.508Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T01:17:15.007Z"}