Skip to content

Commit c9d006d

Browse files
committed
don't import packages twice for golint
1 parent e6657e8 commit c9d006d

File tree

2 files changed

+15
-42
lines changed

2 files changed

+15
-42
lines changed

pkg/fsutils/fsutils.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ func (p ProjectPaths) MixedPaths() []string {
3131
return p.Files
3232
}
3333

34+
func (p ProjectPaths) FilesGrouppedByDirs() [][]string {
35+
dirToFiles := map[string][]string{}
36+
for _, f := range p.Files {
37+
dirToFiles[filepath.Dir(f)] = append(dirToFiles[f], f)
38+
}
39+
40+
ret := [][]string{}
41+
for _, files := range dirToFiles {
42+
ret = append(ret, files)
43+
}
44+
return ret
45+
}
46+
3447
func processPaths(root string, paths []string, maxPaths int) ([]string, error) {
3548
if len(paths) > maxPaths {
3649
logrus.Warnf("Gofmt: got too much paths (%d), analyze first %d", len(paths), maxPaths)

pkg/golinters/golint.go

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package golinters
33
import (
44
"context"
55
"fmt"
6-
"go/build"
76
"io/ioutil"
8-
"path/filepath"
97

108
"github.com/golang/lint"
119
"github.com/golangci/golangci-lint/pkg/result"
@@ -19,17 +17,8 @@ func (Golint) Name() string {
1917

2018
func (g Golint) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, error) {
2119
var issues []result.Issue
22-
if lintCtx.Paths.IsDirsRun {
23-
for _, path := range lintCtx.Paths.Dirs { // TODO: support exclusion of test files
24-
i, err := lintDir(path, lintCtx.RunCfg().Golint.MinConfidence)
25-
if err != nil {
26-
// TODO: skip and warn
27-
return nil, fmt.Errorf("can't lint dir %s: %s", path, err)
28-
}
29-
issues = append(issues, i...)
30-
}
31-
} else {
32-
i, err := lintFiles(lintCtx.RunCfg().Golint.MinConfidence, lintCtx.Paths.Files...)
20+
for _, pkgFiles := range lintCtx.Paths.FilesGrouppedByDirs() {
21+
i, err := lintFiles(lintCtx.RunCfg().Golint.MinConfidence, pkgFiles...)
3322
if err != nil {
3423
// TODO: skip and warn
3524
return nil, fmt.Errorf("can't lint files %s: %s", lintCtx.Paths.Files, err)
@@ -40,35 +29,6 @@ func (g Golint) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, erro
4029
return issues, nil
4130
}
4231

43-
func lintDir(dirname string, minConfidence float64) ([]result.Issue, error) {
44-
pkg, err := build.ImportDir(dirname, 0)
45-
if err != nil {
46-
if _, nogo := err.(*build.NoGoError); nogo {
47-
// Don't complain if the failure is due to no Go source files.
48-
return nil, nil
49-
}
50-
51-
return nil, fmt.Errorf("can't import dir %s", dirname)
52-
}
53-
54-
return lintImportedPackage(pkg, minConfidence)
55-
}
56-
57-
func lintImportedPackage(pkg *build.Package, minConfidence float64) ([]result.Issue, error) {
58-
var files []string
59-
files = append(files, pkg.GoFiles...)
60-
files = append(files, pkg.CgoFiles...)
61-
files = append(files, pkg.TestGoFiles...)
62-
files = append(files, pkg.XTestGoFiles...)
63-
if pkg.Dir != "." {
64-
for i, f := range files {
65-
files[i] = filepath.Join(pkg.Dir, f)
66-
}
67-
}
68-
69-
return lintFiles(minConfidence, files...)
70-
}
71-
7232
func lintFiles(minConfidence float64, filenames ...string) ([]result.Issue, error) {
7333
files := make(map[string][]byte)
7434
for _, filename := range filenames {

0 commit comments

Comments
 (0)