{"record":{"id":"369bc58273f1bac4","repo":"gohugoio/hugo","slug":"merge-requires-at-least-two-parameters","errorCode":null,"errorMessage":"merge requires at least two parameters","messagePattern":"merge requires at least two parameters","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/merge.go","lineNumber":32,"sourceCode":"package collections\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"reflect\"\n\t\"strings\"\n\n\t\"github.com/gohugoio/hugo/common/hmaps\"\n\t\"github.com/gohugoio/hugo/common/hreflect\"\n)\n\n// Merge creates a copy of the final parameter in params and merges the preceding\n// parameters into it in reverse order.\n//\n// Currently only maps are supported. Key handling is case insensitive.\nfunc (ns *Namespace) Merge(params ...any) (any, error) {\n\tif len(params) < 2 {\n\t\treturn nil, errors.New(\"merge requires at least two parameters\")\n\t}\n\n\tvar err error\n\tresult := params[len(params)-1]\n\n\tfor i := len(params) - 2; i >= 0; i-- {\n\t\tresult, err = ns.merge(params[i], result)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t}\n\n\treturn result, nil\n}\n\n// merge creates a copy of dst and merges src into it.\nfunc (ns *Namespace) merge(src, dst any) (any, error) {\n\tvdst, vsrc := reflect.ValueOf(dst), reflect.ValueOf(src)","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/collections/merge.go#L14-L50","documentation":"The `merge` template function combines two or more maps into one, copying the last parameter and merging preceding maps into it in reverse order. This error is thrown when fewer than two parameters are supplied, because merging is undefined without at least a source and a destination. Only maps are supported and key handling is case insensitive.","triggerScenarios":"Calling `collections.Merge` with zero arguments or a single argument, e.g. `{{ merge .Params }}` or `{{ merge }}`. The check at tpl/collections/merge.go:31 fails immediately when `len(params) < 2`.","commonSituations":"A developer writes `{{ merge .Params }}` intending to merge a page's Params with a default dict but forgets the second map. Also occurs when a template variable holding the second map is nil/unset and variadic expansion yields a single arg, or when copy-pasting from a single-arg `dict` call.","solutions":["Add at least a second map argument, e.g. `{{ merge $defaults .Params }}` where the last parameter is the destination.","If a map may be nil, default it: `{{ $defaults = $defaults | default dict }}` before calling merge.","Verify that each argument resolves to a `map[string]any` or `hmaps.Params` before the call."],"exampleFix":"// before\n{{ merge .Params }}\n// after\n{{ merge $defaults .Params }}","handlingStrategy":"validation","validationCode":"{{ $maps := slice $a $b }}\n{{ $allMaps := true }}\n{{ range $maps }}{{ if not (reflectIsMap .) }}{{ $allMaps = false }}{{ end }}{{ end }}\n{{ if and (ge (len $maps) 2) $allMaps }}\n  {{ merge $a $b }}\n{{ end }}","typeGuard":"// In Go: ensure at least two map[string]any args before calling\nfunc isMergeableMaps(params ...any) bool {\n    if len(params) < 2 { return false }\n    for _, p := range params {\n        if reflect.TypeOf(p).Kind() != reflect.Map { return false }\n    }\n    return true\n}","tryCatchPattern":null,"preventionTips":["Always provide a source and destination map to merge.","Default optional maps with `| default dict` before merging.","Confirm each argument is a map, not a slice or scalar."],"tags":["hugo","template","collections","merge","variadic"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}