golangci-lint/pkg/runner.go

161 lines
3.9 KiB
Go
Raw Normal View History

2018-05-05 19:43:18 +03:00
package pkg
import (
"context"
"fmt"
2018-05-05 22:22:21 +03:00
"os"
2018-05-06 19:08:34 +03:00
"runtime/debug"
2018-05-06 12:08:57 +03:00
"sync"
2018-05-06 19:08:34 +03:00
"time"
2018-05-06 19:08:34 +03:00
"github.com/golangci/golangci-lint/pkg/golinters"
"github.com/golangci/golangci-lint/pkg/result"
"github.com/golangci/golangci-lint/pkg/result/processors"
2018-05-07 21:44:40 +03:00
"github.com/sirupsen/logrus"
)
type SimpleRunner struct {
Processors []processors.Processor
}
2018-05-06 12:08:57 +03:00
type lintRes struct {
linter Linter
err error
2018-05-07 21:44:40 +03:00
issues []result.Issue
2018-05-06 12:08:57 +03:00
}
2018-05-07 21:44:40 +03:00
func (r *SimpleRunner) runLinter(ctx context.Context, linter Linter, lintCtx *golinters.Context, i int) (res []result.Issue, err error) {
2018-05-06 14:51:06 +03:00
defer func() {
if panicData := recover(); panicData != nil {
err = fmt.Errorf("panic occured: %s", panicData)
2018-05-07 21:44:40 +03:00
logrus.Infof("Panic stack trace: %s", debug.Stack())
2018-05-06 14:51:06 +03:00
}
}()
2018-05-06 19:08:34 +03:00
startedAt := time.Now()
res, err = linter.Run(ctx, lintCtx)
2018-05-07 21:44:40 +03:00
if err == nil && len(res) != 0 {
res = r.processIssues(ctx, res)
}
logrus.Infof("worker #%d: linter %s took %s and found %d issues", i, linter.Name(),
time.Since(startedAt), len(res))
2018-05-06 14:51:06 +03:00
return
}
2018-05-07 21:44:40 +03:00
func (r *SimpleRunner) runLinters(ctx context.Context, wg *sync.WaitGroup, tasksCh chan Linter, lintResultsCh chan lintRes, lintCtx *golinters.Context, workersCount int) {
for i := 0; i < workersCount; i++ {
2018-05-06 19:08:34 +03:00
go func(i int) {
2018-05-06 12:08:57 +03:00
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case linter, ok := <-tasksCh:
if !ok {
return
}
2018-05-07 16:38:05 +03:00
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
}
2018-05-07 21:44:40 +03:00
issues, lerr := r.runLinter(ctx, linter, lintCtx, i)
2018-05-06 12:08:57 +03:00
lintResultsCh <- lintRes{
linter: linter,
err: lerr,
2018-05-07 21:44:40 +03:00
issues: issues,
2018-05-06 12:08:57 +03:00
}
}
}
2018-05-06 19:08:34 +03:00
}(i + 1)
2018-05-06 12:08:57 +03:00
}
}
2018-05-07 21:44:40 +03:00
func (r SimpleRunner) Run(ctx context.Context, linters []Linter, lintCtx *golinters.Context) chan result.Issue {
retIssues := make(chan result.Issue, 1024)
go func() {
defer close(retIssues)
if err := r.runGo(ctx, linters, lintCtx, retIssues); err != nil {
logrus.Warnf("error running linters: %s", err)
}
}()
return retIssues
}
func (r SimpleRunner) runGo(ctx context.Context, linters []Linter, lintCtx *golinters.Context, retIssues chan result.Issue) error {
2018-05-05 22:22:21 +03:00
savedStdout, savedStderr := os.Stdout, os.Stderr
devNull, err := os.Open(os.DevNull)
if err != nil {
2018-05-07 21:44:40 +03:00
return fmt.Errorf("can't open null device %q: %s", os.DevNull, err)
2018-05-05 22:22:21 +03:00
}
2018-05-07 21:44:40 +03:00
// Don't allow linters to print anything
2018-05-06 12:08:57 +03:00
os.Stdout, os.Stderr = devNull, devNull
lintResultsCh := make(chan lintRes, len(linters))
2018-05-07 16:38:05 +03:00
tasksCh := make(chan Linter, len(linters))
2018-05-07 21:44:40 +03:00
workersCount := lintCtx.Cfg.Common.Concurrency - 1
2018-05-06 12:08:57 +03:00
var wg sync.WaitGroup
2018-05-07 21:44:40 +03:00
wg.Add(workersCount)
r.runLinters(ctx, &wg, tasksCh, lintResultsCh, lintCtx, workersCount)
2018-05-06 12:08:57 +03:00
for _, linter := range linters {
2018-05-06 12:08:57 +03:00
tasksCh <- linter
}
close(tasksCh)
2018-05-07 21:44:40 +03:00
go func() {
wg.Wait()
close(lintResultsCh)
os.Stdout, os.Stderr = savedStdout, savedStderr
}()
2018-05-07 16:38:05 +03:00
finishedN := 0
2018-05-06 12:08:57 +03:00
for res := range lintResultsCh {
if res.err != nil {
2018-05-07 21:44:40 +03:00
logrus.Warnf("Can't run linter %s: %s", res.linter.Name(), res.err)
continue
}
2018-05-07 16:38:05 +03:00
finishedN++
2018-05-07 21:44:40 +03:00
for _, i := range res.issues {
retIssues <- i
}
}
// finalize processors: logging, clearing, no heavy work here
for _, p := range r.Processors {
p.Finish()
}
2018-05-07 16:38:05 +03:00
if ctx.Err() != nil {
2018-05-07 21:44:40 +03:00
return fmt.Errorf("%d/%d linters finished: deadline exceeded: try increase it by passing --deadline option",
2018-05-07 16:38:05 +03:00
finishedN, len(linters))
}
2018-05-07 21:44:40 +03:00
return nil
}
2018-05-07 21:44:40 +03:00
func (r *SimpleRunner) processIssues(ctx context.Context, issues []result.Issue) []result.Issue {
for _, p := range r.Processors {
2018-05-06 22:58:04 +03:00
startedAt := time.Now()
2018-05-07 21:44:40 +03:00
newIssues, err := p.Process(issues)
2018-05-06 22:58:04 +03:00
elapsed := time.Since(startedAt)
if elapsed > 50*time.Millisecond {
2018-05-07 21:44:40 +03:00
logrus.Infof("Result processor %s took %s", p.Name(), elapsed)
2018-05-06 22:58:04 +03:00
}
if err != nil {
2018-05-07 21:44:40 +03:00
logrus.Warnf("Can't process result by %s processor: %s", p.Name(), err)
2018-05-07 09:09:10 +03:00
} else {
2018-05-07 21:44:40 +03:00
issues = newIssues
}
if issues == nil {
issues = []result.Issue{}
}
}
return issues
}