2018-05-31 23:53:01 +03:00
|
|
|
package lint
|
2018-05-05 07:45:06 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-05-06 19:08:34 +03:00
|
|
|
"runtime/debug"
|
2018-05-19 16:18:23 +03:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2018-05-06 12:08:57 +03:00
|
|
|
"sync"
|
2018-05-06 19:08:34 +03:00
|
|
|
"time"
|
2018-05-05 07:45:06 +03:00
|
|
|
|
2019-09-17 08:42:16 +03:00
|
|
|
"github.com/golangci/golangci-lint/internal/errorutil"
|
2019-03-17 22:47:29 +03:00
|
|
|
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
|
|
|
|
|
2019-03-17 14:52:04 +03:00
|
|
|
"github.com/golangci/golangci-lint/pkg/fsutils"
|
|
|
|
|
2018-06-13 22:37:48 +03:00
|
|
|
"github.com/golangci/golangci-lint/pkg/config"
|
2018-10-21 17:06:51 +03:00
|
|
|
"github.com/golangci/golangci-lint/pkg/goutil"
|
2018-06-13 22:37:48 +03:00
|
|
|
"github.com/golangci/golangci-lint/pkg/lint/astcache"
|
2018-06-02 11:36:50 +03:00
|
|
|
"github.com/golangci/golangci-lint/pkg/lint/linter"
|
2018-06-08 08:43:43 +03:00
|
|
|
"github.com/golangci/golangci-lint/pkg/logutils"
|
2018-10-21 17:06:51 +03:00
|
|
|
"github.com/golangci/golangci-lint/pkg/packages"
|
2018-05-05 07:45:06 +03:00
|
|
|
"github.com/golangci/golangci-lint/pkg/result"
|
|
|
|
"github.com/golangci/golangci-lint/pkg/result/processors"
|
2018-05-19 16:18:23 +03:00
|
|
|
"github.com/golangci/golangci-lint/pkg/timeutils"
|
2018-05-05 07:45:06 +03:00
|
|
|
)
|
|
|
|
|
2018-06-13 22:37:48 +03:00
|
|
|
type Runner struct {
|
2018-05-05 07:45:06 +03:00
|
|
|
Processors []processors.Processor
|
2018-06-13 22:37:48 +03:00
|
|
|
Log logutils.Log
|
|
|
|
}
|
|
|
|
|
2019-03-17 14:52:04 +03:00
|
|
|
func NewRunner(astCache *astcache.Cache, cfg *config.Config, log logutils.Log, goenv *goutil.Env,
|
2019-03-17 22:47:29 +03:00
|
|
|
lineCache *fsutils.LineCache, dbManager *lintersdb.Manager) (*Runner, error) {
|
2018-06-13 22:37:48 +03:00
|
|
|
icfg := cfg.Issues
|
|
|
|
excludePatterns := icfg.ExcludePatterns
|
|
|
|
if icfg.UseDefaultExcludes {
|
|
|
|
excludePatterns = append(excludePatterns, config.GetDefaultExcludePatternsStrings()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
var excludeTotalPattern string
|
|
|
|
if len(excludePatterns) != 0 {
|
|
|
|
excludeTotalPattern = fmt.Sprintf("(%s)", strings.Join(excludePatterns, "|"))
|
|
|
|
}
|
|
|
|
|
|
|
|
skipFilesProcessor, err := processors.NewSkipFiles(cfg.Run.SkipFiles)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-10 01:14:25 -07:00
|
|
|
skipDirs := cfg.Run.SkipDirs
|
|
|
|
if cfg.Run.UseDefaultSkipDirs {
|
|
|
|
skipDirs = append(skipDirs, packages.StdExcludeDirRegexps...)
|
|
|
|
}
|
2018-10-21 17:06:51 +03:00
|
|
|
skipDirsProcessor, err := processors.NewSkipDirs(skipDirs, log.Child("skip dirs"), cfg.Run.Args)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-09 03:24:30 +03:00
|
|
|
var excludeRules []processors.ExcludeRule
|
|
|
|
for _, r := range icfg.ExcludeRules {
|
|
|
|
excludeRules = append(excludeRules, processors.ExcludeRule{
|
|
|
|
Text: r.Text,
|
2019-03-17 14:52:04 +03:00
|
|
|
Source: r.Source,
|
2019-02-09 03:24:30 +03:00
|
|
|
Path: r.Path,
|
|
|
|
Linters: r.Linters,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-13 22:37:48 +03:00
|
|
|
return &Runner{
|
|
|
|
Processors: []processors.Processor{
|
2018-10-21 17:06:51 +03:00
|
|
|
processors.NewCgo(goenv),
|
2019-04-20 21:14:28 +03:00
|
|
|
processors.NewFilenameUnadjuster(astCache, log.Child("filename_unadjuster")), // must go after Cgo
|
|
|
|
processors.NewPathPrettifier(), // must be before diff, nolint and exclude autogenerated processor at least
|
2018-06-13 22:37:48 +03:00
|
|
|
skipFilesProcessor,
|
2018-12-22 12:11:37 +03:00
|
|
|
skipDirsProcessor, // must be after path prettifier
|
2018-06-13 22:37:48 +03:00
|
|
|
|
|
|
|
processors.NewAutogeneratedExclude(astCache),
|
2019-09-14 20:15:11 +03:00
|
|
|
processors.NewIdentifierMarker(), // must be before exclude because users see already marked output and configure excluding by it
|
2018-06-13 22:37:48 +03:00
|
|
|
processors.NewExclude(excludeTotalPattern),
|
2019-03-17 14:52:04 +03:00
|
|
|
processors.NewExcludeRules(excludeRules, lineCache, log.Child("exclude_rules")),
|
2019-03-17 22:47:29 +03:00
|
|
|
processors.NewNolint(astCache, log.Child("nolint"), dbManager),
|
2018-06-13 22:37:48 +03:00
|
|
|
|
2019-06-09 17:30:45 +03:00
|
|
|
processors.NewUniqByLine(cfg),
|
2018-06-13 22:37:48 +03:00
|
|
|
processors.NewDiff(icfg.Diff, icfg.DiffFromRevision, icfg.DiffPatchFilePath),
|
2019-02-16 21:23:57 +03:00
|
|
|
processors.NewMaxPerFileFromLinter(cfg),
|
|
|
|
processors.NewMaxSameIssues(icfg.MaxSameIssues, log.Child("max_same_issues"), cfg),
|
|
|
|
processors.NewMaxFromLinter(icfg.MaxIssuesPerLinter, log.Child("max_from_linter"), cfg),
|
2019-03-17 14:52:04 +03:00
|
|
|
processors.NewSourceCode(lineCache, log.Child("source_code")),
|
2018-10-21 17:06:51 +03:00
|
|
|
processors.NewPathShortener(),
|
2018-06-13 22:37:48 +03:00
|
|
|
},
|
|
|
|
Log: log,
|
|
|
|
}, nil
|
2018-05-05 07:45:06 +03:00
|
|
|
}
|
|
|
|
|
2018-05-06 12:08:57 +03:00
|
|
|
type lintRes struct {
|
2019-01-03 11:33:05 +01:00
|
|
|
linter *linter.Config
|
2018-05-06 12:08:57 +03:00
|
|
|
err error
|
2018-05-07 21:44:40 +03:00
|
|
|
issues []result.Issue
|
2018-05-06 12:08:57 +03:00
|
|
|
}
|
|
|
|
|
2019-01-03 11:33:05 +01:00
|
|
|
func (r *Runner) runLinterSafe(ctx context.Context, lintCtx *linter.Context,
|
|
|
|
lc *linter.Config) (ret []result.Issue, err error) {
|
2018-05-06 14:51:06 +03:00
|
|
|
defer func() {
|
|
|
|
if panicData := recover(); panicData != nil {
|
2019-09-17 08:42:16 +03:00
|
|
|
if pe, ok := panicData.(*errorutil.PanicError); ok {
|
|
|
|
// Don't print stacktrace from goroutines twice
|
|
|
|
lintCtx.Log.Warnf("Panic: %s: %s", pe, pe.Stack())
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("panic occurred: %s", panicData)
|
|
|
|
r.Log.Warnf("Panic stack trace: %s", debug.Stack())
|
|
|
|
}
|
2018-05-06 14:51:06 +03:00
|
|
|
}
|
|
|
|
}()
|
2018-05-07 21:44:40 +03:00
|
|
|
|
2018-06-13 22:37:48 +03:00
|
|
|
specificLintCtx := *lintCtx
|
2018-09-01 14:16:30 +03:00
|
|
|
specificLintCtx.Log = r.Log.Child(lc.Name())
|
2018-06-13 22:37:48 +03:00
|
|
|
issues, err := lc.Linter.Run(ctx, &specificLintCtx)
|
2018-06-02 11:36:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range issues {
|
2018-09-01 14:16:30 +03:00
|
|
|
i.FromLinter = lc.Name()
|
2018-06-02 11:36:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return issues, nil
|
2018-05-06 14:51:06 +03:00
|
|
|
}
|
|
|
|
|
2018-06-28 22:39:23 +03:00
|
|
|
func (r Runner) runWorker(ctx context.Context, lintCtx *linter.Context,
|
2019-01-03 11:33:05 +01:00
|
|
|
tasksCh <-chan *linter.Config, lintResultsCh chan<- lintRes, name string) {
|
2018-06-13 22:37:48 +03:00
|
|
|
sw := timeutils.NewStopwatch(name, r.Log)
|
2018-05-19 16:18:23 +03:00
|
|
|
defer sw.Print()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2018-06-02 11:36:50 +03:00
|
|
|
case lc, ok := <-tasksCh:
|
2018-05-19 16:18:23 +03:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
// XXX: if check it in only int a select
|
|
|
|
// it's possible to not enter to this case until tasksCh is empty.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var issues []result.Issue
|
|
|
|
var err error
|
2018-09-01 14:16:30 +03:00
|
|
|
sw.TrackStage(lc.Name(), func() {
|
2018-06-13 22:37:48 +03:00
|
|
|
issues, err = r.runLinterSafe(ctx, lintCtx, lc)
|
2018-05-19 16:18:23 +03:00
|
|
|
})
|
|
|
|
lintResultsCh <- lintRes{
|
2018-06-02 11:36:50 +03:00
|
|
|
linter: lc,
|
2018-05-19 16:18:23 +03:00
|
|
|
err: err,
|
|
|
|
issues: issues,
|
2018-05-06 12:08:57 +03:00
|
|
|
}
|
2018-05-19 16:18:23 +03:00
|
|
|
}
|
2018-05-06 12:08:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-13 22:37:48 +03:00
|
|
|
func (r Runner) logWorkersStat(workersFinishTimes []time.Time) {
|
2018-05-19 16:18:23 +03:00
|
|
|
lastFinishTime := workersFinishTimes[0]
|
|
|
|
for _, t := range workersFinishTimes {
|
|
|
|
if t.After(lastFinishTime) {
|
|
|
|
lastFinishTime = t
|
2018-05-07 21:44:40 +03:00
|
|
|
}
|
2018-05-19 16:18:23 +03:00
|
|
|
}
|
2018-05-07 21:44:40 +03:00
|
|
|
|
2018-05-19 16:18:23 +03:00
|
|
|
logStrings := []string{}
|
|
|
|
for i, t := range workersFinishTimes {
|
|
|
|
if t.Equal(lastFinishTime) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
logStrings = append(logStrings, fmt.Sprintf("#%d: %s", i+1, lastFinishTime.Sub(t)))
|
|
|
|
}
|
|
|
|
|
2018-06-13 22:37:48 +03:00
|
|
|
r.Log.Infof("Workers idle times: %s", strings.Join(logStrings, ", "))
|
2018-05-07 21:44:40 +03:00
|
|
|
}
|
|
|
|
|
2019-01-03 11:33:05 +01:00
|
|
|
func getSortedLintersConfigs(linters []*linter.Config) []*linter.Config {
|
|
|
|
ret := make([]*linter.Config, len(linters))
|
2018-05-31 23:53:01 +03:00
|
|
|
copy(ret, linters)
|
2018-05-05 22:22:21 +03:00
|
|
|
|
2018-05-19 16:18:23 +03:00
|
|
|
sort.Slice(ret, func(i, j int) bool {
|
2018-05-31 23:53:01 +03:00
|
|
|
return ret[i].GetSpeed() < ret[j].GetSpeed()
|
2018-05-19 16:18:23 +03:00
|
|
|
})
|
2018-05-06 12:08:57 +03:00
|
|
|
|
2018-05-19 16:18:23 +03:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-01-03 11:33:05 +01:00
|
|
|
func (r *Runner) runWorkers(ctx context.Context, lintCtx *linter.Context, linters []*linter.Config) <-chan lintRes {
|
|
|
|
tasksCh := make(chan *linter.Config, len(linters))
|
2018-05-19 16:18:23 +03:00
|
|
|
lintResultsCh := make(chan lintRes, len(linters))
|
2018-05-06 12:08:57 +03:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2018-05-19 16:18:23 +03:00
|
|
|
workersFinishTimes := make([]time.Time, lintCtx.Cfg.Run.Concurrency)
|
|
|
|
|
|
|
|
for i := 0; i < lintCtx.Cfg.Run.Concurrency; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(i int) {
|
|
|
|
defer wg.Done()
|
|
|
|
name := fmt.Sprintf("worker.%d", i+1)
|
2018-06-13 22:37:48 +03:00
|
|
|
r.runWorker(ctx, lintCtx, tasksCh, lintResultsCh, name)
|
2018-05-19 16:18:23 +03:00
|
|
|
workersFinishTimes[i] = time.Now()
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
lcs := getSortedLintersConfigs(linters)
|
|
|
|
for _, lc := range lcs {
|
2018-06-02 11:36:50 +03:00
|
|
|
tasksCh <- lc
|
2018-05-06 12:08:57 +03:00
|
|
|
}
|
|
|
|
close(tasksCh)
|
|
|
|
|
2018-05-07 21:44:40 +03:00
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(lintResultsCh)
|
2018-05-19 16:18:23 +03:00
|
|
|
|
2018-06-13 22:37:48 +03:00
|
|
|
r.logWorkersStat(workersFinishTimes)
|
2018-05-07 21:44:40 +03:00
|
|
|
}()
|
|
|
|
|
2018-05-19 16:18:23 +03:00
|
|
|
return lintResultsCh
|
|
|
|
}
|
|
|
|
|
2019-09-14 20:15:11 +03:00
|
|
|
type processorStat struct {
|
|
|
|
inCount int
|
|
|
|
outCount int
|
|
|
|
}
|
|
|
|
|
2018-06-29 00:12:30 +03:00
|
|
|
func (r Runner) processLintResults(inCh <-chan lintRes) <-chan lintRes {
|
2018-05-19 16:18:23 +03:00
|
|
|
outCh := make(chan lintRes, 64)
|
|
|
|
|
|
|
|
go func() {
|
2018-06-13 22:37:48 +03:00
|
|
|
sw := timeutils.NewStopwatch("processing", r.Log)
|
2018-05-19 16:18:23 +03:00
|
|
|
|
2018-12-22 12:11:37 +03:00
|
|
|
var issuesBefore, issuesAfter int
|
2019-09-14 20:15:11 +03:00
|
|
|
statPerProcessor := map[string]processorStat{}
|
2018-05-19 16:18:23 +03:00
|
|
|
defer close(outCh)
|
|
|
|
|
|
|
|
for res := range inCh {
|
|
|
|
if res.err != nil {
|
2018-09-01 14:16:30 +03:00
|
|
|
r.Log.Warnf("Can't run linter %s: %s", res.linter.Name(), res.err)
|
2018-05-19 16:18:23 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(res.issues) != 0 {
|
2018-12-22 12:11:37 +03:00
|
|
|
issuesBefore += len(res.issues)
|
2019-09-14 20:15:11 +03:00
|
|
|
res.issues = r.processIssues(res.issues, sw, statPerProcessor)
|
2018-12-22 12:11:37 +03:00
|
|
|
issuesAfter += len(res.issues)
|
2018-05-19 16:18:23 +03:00
|
|
|
outCh <- res
|
|
|
|
}
|
2018-05-05 07:45:06 +03:00
|
|
|
}
|
|
|
|
|
2018-05-19 16:18:23 +03:00
|
|
|
// finalize processors: logging, clearing, no heavy work here
|
2018-05-08 17:13:16 +03:00
|
|
|
|
2018-05-19 16:18:23 +03:00
|
|
|
for _, p := range r.Processors {
|
2018-11-05 22:29:45 +03:00
|
|
|
p := p
|
2018-05-19 16:18:23 +03:00
|
|
|
sw.TrackStage(p.Name(), func() {
|
|
|
|
p.Finish()
|
|
|
|
})
|
2018-05-08 17:13:16 +03:00
|
|
|
}
|
|
|
|
|
2018-12-22 12:11:37 +03:00
|
|
|
if issuesBefore != issuesAfter {
|
|
|
|
r.Log.Infof("Issues before processing: %d, after processing: %d", issuesBefore, issuesAfter)
|
|
|
|
}
|
2019-09-14 20:15:11 +03:00
|
|
|
r.printPerProcessorStat(statPerProcessor)
|
2018-05-19 16:18:23 +03:00
|
|
|
sw.PrintStages()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return outCh
|
|
|
|
}
|
|
|
|
|
2019-09-14 20:15:11 +03:00
|
|
|
func (r Runner) printPerProcessorStat(stat map[string]processorStat) {
|
|
|
|
parts := make([]string, 0, len(stat))
|
|
|
|
for name, ps := range stat {
|
|
|
|
if ps.inCount != 0 {
|
|
|
|
parts = append(parts, fmt.Sprintf("%s: %d/%d", name, ps.outCount, ps.inCount))
|
|
|
|
}
|
|
|
|
}
|
2019-09-17 08:42:16 +03:00
|
|
|
if len(parts) != 0 {
|
|
|
|
r.Log.Infof("Processors filtering stat (out/in): %s", strings.Join(parts, ", "))
|
|
|
|
}
|
2019-09-14 20:15:11 +03:00
|
|
|
}
|
|
|
|
|
2018-06-29 00:12:30 +03:00
|
|
|
func collectIssues(resCh <-chan lintRes) <-chan result.Issue {
|
2018-05-19 16:18:23 +03:00
|
|
|
retIssues := make(chan result.Issue, 1024)
|
|
|
|
go func() {
|
|
|
|
defer close(retIssues)
|
|
|
|
|
|
|
|
for res := range resCh {
|
|
|
|
if len(res.issues) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range res.issues {
|
|
|
|
retIssues <- i
|
|
|
|
}
|
2018-05-05 07:45:06 +03:00
|
|
|
}
|
2018-05-19 16:18:23 +03:00
|
|
|
}()
|
2018-05-05 07:45:06 +03:00
|
|
|
|
2018-05-19 16:18:23 +03:00
|
|
|
return retIssues
|
|
|
|
}
|
|
|
|
|
2019-01-03 11:33:05 +01:00
|
|
|
func (r Runner) Run(ctx context.Context, linters []*linter.Config, lintCtx *linter.Context) <-chan result.Issue {
|
2018-05-19 16:18:23 +03:00
|
|
|
lintResultsCh := r.runWorkers(ctx, lintCtx, linters)
|
2018-06-29 00:12:30 +03:00
|
|
|
processedLintResultsCh := r.processLintResults(lintResultsCh)
|
2018-05-07 16:38:05 +03:00
|
|
|
if ctx.Err() != nil {
|
2018-06-28 21:27:07 +03:00
|
|
|
// XXX: always process issues, even if timeout occurred
|
2018-05-19 16:18:23 +03:00
|
|
|
finishedLintersN := 0
|
|
|
|
for range processedLintResultsCh {
|
|
|
|
finishedLintersN++
|
|
|
|
}
|
|
|
|
|
2018-10-21 17:06:51 +03:00
|
|
|
r.Log.Errorf("%d/%d linters finished: deadline exceeded",
|
2018-05-19 16:18:23 +03:00
|
|
|
finishedLintersN, len(linters))
|
2018-05-07 16:38:05 +03:00
|
|
|
}
|
|
|
|
|
2018-06-29 00:12:30 +03:00
|
|
|
return collectIssues(processedLintResultsCh)
|
2018-05-05 07:45:06 +03:00
|
|
|
}
|
|
|
|
|
2019-09-14 20:15:11 +03:00
|
|
|
func (r *Runner) processIssues(issues []result.Issue, sw *timeutils.Stopwatch, statPerProcessor map[string]processorStat) []result.Issue {
|
2018-05-05 07:45:06 +03:00
|
|
|
for _, p := range r.Processors {
|
2018-05-19 16:18:23 +03:00
|
|
|
var newIssues []result.Issue
|
|
|
|
var err error
|
2018-11-05 22:29:45 +03:00
|
|
|
p := p
|
2018-05-19 16:18:23 +03:00
|
|
|
sw.TrackStage(p.Name(), func() {
|
|
|
|
newIssues, err = p.Process(issues)
|
|
|
|
})
|
|
|
|
|
2018-05-05 07:45:06 +03:00
|
|
|
if err != nil {
|
2018-06-13 22:37:48 +03:00
|
|
|
r.Log.Warnf("Can't process result by %s processor: %s", p.Name(), err)
|
2018-05-07 09:09:10 +03:00
|
|
|
} else {
|
2019-09-14 20:15:11 +03:00
|
|
|
stat := statPerProcessor[p.Name()]
|
|
|
|
stat.inCount += len(issues)
|
|
|
|
stat.outCount += len(newIssues)
|
|
|
|
statPerProcessor[p.Name()] = stat
|
2018-05-07 21:44:40 +03:00
|
|
|
issues = newIssues
|
|
|
|
}
|
2018-05-19 16:18:23 +03:00
|
|
|
|
2018-05-07 21:44:40 +03:00
|
|
|
if issues == nil {
|
|
|
|
issues = []result.Issue{}
|
2018-05-05 07:45:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return issues
|
|
|
|
}
|