twpayne/chezmoi · error

%s: invalid pattern

Error message

%s: invalid pattern

What it means

PatternSet.Add validates each pattern with doublestar.ValidatePattern before adding it to the include or exclude sets. If the pattern is not valid doublestar glob syntax, the pattern is rejected with this error naming the pattern. This catches malformed globs early instead of failing during matching.

Source

Thrown at internal/chezmoi/patternset.go:48

// An PatternSet is a set of patterns.
type PatternSet struct {
	IncludePatterns chezmoiset.Set[string]
	ExcludePatterns chezmoiset.Set[string]
}

// NewPatternSet returns a new patternSet.
func NewPatternSet() *PatternSet {
	return &PatternSet{
		IncludePatterns: chezmoiset.New[string](),
		ExcludePatterns: chezmoiset.New[string](),
	}
}

// Add adds a pattern to ps.
func (ps *PatternSet) Add(pattern string, include PatternSetIncludeType) error {
	if ok := doublestar.ValidatePattern(pattern); !ok {
		return fmt.Errorf("%s: invalid pattern", pattern)
	}
	switch include {
	case PatternSetInclude:
		ps.IncludePatterns.Add(pattern)
	case PatternSetExclude:
		ps.ExcludePatterns.Add(pattern)
	}
	return nil
}

// Glob returns all matches in fileSystem.
func (ps *PatternSet) Glob(fileSystem vfs.FS, prefix string) ([]string, error) {
	allMatches := chezmoiset.New[string]()
	for includePattern := range ps.IncludePatterns {
		matches, err := Glob(fileSystem, filepath.ToSlash(prefix+includePattern))
		if err != nil {
			return nil, err
		}

View on GitHub (pinned to f901167e46)

Solutions

  1. Fix the pattern to valid doublestar glob syntax (balance [], escape literals with \\)
  2. Test the pattern with doublestar.ValidatePattern or a glob tester before adding
  3. Remove or comment out the offending pattern to isolate which one is invalid

Example fix

// before
patternSet.Add("[a-.md", PatternSetInclude) // unbalanced class

// after
patternSet.Add("[a-z]*.md", PatternSetInclude)
Defensive patterns

Strategy: validation

Validate before calling

if !doublestar.ValidatePattern(pattern) {
    return fmt.Errorf("rejecting invalid pattern %q", pattern)
}

Try / catch

if err := ps.Add(pattern, include); err != nil {
    if strings.Contains(err.Error(), "invalid pattern") {
        // log pattern and skip it instead of aborting
    }
}

Prevention

When it happens

Trigger: Calling Add (via mustNewPatternSet, addPatterns, or readExternalArchive) with a pattern containing invalid glob syntax, e.g. unclosed '[', bad '**' placement, or stray characters doublestar rejects.

Common situations: Hand-edited .chezmoiignore patterns, copy-pasted globs from other tools with unsupported syntax, unbalanced character classes like '[a-' in ignore patterns.

Related errors


AI-assisted analysis of twpayne/chezmoi@f901167e46 (2026-09-01). Data as JSON: /api/errors/e1ecc4d5d11c3940. Report an issue: GitHub.