From c9d006d77aa886f8cdf159d23c2e700cac8f92a4 Mon Sep 17 00:00:00 2001 From: golangci Date: Tue, 8 May 2018 13:04:15 +0300 Subject: [PATCH] don't import packages twice for golint --- pkg/fsutils/fsutils.go | 13 ++++++++++++ pkg/golinters/golint.go | 44 ++--------------------------------------- 2 files changed, 15 insertions(+), 42 deletions(-) diff --git a/pkg/fsutils/fsutils.go b/pkg/fsutils/fsutils.go index 09100f5b..11db33c3 100644 --- a/pkg/fsutils/fsutils.go +++ b/pkg/fsutils/fsutils.go @@ -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) diff --git a/pkg/golinters/golint.go b/pkg/golinters/golint.go index 5e96a7db..dbf099fe 100644 --- a/pkg/golinters/golint.go +++ b/pkg/golinters/golint.go @@ -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 {