{"record":{"id":"4fa2dcbd99298cc4","repo":"gohugoio/hugo","slug":"defer-does-not-take-any-arguments","errorCode":null,"errorMessage":"Defer does not take any arguments","messagePattern":"Defer does not take any arguments","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/templates/templates.go","lineNumber":69,"sourceCode":"func (ns *Namespace) Exists(name string) bool {\n\treturn ns.deps.GetTemplateStore().HasTemplate(name)\n}\n\n// Defer defers the execution of a template block.\nfunc (ns *Namespace) Defer(ctx context.Context, args ...any) (bool, error) {\n\t// Prevent defer from being used in content adapters,\n\t// that just doesn't work.\n\tns.deps.Site.CheckReady()\n\n\t// Reject use inside a partialCached body: the cached output retains the\n\t// deferred placeholder across rebuilds while the executions map is reset,\n\t// which used to cause a panic in executeDeferredTemplates. See issue #13492.\n\tif tpl.Context.IsInPartialCached.Get(ctx) {\n\t\treturn false, fmt.Errorf(\"templates.Defer cannot be used inside a partialCached partial; use partial instead, or move templates.Defer to the calling template\")\n\t}\n\n\tif len(args) != 0 {\n\t\treturn false, fmt.Errorf(\"Defer does not take any arguments\")\n\t}\n\treturn true, nil\n}\n\nvar defferedIDCounter atomic.Uint64\n\ntype DeferOpts struct {\n\t// Optional cache key. If set, the deferred block will be executed\n\t// once per unique key.\n\tKey string\n\n\t// Optional data context to use when executing the deferred block.\n\tData any\n}\n\n// Inner executes the inner content of a partial decorator.\n// Note that there is only one inner block per partial decorator, but inner may be called multiple times with, typically, different data.\nfunc (ns *Namespace) Inner(ctx context.Context, data any) (any, error) {","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/templates/templates.go#L51-L87","documentation":"Thrown by templates.Defer when len(args) != 0. The Defer function takes no positional arguments; configuration is done via the block's own (…) syntax with DeferOpts fields, not function arguments.","triggerScenarios":"Calling {{ templates.Defer \"key\" }} or passing any positional argument inside the function parentheses: templates.Defer $key $data. The function signature is Defer(ctx, args ...any) and rejects any args.","commonSituations":"Confusing templates.Defer's function args with the block's DeferOpts. Users coming from resources.PostProcess or other functions try to pass a key or data as a positional arg.","solutions":["Use the block syntax for options: {{ templates.Defer (key=\"mykey\" data=.) }} … {{ end }}.","Remove any positional arguments from the templates.Defer call.","Confirm you are using the block form {{ templates.Defer }}…{{ end }} and not a function call form."],"exampleFix":"// before (positional args):\n{{ templates.Defer \"mykey\" }}\n// after (no function args; use block opts):\n{{ templates.Defer (key=\"mykey\") }}\n  ...content...\n{{ end }}","handlingStrategy":"validation","validationCode":"{{ templates.Defer }}…{{ end }}\n// or with block opts only:\n{{ templates.Defer (key=\"k\") }}…{{ end }}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass no positional arguments to templates.Defer.","Use the block (…) syntax for Key/Data options.","Remember Defer is a block construct, not a function returning a value."],"tags":["templates","defer","argument-count","template"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}