golangci/golangci-lint · error

compile path pattern %q: %w

Error message

compile path pattern %q: %w

What it means

Each path pattern under formatters.exclusions.paths is normalized and compiled as a regular expression; an invalid regex aborts runner-options construction with this wrap. golangci-lint treats formatter exclusion patterns as regexes, so syntax errors are caught eagerly at startup.

Source

Thrown at pkg/goformat/runner.go:248

	absBasePath, err := filepath.Abs(basePath)
	if err != nil {
		return RunnerOptions{}, err
	}

	opts := RunnerOptions{
		basePath:            absBasePath,
		generated:           cfg.Formatters.Exclusions.Generated,
		diff:                diff || diffColored,
		colors:              diffColored,
		stdin:               stdin,
		excludedPathCounter: make(map[*regexp.Regexp]int),
		warnUnused:          cfg.Formatters.Exclusions.WarnUnused,
	}

	for _, pattern := range cfg.Formatters.Exclusions.Paths {
		exp, err := regexp.Compile(fsutils.NormalizePathInRegex(pattern))
		if err != nil {
			return RunnerOptions{}, fmt.Errorf("compile path pattern %q: %w", pattern, err)
		}

		opts.patterns = append(opts.patterns, exp)
		opts.excludedPathCounter[exp] = 0
	}

	return opts, nil
}

func (o RunnerOptions) MatchAnyPattern(path string) (bool, error) {
	if len(o.patterns) == 0 {
		return false, nil
	}

	abs, err := filepath.Abs(path)
	if err != nil {
		return false, err
	}

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Fix the regex in formatters.exclusions.paths; test with `go` regexp (RE2) semantics
  2. Escape backslashes: use `\\` on Windows paths, or prefer forward slashes
  3. Convert glob-style patterns (`**`) to regex equivalents
  4. Validate quickly with `go run` / a RE2 playground before editing config

Example fix

# before
formatters:
  exclusions:
    paths:
      - (gen|unbalanced
# after
formatters:
  exclusions:
    paths:
      - (gen|vendor)/.*
Defensive patterns

Strategy: validation

Validate before calling

// validate exclusion patterns are valid RE2 before use
for _, p := range patterns {
    if _, err := regexp.Compile(p); err != nil {
        return fmt.Errorf("invalid exclusion pattern %q: %w", p, err)
    }
}

Try / catch

// catch and point at the offending config key
if _, err := NewRunnerOptions(cfg, false, false, false); err != nil {
    var reErr error
    if strings.Contains(err.Error(), "compile path pattern") {
        reErr = err // fix formatters.exclusions.paths
    }
}

Prevention

When it happens

Trigger: NewRunnerOptions iterates cfg.Formatters.Exclusions.Paths and regexp.Compile fails for one of the configured patterns (e.g. unbalanced `(`, stray `*`, invalid escape).

Common situations: Hand-written exclusion regex in .golangci.yml with escaping mistakes (Windows backslashes not escaped); patterns copied from glob-style configs (e.g. `src/**` instead of regex).

Related errors


AI-assisted analysis of golangci/golangci-lint@ed7a235d2d (2026-09-02). Data as JSON: /api/errors/e96b91ed47e335a2. Report an issue: GitHub.