gohugoio/hugo · error
type %T is not a Resource
Error message
type %T is not a Resource
What it means
Returned by `commonResource.Slice` when iterating a `[]any` whose elements are not all `resource.Resource`. This is the resource-side adapter for `collections.Slice`, used to normalize heterogeneous template slices into `resource.Resources`. The per-element assertion at resource.go:290-292 fails on the first non-Resource element.
Source
Thrown at resources/resource.go:292
baseResourceResource
baseResourceInternal
resource.Staler
}
type commonResource struct{}
// Slice is for internal use.
// for the template functions. See collections.Slice.
func (commonResource) Slice(in any) (any, error) {
switch items := in.(type) {
case resource.Resources:
return items, nil
case []any:
groups := make(resource.Resources, len(items))
for i, v := range items {
g, ok := v.(resource.Resource)
if !ok {
return nil, fmt.Errorf("type %T is not a Resource", v)
}
groups[i] = g
{
}
}
return groups, nil
default:
return nil, fmt.Errorf("invalid slice type %T", items)
}
}
type fileInfo interface {
setOpenSource(hugio.OpenReadSeekCloser)
setSourceFilenameIsHash(bool)
setTargetPath(internal.ResourcePaths)
size() int64
hashProvider
}View on GitHub (pinned to 52c9bd7908)
Solutions
- Ensure every element of the input slice satisfies `resource.Resource` (filter out Pages/strings first).
- Use `.Resources` directly or `resources.Get`/`resources.Match` to build the slice.
- If you need to mix text, create a Resource first via `resources.FromString`.
Example fix
// template -- before
{{ $r := slice "main.js" $page.Resources.Get "app.js" }}
// after
{{ $r := slice (resources.Get "js/main.js") (resources.Get "js/app.js") }} Defensive patterns
Strategy: type-guard
Type guard
// Go
func isResource(v any) bool {
_, ok := v.(resource.Resource)
return ok
}
// Template
{{ $clean := slice }}
{{ range $r := $input }}{{ if (eq (printf "%T" $r) "*resources.resourceAdapter") }}{{ $clean = $clean | append $r }}{{ end }}{{ end }} Prevention
- Build resource lists only from `resources.Get`/`resources.Match`/`.Resources`.
- Wrap strings with `resources.FromString` before mixing into a resource list.
- Filter with `where` in templates before passing to resource helpers.
When it happens
Trigger: A template calls `collections.Slice` (or a pipe through `apply`) on a list containing non-resource values -- e.g. mixing `.Resources` entries with strings or pages, or passing page resources alongside plain Pages.
Common situations: Templates that build a JS/CSS bundle from `.Resources` plus a hardcoded string path; partial reuse where the param is sometimes a Resource and sometimes a Page; `slice` template function called with mixed media inputs.
Related errors
- invalid slice type %T
- type %T is not a WeightedPage
- invalid slice type %T
- %T cannot be merged by language
- called apply using {xt} as type {targ}
AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09).
Data as JSON: /api/errors/4f8391e9b876bcc5.
Report an issue: GitHub.