Skip to content

Commit 362aea5

Browse files
authored
dev: new processor order (#5322)
1 parent 6d29861 commit 362aea5

25 files changed

+130
-44
lines changed

pkg/fsutils/fsutils.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ func Getwd() (string, error) {
3434
return
3535
}
3636

37-
evaledWd, err := EvalSymlinks(cachedWd)
37+
evaluatedWd, err := EvalSymlinks(cachedWd)
3838
if err != nil {
3939
cachedWd, cachedWdError = "", fmt.Errorf("can't eval symlinks on wd %s: %w", cachedWd, err)
4040
return
4141
}
4242

43-
cachedWd = evaledWd
43+
cachedWd = evaluatedWd
4444
})
4545

4646
return cachedWd, cachedWdError
@@ -83,8 +83,8 @@ func ShortestRelPath(path, wd string) (string, error) {
8383
path = evaledPath
8484

8585
// make path absolute and then relative to be able to fix this case:
86-
// we are in /test dir, we want to normalize ../test, and have file file.go in this dir;
87-
// it must have normalized path file.go, not ../test/file.go,
86+
// we are in `/test` dir, we want to normalize `../test`, and have file `file.go` in this dir;
87+
// it must have normalized path `file.go`, not `../test/file.go`,
8888
var absPath string
8989
if filepath.IsAbs(path) {
9090
absPath = path

pkg/fsutils/fsutils_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package fsutils
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestShortestRelPath(t *testing.T) {
12+
testCases := []struct {
13+
desc string
14+
path string
15+
wd string
16+
expected string
17+
}{
18+
{
19+
desc: "based on parent path",
20+
path: "fsutils_test.go",
21+
wd: filepath.Join("..", "fsutils"),
22+
expected: "fsutils_test.go",
23+
},
24+
{
25+
desc: "based on current working directory",
26+
path: "fsutils_test.go",
27+
wd: "",
28+
expected: "fsutils_test.go",
29+
},
30+
}
31+
32+
for _, test := range testCases {
33+
t.Run(test.desc, func(t *testing.T) {
34+
t.Parallel()
35+
36+
rel, err := ShortestRelPath("fsutils_test.go", filepath.Join("..", "fsutils"))
37+
require.NoError(t, err)
38+
39+
assert.Equal(t, test.expected, rel)
40+
})
41+
}
42+
}

pkg/lint/runner.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ func NewRunner(log logutils.Log, cfg *config.Config, args []string, goenv *gouti
7979
// Must be after FilenameUnadjuster.
8080
processors.NewInvalidIssue(log.Child(logutils.DebugKeyInvalidIssue)),
8181

82-
// Must be before diff, nolint and exclude autogenerated processor at least.
82+
// Must be before Diff, SkipFiles, SkipDirs, ExcludeRules processors at least.
8383
processors.NewPathPrettifier(log),
84+
85+
// must be after PathPrettifier.
8486
skipFilesProcessor,
85-
skipDirsProcessor, // must be after path prettifier
87+
skipDirsProcessor,
8688

8789
processors.NewAutogeneratedExclude(cfg.Issues.ExcludeGenerated),
8890

@@ -94,20 +96,21 @@ func NewRunner(log logutils.Log, cfg *config.Config, args []string, goenv *gouti
9496

9597
processors.NewNolint(log.Child(logutils.DebugKeyNolint), dbManager, enabledLinters),
9698

97-
processors.NewUniqByLine(cfg),
9899
processors.NewDiff(&cfg.Issues),
100+
101+
// The fixer still needs to see paths for the issues that are relative to the current directory.
102+
processors.NewFixer(cfg, log, fileCache, metaFormatter),
103+
104+
// Must be after the Fixer.
105+
processors.NewUniqByLine(cfg),
99106
processors.NewMaxPerFileFromLinter(cfg),
100107
processors.NewMaxSameIssues(cfg.Issues.MaxSameIssues, log.Child(logutils.DebugKeyMaxSameIssues), cfg),
101108
processors.NewMaxFromLinter(cfg.Issues.MaxIssuesPerLinter, log.Child(logutils.DebugKeyMaxFromLinter), cfg),
102109

110+
// Now we can modify the issues for output.
103111
processors.NewSourceCode(lineCache, log.Child(logutils.DebugKeySourceCode)),
104112
processors.NewPathShortener(),
105113
processors.NewSeverity(log.Child(logutils.DebugKeySeverityRules), files, &cfg.Severity),
106-
107-
// The fixer still needs to see paths for the issues that are relative to the current directory.
108-
processors.NewFixer(cfg, log, fileCache, metaFormatter),
109-
110-
// Now we can modify the issues for output.
111114
processors.NewPathPrefixer(cfg.Output.PathPrefix),
112115
processors.NewSortResults(cfg),
113116
},

pkg/result/processors/autogenerated_exclude.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ type fileSummary struct {
3939
generated bool
4040
}
4141

42+
// AutogeneratedExclude filters generated files.
43+
// - mode "lax": see `isGeneratedFileLax` documentation.
44+
// - mode "strict": see `isGeneratedFileStrict` documentation.
45+
// - mode "disable": skips this processor.
4246
type AutogeneratedExclude struct {
4347
debugf logutils.DebugFunc
4448

pkg/result/processors/cgo.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import (
1010

1111
var _ Processor = (*Cgo)(nil)
1212

13-
// Cgo some linters (e.g. gosec, deadcode) return incorrect filepaths for cgo issues,
14-
// also cgo files have strange issues looking like false positives.
13+
// Cgo filters cgo artifacts.
1514
//
16-
// Require absolute filepath.
15+
// Some linters (e.g. gosec, etc.) return incorrect file paths for cgo files.
16+
//
17+
// Require absolute file path.
1718
type Cgo struct {
1819
goCacheDir string
1920
}

pkg/result/processors/diff.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ const envGolangciDiffProcessorPatch = "GOLANGCI_DIFF_PROCESSOR_PATCH"
1818

1919
var _ Processor = (*Diff)(nil)
2020

21+
// Diff filters issues based on options `new`, `new-from-rev`, etc.
22+
//
23+
// Uses `git`.
24+
// The paths inside the patch are relative to the path where git is run (the same location where golangci-lint is run).
25+
//
26+
// Warning: it doesn't use `path-prefix` option.
2127
type Diff struct {
2228
onlyNew bool
2329
fromRev string

pkg/result/processors/exclude.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
var _ Processor = (*Exclude)(nil)
1313

14+
// Exclude filters reports only based on regular expressions applied to the report text.
1415
type Exclude struct {
1516
name string
1617

pkg/result/processors/exclude_rules.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ type excludeRule struct {
1515
baseRule
1616
}
1717

18+
// ExcludeRules filters reports based on multiple criteria:
19+
// - linter names (string)
20+
// - file path (regular expressions)
21+
// - text (regular expressions)
22+
// - code source (regular expressions)
23+
//
24+
// It uses the shortest relative paths and `path-prefix` option.
1825
type ExcludeRules struct {
1926
name string
2027

pkg/result/processors/filename_unadjuster.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ type adjustMap struct {
2222
m map[string]posMapper
2323
}
2424

25-
// FilenameUnadjuster is needed because a lot of linters use `fset.Position(f.Pos())` to get filename.
26-
// And they return adjusted filename (e.g.` *.qtpl`) for an issue.
25+
// FilenameUnadjuster fixes filename based on adjusted and unadjusted position (related to line directives and cgo).
26+
//
27+
// A lot of linters use `fset.Position(f.Pos())` to get filename,
28+
// and they return adjusted filename (e.g.` *.qtpl`) for an issue.
2729
// We need restore real `.go` filename to properly output it, parse it, etc.
2830
//
29-
// Require absolute filepath.
31+
// Require absolute file path.
3032
type FilenameUnadjuster struct {
3133
m map[string]posMapper // map from adjusted filename to position mapper: adjusted -> unadjusted position
3234
log logutils.Log

pkg/result/processors/fixer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ var _ Processor = (*Fixer)(nil)
3131

3232
const filePerm = 0644
3333

34+
// Fixer fixes reports if possible.
35+
// The reports that are not fixed are passed to the next processor.
3436
type Fixer struct {
3537
cfg *config.Config
3638
log logutils.Log

0 commit comments

Comments
 (0)