{"record":{"id":"bea81f40e269fbbc","repo":"gohugoio/hugo","slug":"invalid-number-of-arguments-to-ref","errorCode":null,"errorMessage":"invalid number of arguments to ref","messagePattern":"invalid number of arguments to ref","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/urls/urls.go","lineNumber":143,"sourceCode":"\t)\n\n\tv := args\n\tif _, ok := v.([]any); ok {\n\t\tv = cast.ToStringSlice(v)\n\t}\n\n\tswitch v := v.(type) {\n\tcase map[string]any:\n\t\treturn v, nil\n\tcase map[string]string:\n\t\tm := make(map[string]any)\n\t\tfor k, v := range v {\n\t\t\tm[k] = v\n\t\t}\n\t\treturn m, nil\n\tcase []string:\n\t\tif len(v) == 0 || len(v) > 2 {\n\t\t\treturn nil, fmt.Errorf(\"invalid number of arguments to ref\")\n\t\t}\n\t\t// These were the options before we introduced the map type:\n\t\ts = v[0]\n\t\tif len(v) == 2 {\n\t\t\tof = v[1]\n\t\t}\n\tdefault:\n\t\tvar err error\n\t\ts, err = cast.ToStringE(args)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t}\n\n\treturn map[string]any{\n\t\t\"path\":         s,\n\t\t\"outputFormat\": of,","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/urls/urls.go#L125-L161","documentation":"Thrown by refArgsToMap (tpl/urls/urls.go:143) when the arguments to ref/relref arrive as a []string slice whose length is 0 or greater than 2. ref accepts a path plus an optional outputFormat, so a slice must have 1 or 2 elements.","triggerScenarios":"Calling {{ ref . (slice) }} with an empty slice, or {{ ref . (slice \"a\" \"b\" \"c\") }} with three elements; passing a dynamically-built collection that yields too many items.","commonSituations":"Building the argument list programmatically and passing an empty collection; misunderstanding that ref takes at most path+outputFormat.","solutions":["Pass exactly one path string, or a slice of [path] or [path, outputFormat]","Use the map form for clarity: {{ ref . (dict \"path\" \"foo\" \"outputFormat\" \"amp\") }}","Validate the slice length before calling ref/relref"],"exampleFix":"// before\n{{ ref . (slice \"a.md\" \"amp\" \"extra\") }}\n// after\n{{ ref . (dict \"path\" \"a.md\" \"outputFormat\" \"amp\") }}","handlingStrategy":"validation","validationCode":"if s, ok := args.([]string); ok && (len(s) == 0 || len(s) > 2) {\n    return nil, fmt.Errorf(\"ref needs 1 or 2 string args, got %d\", len(s))\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass a map (dict \"path\" ... \"outputFormat\" ...) for clarity","Limit slice args to [path] or [path, outputFormat]","Validate dynamic argument collections before calling ref/relref"],"tags":["template","urls","ref","relref"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}