Skip to content

Commit 9664d89

Browse files
committed
WIP: consider path prefix when matching exclude rules
When invoking or configuring golangci-lint with a path prefix, the output contains files with that path. But the paths that the exclude rules get compared against are the ones without the additional prefix. This makes it impossible to use a configuration file with non-trivial path rules (i.e. anything that tries to match directory names) from different directories because the rules will only match in one of them (typically the root).
1 parent a9acb8d commit 9664d89

File tree

5 files changed

+27
-21
lines changed

5 files changed

+27
-21
lines changed

Diff for: pkg/lint/runner.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env, es *lint
8383
processors.NewIdentifierMarker(),
8484

8585
getExcludeProcessor(&cfg.Issues),
86-
getExcludeRulesProcessor(&cfg.Issues, log, lineCache),
86+
getExcludeRulesProcessor(&cfg.Issues, log, cfg.Output.PathPrefix, lineCache),
8787
processors.NewNolint(log.Child(logutils.DebugKeyNolint), dbManager, enabledLinters),
8888

8989
processors.NewUniqByLine(cfg),
@@ -259,7 +259,7 @@ func getExcludeProcessor(cfg *config.Issues) processors.Processor {
259259
return excludeProcessor
260260
}
261261

262-
func getExcludeRulesProcessor(cfg *config.Issues, log logutils.Log, lineCache *fsutils.LineCache) processors.Processor {
262+
func getExcludeRulesProcessor(cfg *config.Issues, log logutils.Log, pathPrefix string, lineCache *fsutils.LineCache) processors.Processor {
263263
var excludeRules []processors.ExcludeRule
264264
for _, r := range cfg.ExcludeRules {
265265
excludeRules = append(excludeRules, processors.ExcludeRule{
@@ -287,12 +287,14 @@ func getExcludeRulesProcessor(cfg *config.Issues, log logutils.Log, lineCache *f
287287
if cfg.ExcludeCaseSensitive {
288288
excludeRulesProcessor = processors.NewExcludeRulesCaseSensitive(
289289
excludeRules,
290+
pathPrefix,
290291
lineCache,
291292
log.Child(logutils.DebugKeyExcludeRules),
292293
)
293294
} else {
294295
excludeRulesProcessor = processors.NewExcludeRules(
295296
excludeRules,
297+
pathPrefix,
296298
lineCache,
297299
log.Child(logutils.DebugKeyExcludeRules),
298300
)

Diff for: pkg/result/processors/base_rule.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package processors
22

33
import (
4+
"path"
45
"regexp"
56

67
"github.com/golangci/golangci-lint/pkg/fsutils"
@@ -26,14 +27,14 @@ func (r *baseRule) isEmpty() bool {
2627
return r.text == nil && r.source == nil && r.path == nil && len(r.linters) == 0
2728
}
2829

29-
func (r *baseRule) match(issue *result.Issue, lineCache *fsutils.LineCache, log logutils.Log) bool {
30+
func (r *baseRule) match(issue *result.Issue, pathPrefix string, lineCache *fsutils.LineCache, log logutils.Log) bool {
3031
if r.isEmpty() {
3132
return false
3233
}
3334
if r.text != nil && !r.text.MatchString(issue.Text) {
3435
return false
3536
}
36-
if r.path != nil && !r.path.MatchString(issue.FilePath()) {
37+
if r.path != nil && !r.path.MatchString(path.Join(pathPrefix, issue.FilePath())) {
3738
return false
3839
}
3940
if len(r.linters) != 0 && !r.matchLinter(issue) {

Diff for: pkg/result/processors/exclude_rules.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ type ExcludeRule struct {
1717
}
1818

1919
type ExcludeRules struct {
20-
rules []excludeRule
21-
lineCache *fsutils.LineCache
22-
log logutils.Log
20+
rules []excludeRule
21+
lineCache *fsutils.LineCache
22+
log logutils.Log
23+
pathPrefix string
2324
}
2425

25-
func NewExcludeRules(rules []ExcludeRule, lineCache *fsutils.LineCache, log logutils.Log) *ExcludeRules {
26+
func NewExcludeRules(rules []ExcludeRule, pathPrefix string, lineCache *fsutils.LineCache, log logutils.Log) *ExcludeRules {
2627
r := &ExcludeRules{
27-
lineCache: lineCache,
28-
log: log,
28+
lineCache: lineCache,
29+
log: log,
30+
pathPrefix: pathPrefix,
2931
}
3032
r.rules = createRules(rules, "(?i)")
3133

@@ -59,7 +61,7 @@ func (p ExcludeRules) Process(issues []result.Issue) ([]result.Issue, error) {
5961
return filterIssues(issues, func(i *result.Issue) bool {
6062
for _, rule := range p.rules {
6163
rule := rule
62-
if rule.match(i, p.lineCache, p.log) {
64+
if rule.match(i, p.pathPrefix, p.lineCache, p.log) {
6365
return false
6466
}
6567
}
@@ -76,10 +78,11 @@ type ExcludeRulesCaseSensitive struct {
7678
*ExcludeRules
7779
}
7880

79-
func NewExcludeRulesCaseSensitive(rules []ExcludeRule, lineCache *fsutils.LineCache, log logutils.Log) *ExcludeRulesCaseSensitive {
81+
func NewExcludeRulesCaseSensitive(rules []ExcludeRule, pathPrefix string, lineCache *fsutils.LineCache, log logutils.Log) *ExcludeRulesCaseSensitive {
8082
r := &ExcludeRules{
81-
lineCache: lineCache,
82-
log: log,
83+
lineCache: lineCache,
84+
log: log,
85+
pathPrefix: pathPrefix,
8386
}
8487
r.rules = createRules(rules, "")
8588

Diff for: pkg/result/processors/exclude_rules_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestExcludeRulesMultiple(t *testing.T) {
3737
Linters: []string{"lll"},
3838
},
3939
},
40-
}, lineCache, nil)
40+
}, "" /* path prefix */, lineCache, nil)
4141

4242
cases := []issueTestCase{
4343
{Path: "e.go", Text: "exclude", Linter: "linter"},
@@ -78,7 +78,7 @@ func TestExcludeRulesText(t *testing.T) {
7878
Linters: []string{"linter"},
7979
},
8080
},
81-
}, nil, nil)
81+
}, "" /* path prefix */, nil, nil)
8282
texts := []string{"excLude", "1", "", "exclud", "notexclude"}
8383
var issues []result.Issue
8484
for _, t := range texts {
@@ -99,7 +99,7 @@ func TestExcludeRulesText(t *testing.T) {
9999
}
100100

101101
func TestExcludeRulesEmpty(t *testing.T) {
102-
processAssertSame(t, NewExcludeRules(nil, nil, nil), newIssueFromTextTestCase("test"))
102+
processAssertSame(t, NewExcludeRules(nil, "" /* path prefix */, nil, nil), newIssueFromTextTestCase("test"))
103103
}
104104

105105
func TestExcludeRulesCaseSensitiveMultiple(t *testing.T) {
@@ -129,7 +129,7 @@ func TestExcludeRulesCaseSensitiveMultiple(t *testing.T) {
129129
Linters: []string{"lll"},
130130
},
131131
},
132-
}, lineCache, nil)
132+
}, "" /* path prefix */, lineCache, nil)
133133

134134
cases := []issueTestCase{
135135
{Path: "e.go", Text: "exclude", Linter: "linter"},
@@ -175,7 +175,7 @@ func TestExcludeRulesCaseSensitiveText(t *testing.T) {
175175
Linters: []string{"linter"},
176176
},
177177
},
178-
}, nil, nil)
178+
}, "" /* path prefix */, nil, nil)
179179
texts := []string{"exclude", "excLude", "1", "", "exclud", "notexclude"}
180180
var issues []result.Issue
181181
for _, t := range texts {
@@ -196,5 +196,5 @@ func TestExcludeRulesCaseSensitiveText(t *testing.T) {
196196
}
197197

198198
func TestExcludeRulesCaseSensitiveEmpty(t *testing.T) {
199-
processAssertSame(t, NewExcludeRulesCaseSensitive(nil, nil, nil), newIssueFromTextTestCase("test"))
199+
processAssertSame(t, NewExcludeRulesCaseSensitive(nil, "" /* path prefix */, nil, nil), newIssueFromTextTestCase("test"))
200200
}

Diff for: pkg/result/processors/severity_rules.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (p SeverityRules) Process(issues []result.Issue) ([]result.Issue, error) {
7070
ruleSeverity = rule.severity
7171
}
7272

73-
if rule.match(i, p.lineCache, p.log) {
73+
if rule.match(i, "" /* TODO? path prefix */, p.lineCache, p.log) {
7474
i.Severity = ruleSeverity
7575
return i
7676
}

0 commit comments

Comments
 (0)