golangci/golangci-lint · error
invalid source regex: %w
Error message
invalid source regex: %w
What it means
Validation guard in BaseRule.Validate: the 'source' field of a rule is set but failed to compile as a regular expression. validateOptionalRegex rejects the rule before it can be used for filtering issues; the regex compile error is preserved via %w.
Source
Thrown at pkg/config/base_rule.go:34
// 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.
if b.Path != "" || b.PathExcept != "" {
nonBlank++
}
View on GitHub (pinned to ed7a235d2d)
Solutions
- Fix the `source` regex to valid Go RE2 syntax
- Rewrite lookbehind/backreference patterns using RE2-compatible constructs
- Prefer single-quoted YAML scalars for regexes to avoid backslash mangling
- Validate the pattern compiles with Go's regexp before committing config
Example fix
# before source: "(?<=foo)bar" # after source: 'foobar'
Defensive patterns
Strategy: validation
Validate before calling
func checkSourceRegex(pattern string) error {
if pattern == "" {
return nil
}
_, err := regexp.Compile(pattern)
return err
}
// before running: checkSourceRegex(rule.Source) Prevention
- Rewrite lookbehind patterns (`(?<=...)`) in RE2-compatible form
- Compile all source patterns in tests to catch breakage early
- Quote regexes with single quotes in YAML
- Consult Go regexp docs (RE2) for supported syntax before writing patterns
When it happens
Trigger: An exclusion/inclusion rule sets `source` to a pattern Go's regexp package cannot compile — syntax errors or RE2-unsupported features like lookbehinds and backreferences.
Common situations: Reusing PCRE-style source filters from other linters; regexes with `(?<=...)` lookbehinds; escaping mistakes in YAML double-quoted strings.
Related errors
- invalid path regex: %w
- invalid path-except regex: %w
- invalid text regex: %w
- the configuration contains invalid elements
- the configuration contains invalid elements
AI-assisted analysis of golangci/golangci-lint@ed7a235d2d (2026-09-02).
Data as JSON: /api/errors/dfe0ff3fa3ea9e4f.
Report an issue: GitHub.