golangci/golangci-lint · error
invalid path regex: %w
Error message
invalid path regex: %w
What it means
BaseRule.Validate checks that rule conditions hold valid regular expressions. The Path (path) pattern of an exclude/include rule is compiled via validateOptionalRegex; an invalid Go regex fails validation with this wrapped error before the linter runs.
Source
Thrown at pkg/config/base_rule.go:22
"errors"
"fmt"
"regexp"
)
type BaseRule struct {
Linters []string `mapstructure:"linters"`
Path string `mapstructure:"path"`
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")
}
View on GitHub (pinned to ed7a235d2d)
Solutions
- Fix the `path` regex so it compiles as Go RE2 syntax (test with regexp.Compile)
- Replace lookaheads/lookbehinds with RE2-compatible alternatives (e.g. two rules, or `path-except`)
- Quote regex carefully in YAML: prefer single quotes to avoid escape-eating by double quotes
- Verify the pattern with `go run` / a RE2 playground before committing it
Example fix
# before path: "(?!vendor).*\\.go$" # after path: "^(?!vendor/).*\\.go$" # invalid in RE2 — use instead: # path-except: '^vendor/.*\.go$'
Defensive patterns
Strategy: validation
Validate before calling
func checkRegex(field, pattern string) error {
if pattern == "" {
return nil
}
if _, err := regexp.Compile(pattern); err != nil {
return fmt.Errorf("%s: %w", field, err)
}
return nil
}
// before running: checkRegex("path", rule.Path) Prevention
- Validate every regex in config with Go's regexp.Compile in tests
- Remember Go RE2 has no lookaheads/lookbehinds/backreferences
- Use single-quoted YAML strings for regex patterns
- Convert glob habits (`**`) into regex equivalents (`.*`)
When it happens
Trigger: Configuring `issues.exclude-rules` or `linters.exclusion-rules` (or presets referencing BaseRule) with a `path` value that is not a valid Go regexp — e.g. unbalanced parentheses, trailing `\`, invalid `(?!...)` lookahead which Go RE2 does not support.
Common situations: Porting PCRE/JS-style regexes (lookaheads/lookbehinds) into Go config; typos like `[a-z` unclosed character class; escaping mistakes in YAML double-quoted strings.
Related errors
- invalid path-except regex: %w
- invalid text regex: %w
- invalid source 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/ed3142ac2dc10626.
Report an issue: GitHub.