golangci/golangci-lint · error

invalid text regex: %w

Error message

invalid text regex: %w

What it means

BaseRule.Validate compiles the Text pattern (used to match issue text) via validateOptionalRegex. An invalid regex fails validation with this wrapped error, preventing the linter from starting with a broken rule.

Source

Thrown at pkg/config/base_rule.go:30

	PathExcept string   `mapstructure:"path-except"`
	Text       string   `mapstructure:"text"`
	Source     string   `mapstructure:"source"`

	// For compatibility with exclude-use-default/include.
	InternalReference string `mapstructure:"-"`
}

func (b *BaseRule) Validate(minConditionsCount int) error {
	if err := validateOptionalRegex(b.Path); err != nil {
		return fmt.Errorf("invalid path regex: %w", err)
	}

	if err := validateOptionalRegex(b.PathExcept); err != nil {
		return fmt.Errorf("invalid path-except regex: %w", err)
	}

	if err := validateOptionalRegex(b.Text); err != nil {
		return fmt.Errorf("invalid text regex: %w", err)
	}

	if err := validateOptionalRegex(b.Source); err != nil {
		return fmt.Errorf("invalid source regex: %w", err)
	}

	if b.Path != "" && b.PathExcept != "" {
		return errors.New("path and path-except should not be set at the same time")
	}

	nonBlank := 0
	if len(b.Linters) > 0 {
		nonBlank++
	}

	// Filtering by path counts as one condition, regardless how it is done (one or both).
	// Otherwise, a rule with Path and PathExcept set would pass validation
	// whereas before the introduction of path-except that wouldn't have been precise enough.

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Fix the `text` regex to valid Go RE2 syntax
  2. Escape literal metacharacters (`\(`, `\+`) that were meant literally
  3. Use single-quoted YAML strings to keep backslashes intact
  4. Paste the pattern into a Go-compatible regex tester to confirm it compiles

Example fix

# before
text: "undeclared name: (foo|bar"
# after
text: "undeclared name: (foo|bar)"
Defensive patterns

Strategy: validation

Validate before calling

func checkTextRegex(pattern string) error {
	if pattern == "" {
		return nil
	}
	_, err := regexp.Compile(pattern)
	return err
}
// before running: checkTextRegex(rule.Text)

Prevention

When it happens

Trigger: An exclusion/inclusion rule sets `text` to a pattern that Go's regexp cannot compile — invalid syntax, dangling quantifier, or unsupported constructs (backreferences, lookarounds).

Common situations: Matching lint messages copied from other tools' PCRE syntax; unescaped special characters like `(` or `+` intended literally; regexes written for grep -P.

Related errors


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