gohugoio/hugo · error
must provide targetPath, the template data context and a Res
Error message
must provide targetPath, the template data context and a Resource object
What it means
`resources.ExecuteAsTemplate` takes exactly three arguments: the target path, the data context to execute the template with, and the Resource whose content is the template. Any other argument count fails immediately. (A wrong type in the third position produces a separate "type %T not supported" error.)
Source
Thrown at tpl/resources/resources.go:243
// FromString creates a Resource from a string published to the relative target path.
func (ns *Namespace) FromString(targetPathIn, contentIn any) (resource.Resource, error) {
targetPath, err := cast.ToStringE(targetPathIn)
if err != nil {
return nil, err
}
content, err := cast.ToStringE(contentIn)
if err != nil {
return nil, err
}
return ns.createClient.FromString(targetPath, content)
}
// ExecuteAsTemplate creates a Resource from a Go template, parsed and executed with
// the given data, and published to the relative target path.
func (ns *Namespace) ExecuteAsTemplate(ctx context.Context, args ...any) (resource.Resource, error) {
if len(args) != 3 {
return nil, fmt.Errorf("must provide targetPath, the template data context and a Resource object")
}
targetPath, err := cast.ToStringE(args[0])
if err != nil {
return nil, err
}
data := args[1]
r, ok := args[2].(resources.ResourceTransformer)
if !ok {
return nil, fmt.Errorf("type %T not supported in Resource transformations", args[2])
}
return ns.templatesClient.ExecuteAsTemplate(ctx, r, targetPath, data)
}
// Fingerprint transforms the given Resource with a MD5 hash of the content in
// the RelPermalink and Permalink.
func (ns *Namespace) Fingerprint(args ...any) (resource.Resource, error) {View on GitHub ↗ (pinned to 8a468df065)
Solutions
- Call with all three: `{{ $css := resources.Get "css/main.tpl.css" | resources.ExecuteAsTemplate "css/main.css" . }}` — the pipe supplies the resource as the third argument.
- Check the order: targetPath first, data second, resource last.
- Pass `.` (or site) as the data context even if the template body doesn't use it.
Example fix
<!-- before -->
{{ $css := resources.ExecuteAsTemplate "css/main.css" $tpl }}
<!-- after -->
{{ $css := resources.ExecuteAsTemplate "css/main.css" . $tpl }} Defensive patterns
Strategy: validation
Validate before calling
{{ $tpl := resources.Get "template.tmpl" }}{{ if $tpl }}{{ $out := resources.ExecuteAsTemplate "out.css" . $tpl }}{{ end }} Type guard
{{ $ok := and (eq (printf "%T" $target) "string") $resource }} Try / catch
{{ with try (resources.ExecuteAsTemplate $target $ctx $res) }}{{ with .Err }}{{ errorf "ExecuteAsTemplate failed: %s" . }}{{ end }}{{ end }} Prevention
- Always pass exactly three args: targetPath string, data context, then the Resource
- Verify resources.Get didn't return nil before passing it as the third argument
- Keep the argument order straight — the Resource comes last
When it happens
Trigger: `{{ resources.ExecuteAsTemplate "style.css" $resource }}` — omitting the data context; or passing extra arguments. The correct order is targetPath, data, resource — swapping data and resource also commonly leads here or to the type error.
Common situations: Forgetting to pass `.` as the data context; following outdated snippets with a different argument order; piping the resource in a way that drops an argument (`$r | resources.ExecuteAsTemplate "out.css"` only supplies two).
Related errors
- must provide an URL and optionally an options map
- must provide a Resource object
- must not provide more arguments than Resource and hash algor
- failed to copy resources to vendor dir: %w
- need at least 2 arguments to append
AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31).
Data as JSON: /data/errors/f668d11629ece2ab.json.
Report an issue: GitHub ↗.