Skip to content

Commit b8dc1ca

Browse files
committed
refactor: create PathAbsoluter
1 parent 9b6c99c commit b8dc1ca

File tree

5 files changed

+70
-37
lines changed

5 files changed

+70
-37
lines changed

Diff for: pkg/lint/runner.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,15 @@ func NewRunner(log logutils.Log, cfg *config.Config, args []string, goenv *gouti
6868

6969
return &Runner{
7070
Processors: []processors.Processor{
71+
// Must be the first processor.
72+
processors.NewPathAbsoluter(log),
73+
7174
processors.NewCgo(goenv),
7275

73-
// Must go after Cgo.
76+
// Must be after Cgo.
7477
processors.NewFilenameUnadjuster(lintCtx.Packages, log.Child(logutils.DebugKeyFilenameUnadjuster)),
7578

76-
// Must go after FilenameUnadjuster.
79+
// Must be after FilenameUnadjuster.
7780
processors.NewInvalidIssue(log.Child(logutils.DebugKeyInvalidIssue)),
7881

7982
// Must be before diff, nolint and exclude autogenerated processor at least.

Diff for: pkg/logutils/logutils.go

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const (
3636
DebugKeyLoader = "loader" // Debugs packages loading (including `go/packages` internal debugging).
3737
DebugKeyMaxFromLinter = "max_from_linter"
3838
DebugKeyMaxSameIssues = "max_same_issues"
39+
DebugKeyPathAbsoluter = "path_absoluter"
3940
DebugKeyPkgCache = "pkgcache"
4041
DebugKeyRunner = "runner"
4142
DebugKeySeverityRules = "severity_rules"

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

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

33
import (
4-
"fmt"
54
"path/filepath"
65
"strings"
76

@@ -11,6 +10,10 @@ import (
1110

1211
var _ Processor = (*Cgo)(nil)
1312

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.
15+
//
16+
// Require absolute filepath.
1417
type Cgo struct {
1518
goCacheDir string
1619
}
@@ -21,32 +24,19 @@ func NewCgo(goenv *goutil.Env) *Cgo {
2124
}
2225
}
2326

24-
func (Cgo) Name() string {
27+
func (*Cgo) Name() string {
2528
return "cgo"
2629
}
2730

28-
func (p Cgo) Process(issues []result.Issue) ([]result.Issue, error) {
31+
func (p *Cgo) Process(issues []result.Issue) ([]result.Issue, error) {
2932
return filterIssuesErr(issues, p.shouldPassIssue)
3033
}
3134

32-
func (Cgo) Finish() {}
35+
func (*Cgo) Finish() {}
3336

34-
func (p Cgo) shouldPassIssue(issue *result.Issue) (bool, error) {
35-
// some linters (e.g. gosec, deadcode) return incorrect filepaths for cgo issues,
36-
// also cgo files have strange issues looking like false positives.
37-
38-
// cache dir contains all preprocessed files including cgo files
39-
40-
issueFilePath := issue.FilePath()
41-
if !filepath.IsAbs(issue.FilePath()) {
42-
absPath, err := filepath.Abs(issue.FilePath())
43-
if err != nil {
44-
return false, fmt.Errorf("failed to build abs path for %q: %w", issue.FilePath(), err)
45-
}
46-
issueFilePath = absPath
47-
}
48-
49-
if p.goCacheDir != "" && strings.HasPrefix(issueFilePath, p.goCacheDir) {
37+
func (p *Cgo) shouldPassIssue(issue *result.Issue) (bool, error) {
38+
// [p.goCacheDir] contains all preprocessed files including cgo files.
39+
if p.goCacheDir != "" && strings.HasPrefix(issue.FilePath(), p.goCacheDir) {
5040
return false, nil
5141
}
5242

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

+10-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package processors
33
import (
44
"go/parser"
55
"go/token"
6-
"path/filepath"
76
"strings"
87
"sync"
98
"time"
@@ -23,9 +22,11 @@ type adjustMap struct {
2322
m map[string]posMapper
2423
}
2524

26-
// FilenameUnadjuster is needed because a lot of linters use fset.Position(f.Pos())
27-
// to get filename. And they return adjusted filename (e.g. *.qtpl) for an issue. We need
28-
// restore real .go filename to properly output it, parse it, etc.
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.
27+
// We need restore real `.go` filename to properly output it, parse it, etc.
28+
//
29+
// Require absolute filepath.
2930
type FilenameUnadjuster struct {
3031
m map[string]posMapper // map from adjusted filename to position mapper: adjusted -> unadjusted position
3132
log logutils.Log
@@ -36,16 +37,20 @@ func NewFilenameUnadjuster(pkgs []*packages.Package, log logutils.Log) *Filename
3637
m := adjustMap{m: map[string]posMapper{}}
3738

3839
startedAt := time.Now()
40+
3941
var wg sync.WaitGroup
4042
wg.Add(len(pkgs))
43+
4144
for _, pkg := range pkgs {
4245
go func(pkg *packages.Package) {
4346
// It's important to call func here to run GC
4447
processUnadjusterPkg(&m, pkg, log)
4548
wg.Done()
4649
}(pkg)
4750
}
51+
4852
wg.Wait()
53+
4954
log.Infof("Pre-built %d adjustments in %s", len(m.m), time.Since(startedAt))
5055

5156
return &FilenameUnadjuster{
@@ -61,17 +66,7 @@ func (*FilenameUnadjuster) Name() string {
6166

6267
func (p *FilenameUnadjuster) Process(issues []result.Issue) ([]result.Issue, error) {
6368
return transformIssues(issues, func(issue *result.Issue) *result.Issue {
64-
issueFilePath := issue.FilePath()
65-
if !filepath.IsAbs(issue.FilePath()) {
66-
absPath, err := filepath.Abs(issue.FilePath())
67-
if err != nil {
68-
p.log.Warnf("failed to build abs path for %q: %s", issue.FilePath(), err)
69-
return issue
70-
}
71-
issueFilePath = absPath
72-
}
73-
74-
mapper := p.m[issueFilePath]
69+
mapper := p.m[issue.FilePath()]
7570
if mapper == nil {
7671
return issue
7772
}

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

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package processors
2+
3+
import (
4+
"path/filepath"
5+
6+
"github.com/golangci/golangci-lint/pkg/logutils"
7+
"github.com/golangci/golangci-lint/pkg/result"
8+
)
9+
10+
var _ Processor = (*PathAbsoluter)(nil)
11+
12+
// PathAbsoluter ensures that representation of path are absolute.
13+
type PathAbsoluter struct {
14+
log logutils.Log
15+
}
16+
17+
func NewPathAbsoluter(log logutils.Log) *PathAbsoluter {
18+
return &PathAbsoluter{log: log.Child(logutils.DebugKeyPathAbsoluter)}
19+
}
20+
21+
func (*PathAbsoluter) Name() string {
22+
return "path_absoluter"
23+
}
24+
25+
func (p *PathAbsoluter) Process(issues []result.Issue) ([]result.Issue, error) {
26+
return transformIssues(issues, func(issue *result.Issue) *result.Issue {
27+
if filepath.IsAbs(issue.FilePath()) {
28+
return issue
29+
}
30+
31+
absPath, err := filepath.Abs(issue.FilePath())
32+
if err != nil {
33+
p.log.Warnf("failed to get absolute path for %q: %v", issue.FilePath(), err)
34+
return nil
35+
}
36+
37+
newIssue := issue
38+
newIssue.Pos.Filename = absPath
39+
40+
return newIssue
41+
}), nil
42+
}
43+
44+
func (*PathAbsoluter) Finish() {}

0 commit comments

Comments
 (0)