don't import packages twice for golint

This commit is contained in:
golangci 2018-05-08 13:04:15 +03:00
parent e6657e868e
commit c9d006d77a
2 changed files with 15 additions and 42 deletions

View File

@ -31,6 +31,19 @@ func (p ProjectPaths) MixedPaths() []string {
return p.Files
}
func (p ProjectPaths) FilesGrouppedByDirs() [][]string {
dirToFiles := map[string][]string{}
for _, f := range p.Files {
dirToFiles[filepath.Dir(f)] = append(dirToFiles[f], f)
}
ret := [][]string{}
for _, files := range dirToFiles {
ret = append(ret, files)
}
return ret
}
func processPaths(root string, paths []string, maxPaths int) ([]string, error) {
if len(paths) > maxPaths {
logrus.Warnf("Gofmt: got too much paths (%d), analyze first %d", len(paths), maxPaths)

View File

@ -3,9 +3,7 @@ package golinters
import (
"context"
"fmt"
"go/build"
"io/ioutil"
"path/filepath"
"github.com/golang/lint"
"github.com/golangci/golangci-lint/pkg/result"
@ -19,17 +17,8 @@ func (Golint) Name() string {
func (g Golint) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, error) {
var issues []result.Issue
if lintCtx.Paths.IsDirsRun {
for _, path := range lintCtx.Paths.Dirs { // TODO: support exclusion of test files
i, err := lintDir(path, lintCtx.RunCfg().Golint.MinConfidence)
if err != nil {
// TODO: skip and warn
return nil, fmt.Errorf("can't lint dir %s: %s", path, err)
}
issues = append(issues, i...)
}
} else {
i, err := lintFiles(lintCtx.RunCfg().Golint.MinConfidence, lintCtx.Paths.Files...)
for _, pkgFiles := range lintCtx.Paths.FilesGrouppedByDirs() {
i, err := lintFiles(lintCtx.RunCfg().Golint.MinConfidence, pkgFiles...)
if err != nil {
// TODO: skip and warn
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
return issues, nil
}
func lintDir(dirname string, minConfidence float64) ([]result.Issue, error) {
pkg, err := build.ImportDir(dirname, 0)
if err != nil {
if _, nogo := err.(*build.NoGoError); nogo {
// Don't complain if the failure is due to no Go source files.
return nil, nil
}
return nil, fmt.Errorf("can't import dir %s", dirname)
}
return lintImportedPackage(pkg, minConfidence)
}
func lintImportedPackage(pkg *build.Package, minConfidence float64) ([]result.Issue, error) {
var files []string
files = append(files, pkg.GoFiles...)
files = append(files, pkg.CgoFiles...)
files = append(files, pkg.TestGoFiles...)
files = append(files, pkg.XTestGoFiles...)
if pkg.Dir != "." {
for i, f := range files {
files[i] = filepath.Join(pkg.Dir, f)
}
}
return lintFiles(minConfidence, files...)
}
func lintFiles(minConfidence float64, filenames ...string) ([]result.Issue, error) {
files := make(map[string][]byte)
for _, filename := range filenames {