gohugoio/hugo · error

targetPath cannot be empty

Error message

targetPath cannot be empty

What it means

The `css.ChromaStyles` function generates a syntax-highlighting stylesheet. Its options dict must include a `targetPath` so Hugo knows where to publish the generated CSS. After decoding the options struct, if `TargetPath` is empty, this error fires at css.go:72–73.

Solutions

  1. Add `targetPath` to the options dict: `{{ (css.ChromaStyles (dict "targetPath" "css/chroma.css" "style" "monokai")) }}`.
  2. Use the exact key name `targetPath` (case-insensitive via mapstructure).
  3. Verify the dict is non-nil and contains the key before the call.

Example fix

// before
{{ (css.ChromaStyles (dict "style" "monokai")) }}
// after
{{ (css.ChromaStyles (dict "targetPath" "css/chroma.css" "style" "monokai")) }}
Defensive patterns

Strategy: validation

Validate before calling

{{ $opts := dict "targetPath" "css/chroma.css" "style" "monokai" }}
{{ if $opts.targetPath }}{{ (css.ChromaStyles $opts) }}{{ end }}

Type guard

func hasTargetPath(opts map[string]any) bool {
    tp, _ := opts["targetPath"].(string)
    return tp != ""
}

Prevention

When it happens

Trigger: Calling `{{ (css.ChromaStyles (dict "style" "monokai")) }}` without a `targetPath` key, or passing an empty string: `{{ (css.ChromaStyles (dict "targetPath" "" "style" "dracula")) }}`.

Common situations: A developer copies a ChromaStyles example but drops or renames the `targetPath` key (e.g. uses `target` or `path` instead). The config decode silently leaves TargetPath empty.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/6c222170374cee06. Report an issue: GitHub.

Appendix: 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 52c9bd7908)