{"record":{"id":"2ebf807824664b3e","repo":"gohugoio/hugo","slug":"failed-to-decode-hugo-config-in-template-w","errorCode":null,"errorMessage":"failed to decode $_hugo_config in template: %w","messagePattern":"failed to decode \\$_hugo_config in template: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/tplimpl/templatetransform.go","lineNumber":511,"sourceCode":"\t}\n\n\tv := n.Decl[0]\n\n\tif len(v.Ident) == 0 || v.Ident[0] != \"$_hugo_config\" {\n\t\treturn\n\t}\n\n\tcmd := n.Cmds[0]\n\n\tif len(cmd.Args) == 0 {\n\t\treturn\n\t}\n\n\tif s, ok := cmd.Args[0].(*parse.StringNode); ok {\n\t\terrMsg := \"failed to decode $_hugo_config in template: %w\"\n\t\tm, err := hmaps.ToStringMapE(s.Text)\n\t\tif err != nil {\n\t\t\tc.err = fmt.Errorf(errMsg, err)\n\t\t\treturn\n\t\t}\n\t\tif err := mapstructure.WeakDecode(m, &c.t.ParseInfo.Config); err != nil {\n\t\t\tc.err = fmt.Errorf(errMsg, err)\n\t\t}\n\t}\n}\n\n// collectInnerInShortcode determines if the given CommandNode represents a\n// shortcode call to its .Inner.\nfunc (c *templateTransformContext) collectInnerInShortcode(n *parse.CommandNode) {\n\tif c.t.category != CategoryShortcode {\n\t\treturn\n\t}\n\tif c.t.ParseInfo.IsInner || len(n.Args) == 0 {\n\t\treturn\n\t}\n","sourceCodeStart":493,"sourceCodeEnd":529,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/tplimpl/templatetransform.go#L493-L529","documentation":"Hugo templates may declare a config via {{ $_hugo_config := `{...}` }}. The transformer (templatetransform.go:507-512) first converts the string node's text to a map via hmaps.ToStringMapE. If the JSON-ish string cannot be turned into a string-keyed map, this error wraps the conversion failure. This is the first of two decode failure points for $_hugo_config.","triggerScenarios":"A template declares {{ $_hugo_config := \"...\" }} but the string is not valid JSON or does not decode into a map[string]any (e.g. a bare string, a JSON array, malformed JSON). ToStringMapE rejects non-map results.","commonSituations":"Hand-writing a $_hugo_config with a syntax error; missing quotes; providing a scalar instead of a JSON object; copy-paste errors from documentation.","solutions":["Validate the $_hugo_config JSON with a linter; ensure it is a JSON object.","Check for unescaped quotes or trailing commas in the config string.","Use the documented schema (e.g. {\"version\": 1})."],"exampleFix":"// before\n{{ $_hugo_config := `{version: 1,}` }}\n\n// after — valid JSON object\n{{ $_hugo_config := `{\"version\": 1}` }}","handlingStrategy":"validation","validationCode":"// Validate the $_hugo_config JSON before relying on it. It must be a JSON object:\n{{ $_hugo_config := `{\"version\": 1}` }}\n// Avoid arrays, scalars, or malformed JSON.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use a JSON linter on $_hugo_config values.","Ensure the value is a JSON object (map), not an array or scalar.","Escape inner quotes correctly."],"tags":["template","config","hugo-config","json","parse-error","build"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}