gohugoio/hugo · error

error in Parse: %w

Error message

error in Parse: %w

What it means

`urls.Parse` first coerces its argument to a string with `cast.ToStringE`; if the value can't be stringified (maps, slices, structs like Pages), the cast error is wrapped as 'error in Parse'. Note the actual URL parsing error from `url.Parse` is returned separately — this one is purely a type-conversion failure.

Source

Thrown at tpl/urls/urls.go:56

	multihost bool
}

// AbsURL takes the string s and converts it to an absolute URL.
func (ns *Namespace) AbsURL(s any) (string, error) {
	ss, err := cast.ToStringE(s)
	if err != nil {
		return "", err
	}

	return ns.deps.PathSpec.AbsURL(ss, false), nil
}

// Parse parses rawurl into a URL structure. The rawurl may be relative or
// absolute.
func (ns *Namespace) Parse(rawurl any) (*url.URL, error) {
	s, err := cast.ToStringE(rawurl)
	if err != nil {
		return nil, fmt.Errorf("error in Parse: %w", err)
	}

	return url.Parse(s)
}

// RelURL takes the string s and prepends the relative path according to a
// page's position in the project directory structure.
func (ns *Namespace) RelURL(s any) (string, error) {
	ss, err := cast.ToStringE(s)
	if err != nil {
		return "", err
	}

	return ns.deps.PathSpec.RelURL(ss, false), nil
}

// URLize returns the strings s formatted as an URL.
func (ns *Namespace) URLize(s any) (string, error) {

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Pass a string: `{{ $u := urls.Parse .Params.link }}` where link is a scalar string.
  2. For Pages/Resources, parse the permalink: `{{ urls.Parse $p.Permalink }}`.
  3. If the value is a list, range over it and Parse each element.

Example fix

{{/* before */}}
{{ $u := urls.Parse $page }}
{{/* after */}}
{{ $u := urls.Parse $page.Permalink }}
Defensive patterns

Strategy: try-catch

Validate before calling

{{ $u := trim $input " " }}
{{ if $u }}{{ $parsed := urls.Parse $u }}{{ end }}

Try / catch

{{ with try (urls.Parse $raw) }}
  {{ with .Err }}{{ warnf "unparsable URL %q: %s" $raw . }}{{ else }}{{ $u := .Value }}{{ end }}
{{ end }}

Prevention

When it happens

Trigger: `{{ urls.Parse .Params.links }}` where the param is a map or slice, `{{ urls.Parse . }}` inside a range over complex objects, or passing a Page/Resource object instead of its `.Permalink` string.

Common situations: Front matter fields that are lists (`urls: [a, b]`) passed whole to Parse; passing a Page object rather than `.Permalink`/`.RelPermalink`; unexpected data shapes from `site.Data`.

Related errors


AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31). Data as JSON: /data/errors/8fc52a32a94a89d7.json. Report an issue: GitHub ↗.