gohugoio/hugo · error

failed to compile Glob %q: %w

Error message

failed to compile Glob %q: %w

What it means

Raised by `getGlob` in hugolib/segments/segments.go:284 when compiling a pattern from the `segments` site configuration. Segment matcher fields (`kind`, `path`, `output`) are glob patterns (compiled via Hugo's glob helper, optionally negated with a leading "! "); a syntactically invalid glob aborts config decoding for the segments namespace.

Source

Thrown at hugolib/segments/segments.go:284

// SegmentMatcherFields holds string slices of ordered filters for segment matching.
// The Glob patterns can be negated by prefixing with "! ".
// The first match wins (either include or exclude).
type SegmentMatcherFields struct {
	Kind   []string
	Path   []string
	Output []string
	Lang   string            // Deprecated: use Sites.Matrix instead.
	Sites  sitesmatrix.Sites // Note that we only use Sites.Matrix for now.
}

func getGlob(s string) (glob.Glob, error) {
	if s == "" {
		return nil, nil
	}
	g, err := hglob.GetGlob(s)
	if err != nil {
		return nil, fmt.Errorf("failed to compile Glob %q: %w", s, err)
	}
	return g, nil
}

func DecodeSegments(in map[string]any, segmentsToRender []string, logger loggers.Logger) (*config.ConfigNamespace[map[string]SegmentConfig, *Segments], error) {
	buildConfig := func(in any) (*Segments, any, error) {
		m, err := hmaps.ToStringMapE(in)
		if err != nil {
			return nil, nil, err
		}
		if m == nil {
			m = map[string]any{}
		}
		m = hmaps.CleanConfigStringMap(m)

		var segmentCfg map[string]SegmentConfig
		if err := mapstructure.WeakDecode(m, &segmentCfg); err != nil {
			return nil, nil, err

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Fix the quoted pattern in the error: segments patterns are globs, so use `**` / `*` / `{a,b}` syntax, not regex (`.*`, anchors, backreferences).
  2. Balance any `{...}` groups and `[...]` classes in the pattern.
  3. Test by removing the segment definition and re-adding patterns one at a time to isolate the bad one.

Example fix

# before
[segments.docs]
  [[segments.docs.includes]]
  path = "/docs/{intro,guide"

# after
[segments.docs]
  [[segments.docs.includes]]
  path = "/docs/{intro,guide}/**"
Defensive patterns

Strategy: validation

Validate before calling

import "github.com/gobwas/glob"
if _, err := glob.Compile(pattern, '/'); err != nil {
    return fmt.Errorf("segments filter glob %q: %w", pattern, err)
}

Prevention

When it happens

Trigger: Defining `[segments.<name>]` includes/excludes whose `path`, `kind`, or `output` entries contain an invalid glob — e.g. an unclosed character class `[abc`, an unclosed brace group `{a,b`, or a stray escape at end of pattern — then running any hugo command that loads config (`hugo`, `hugo server --renderSegments`).

Common situations: Writing regex syntax where a glob is expected (segments use globs, unlike deployment matchers which use regexes); unbalanced `{}` alternation groups in path patterns like `/blog/{a,b`; typoed character classes; confusion introduced when copying patterns between config sections.

Related errors


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