gohugoio/hugo · error

targetPath cannot be empty

Error message

targetPath cannot be empty

What it means

css.ChromaStyles generates a CSS stylesheet resource for Chroma syntax highlighting classes. The options map must include a targetPath telling Hugo where to publish the generated CSS; unlike style (which defaults from markup.highlight.style), targetPath has no default, so an empty value is rejected.

Source

Thrown at tpl/css/css.go:73

// markup.highlight.noClasses is disabled in config.
// The style defaults to the markup.highlight.style config setting.
func (ns *Namespace) ChromaStyles(opts any) (resource.Resource, error) {
	key := hashing.HashUint64(opts)
	return ns.chromaStylesCache.GetOrCreate(key, func() (resource.Resource, error) {
		m, err := hmaps.ToStringMapE(opts)
		if err != nil {
			return nil, err
		}
		var o struct {
			TargetPath                    string
			highlight.ChromaStylesOptions `mapstructure:",squash"`
		}

		if err := mapstructure.WeakDecode(m, &o); err != nil {
			return nil, err
		}
		if o.TargetPath == "" {
			return nil, errors.New("targetPath cannot be empty")
		}
		if o.Style == "" {
			o.Style = ns.highlightConfig.Style
		}
		css, err := highlight.ChromaStylesCSS(o.ChromaStylesOptions)
		if err != nil {
			return nil, err
		}

		return ns.d.ResourceSpec.NewResource(
			resources.ResourceSourceDescriptor{
				LazyPublish:   true,
				GroupIdentity: identity.Anonymous,
				OpenReadSeekCloser: func() (hugio.ReadSeekCloser, error) {
					return hugio.NewReadSeekerNoOpCloserFromString(css), nil
				},
				TargetPath: o.TargetPath,
			})

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Pass targetPath in the options dict: {{ $css := css.ChromaStyles (dict "targetPath" "css/chroma.css") }}.
  2. Check the key spelling — it must decode to TargetPath (targetPath/targetpath work; underscored variants do not).
  3. Ensure you pass a dict, not just the style string.

Example fix

{{/* before */}}
{{ $css := css.ChromaStyles (dict "style" "dracula") }}

{{/* after */}}
{{ $css := css.ChromaStyles (dict "style" "dracula" "targetPath" "css/chroma.css") }}
Defensive patterns

Strategy: validation

Validate before calling

{{ $target := "css/main.css" }}
{{ if not $target }}
  {{ errorf "css target path must be set" }}
{{ end }}
{{ $css := resources.Get "sass/main.scss" | css.Sass (dict "targetPath" $target) }}

Prevention

When it happens

Trigger: Calling {{ transform.ToCSS }}-style helper css.ChromaStyles with an options dict missing targetPath, with targetPath set to "", or with a misspelled key (e.g. targetpath decodes fine via WeakDecode but 'target_path' or 'path' does not).

Common situations: Users adding class-based highlighting (markup.highlight.noClasses = false) copy an example that omits targetPath, or pass the style name as a bare string instead of a dict.

Related errors


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