golangci/golangci-lint · error
can't compile regexp %q: %w
Error message
can't compile regexp %q: %w
What it means
NewExclusionPaths compiles each path exclusion pattern from the config into a regular expression. A pattern that is not valid Go regexp syntax fails compilation and produces this error naming the offending pattern. This is a startup/config error: golangci-lint cannot build its issue exclusion pipeline with an invalid regex.
Source
Thrown at pkg/result/processors/exclusion_paths.go:35
pathExceptPatterns []*regexp.Regexp
warnUnused bool
excludedPathCounter map[*regexp.Regexp]int
excludedPathExceptCounter map[*regexp.Regexp]int
log logutils.Log
}
func NewExclusionPaths(log logutils.Log, cfg *config.LinterExclusions) (*ExclusionPaths, error) {
excludedPathCounter := make(map[*regexp.Regexp]int)
var pathPatterns []*regexp.Regexp
for _, p := range cfg.Paths {
p = fsutils.NormalizePathInRegex(p)
patternRe, err := regexp.Compile(p)
if err != nil {
return nil, fmt.Errorf("can't compile regexp %q: %w", p, err)
}
pathPatterns = append(pathPatterns, patternRe)
excludedPathCounter[patternRe] = 0
}
excludedPathExceptCounter := make(map[*regexp.Regexp]int)
var pathExceptPatterns []*regexp.Regexp
for _, p := range cfg.PathsExcept {
p = fsutils.NormalizePathInRegex(p)
patternRe, err := regexp.Compile(p)
if err != nil {
return nil, fmt.Errorf("can't compile regexp %q: %w", p, err)
}
pathExceptPatterns = append(pathExceptPatterns, patternRe)View on GitHub (pinned to ed7a235d2d)
Solutions
- Fix the regex syntax for the reported pattern (test it with a Go regexp playground or go test)
- Escape metacharacters, e.g. use \. for a literal dot; use forward slashes
- Replace glob-style patterns with regex equivalents (e.g. '^src/.*/.*\.go$')
- Validate config before CI with a quick `golangci-lint run` locally
Example fix
# before
issues:
exclude-paths:
- vendor\pkg
- gen(.*
# after
issues:
exclude-paths:
- vendor/pkg
- ^gen/.*$ Defensive patterns
Strategy: validation
Validate before calling
// Go: compile every exclusion pattern before writing it into config
for _, p := range cfg.Paths {
if _, err := regexp.Compile(fsutils.NormalizePathInRegex(p)); err != nil {
log.Fatalf("invalid exclude-paths pattern %q: %v", p, err)
}
} Try / catch
ep, err := processors.NewExclusionPaths(cfg)
if err != nil {
if strings.Contains(err.Error(), "can't compile regexp") {
log.Fatalf("fix your exclusion pattern: %v", err)
}
return err
} Prevention
- Test each pattern with a RE2-compatible playground before adding it
- Remember exclude paths are regexes, not globs — escape dots and use .*
- Avoid Windows backslashes; use forward slashes
When it happens
Trigger: NewExclusionPaths is called (by NewRunner or tests) with cfg.Paths containing a string that regexp.Compile rejects — unbalanced parentheses, stray '*+', invalid escape like '\\', unterminated character class.
Common situations: Hand-written .golangci.yml exclusion patterns with unescaped regex metacharacters (e.g. 'vendor(*') ; copying file globs (paths like 'src/**/*.go') into regex-only settings; Windows-style backslash separators in patterns.
Related errors
- invalid path regex: %w
- invalid path-except regex: %w
- invalid text regex: %w
- invalid source regex: %w
- the configuration contains invalid elements
AI-assisted analysis of golangci/golangci-lint@ed7a235d2d (2026-09-02).
Data as JSON: /api/errors/f2e9e65fe85bd80d.
Report an issue: GitHub.