{"record":{"id":"9f8a29c648b14096","repo":"gohugoio/hugo","slug":"strings-negative-repeat-count","errorCode":null,"errorMessage":"strings: negative Repeat count","messagePattern":"strings: negative Repeat count","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/strings/strings.go","lineNumber":602,"sourceCode":"\t}\n\n\treturn strings.TrimSuffix(ss, sx), nil\n}\n\n// Repeat returns a new string consisting of n copies of the string s.\nfunc (ns *Namespace) Repeat(n, s any) (string, error) {\n\tss, err := cast.ToStringE(s)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\tsn, err := cast.ToIntE(n)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\tif sn < 0 {\n\t\treturn \"\", errors.New(\"strings: negative Repeat count\")\n\t}\n\n\treturn strings.Repeat(ss, sn), nil\n}\n","sourceCodeStart":584,"sourceCodeEnd":607,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/strings/strings.go#L584-L607","documentation":"Thrown by strings.Repeat when the count argument n is negative (tpl/strings/strings.go:601-602). Repeat returns n copies of a string; Go's strings.Repeat panics on negative counts, so Hugo guards explicitly and returns this error instead of crashing the build.","triggerScenarios":"Calling {{ repeat -1 \"ab\" }} or {{ \"ab\" | repeat (sub 0 5) }} where n evaluates to a negative integer.","commonSituations":"Computing a repeat count from an expression that can go negative (e.g. padding math); passing a negated variable; defaulting an unset param to -1.","solutions":["Clamp the count to a minimum of zero: {{ repeat (math.Max 0 .N) \"ab\" }}.","Guard with an if: {{ if ge .N 0 }}{{ repeat .N \"ab\" }}{{ end }}.","Fix upstream logic so the count cannot be negative."],"exampleFix":"// before\n{{ repeat .N \"ab\" }}\n// after\n{{ if ge .N 0 }}{{ repeat .N \"ab\" }}{{ end }}","handlingStrategy":"validation","validationCode":"{{ $n := math.Max 0 .N }}\n{{ repeat $n \"ab\" }}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp repeat counts to >= 0 before calling.","Guard computed counts with {{ if ge .N 0 }}."],"tags":["template-function","strings","repeat","math","validation"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}