Skip to content

Commit 4853151

Browse files
committed
async issues processing
1 parent c9bb265 commit 4853151

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+596
-3495
lines changed

Gopkg.lock

Lines changed: 1 addition & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@
2929
name = "github.com/bradleyfalzon/revgrep"
3030
version = "0.3.0"
3131

32-
[[constraint]]
33-
branch = "master"
34-
name = "github.com/golangci/golangci-shared"
35-
3632
[[constraint]]
3733
name = "github.com/stretchr/testify"
3834
version = "1.2.1"

cmd/golangci-lint/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import (
44
"log"
55

66
"github.com/golangci/golangci-lint/internal/commands"
7-
"github.com/golangci/golangci-shared/pkg/analytics"
87
"github.com/sirupsen/logrus"
98
)
109

1110
func main() {
1211
log.SetFlags(0) // don't print time
13-
analytics.SetLogLevel(logrus.WarnLevel)
12+
logrus.SetLevel(logrus.WarnLevel)
1413

1514
e := commands.NewExecutor()
1615
if err := e.Execute(); err != nil {

internal/commands/root.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"runtime"
77
"runtime/pprof"
88

9-
"github.com/golangci/golangci-shared/pkg/analytics"
109
"github.com/sirupsen/logrus"
1110
"github.com/spf13/cobra"
1211
)
@@ -23,7 +22,7 @@ func (e *Executor) initRoot() {
2322
runtime.GOMAXPROCS(e.cfg.Common.Concurrency)
2423

2524
if e.cfg.Common.IsVerbose {
26-
analytics.SetLogLevel(logrus.InfoLevel)
25+
logrus.SetLevel(logrus.InfoLevel)
2726
}
2827

2928
if e.cfg.Common.CPUProfilePath != "" {

internal/commands/run.go

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"fmt"
77
"go/build"
88
"log"
9+
"os"
910
"strings"
11+
"syscall"
1012
"time"
1113

1214
"github.com/fatih/color"
@@ -18,7 +20,7 @@ import (
1820
"github.com/golangci/golangci-lint/pkg/golinters"
1921
"github.com/golangci/golangci-lint/pkg/result"
2022
"github.com/golangci/golangci-lint/pkg/result/processors"
21-
"github.com/golangci/golangci-shared/pkg/analytics"
23+
"github.com/sirupsen/logrus"
2224
"github.com/spf13/cobra"
2325
"golang.org/x/tools/go/loader"
2426
)
@@ -110,7 +112,7 @@ func loadWholeAppIfNeeded(ctx context.Context, linters []pkg.Linter, cfg *config
110112

111113
startedAt := time.Now()
112114
defer func() {
113-
analytics.Log(ctx).Infof("Program loading took %s", time.Since(startedAt))
115+
logrus.Infof("Program loading took %s", time.Since(startedAt))
114116
}()
115117

116118
bctx := build.Default
@@ -139,7 +141,7 @@ func loadWholeAppIfNeeded(ctx context.Context, linters []pkg.Linter, cfg *config
139141
func buildSSAProgram(ctx context.Context, lprog *loader.Program) *ssa.Program {
140142
startedAt := time.Now()
141143
defer func() {
142-
analytics.Log(ctx).Infof("SSA repr building took %s", time.Since(startedAt))
144+
logrus.Infof("SSA repr building took %s", time.Since(startedAt))
143145
}()
144146

145147
ssaProg := ssautil.CreateProgram(lprog, ssa.GlobalDebug)
@@ -177,8 +179,7 @@ func buildLintCtx(ctx context.Context, linters []pkg.Linter, cfg *config.Config)
177179
}, nil
178180
}
179181

180-
func (e *Executor) runAnalysis(ctx context.Context, args []string) ([]result.Issue, error) {
181-
startedAt := time.Now()
182+
func (e *Executor) runAnalysis(ctx context.Context, args []string) (chan result.Issue, error) {
182183
e.cfg.Run.Args = args
183184

184185
linters, err := pkg.GetEnabledLinters(ctx, &e.cfg.Run)
@@ -193,38 +194,37 @@ func (e *Executor) runAnalysis(ctx context.Context, args []string) ([]result.Iss
193194

194195
runner := pkg.SimpleRunner{
195196
Processors: []processors.Processor{
196-
processors.MaxLinterIssuesPerFile{},
197-
processors.NewExcludeProcessor(fmt.Sprintf("(%s)", strings.Join(e.cfg.Run.ExcludePatterns, "|"))),
198-
processors.NewNolintProcessor(lintCtx.Program),
199-
processors.UniqByLineProcessor{},
197+
processors.NewMaxPerFileFromLinter(),
198+
processors.NewExclude(fmt.Sprintf("(%s)", strings.Join(e.cfg.Run.ExcludePatterns, "|"))),
199+
processors.NewNolint(lintCtx.Program.Fset),
200+
processors.NewUniqByLine(),
200201
processors.NewPathPrettifier(),
201202
},
202203
}
203204

204-
issues, err := runner.Run(ctx, linters, lintCtx)
205-
if err != nil {
206-
return nil, err
207-
}
208-
209-
analytics.Log(ctx).Infof("Analysis took %s", time.Since(startedAt))
210-
return issues, nil
205+
return runner.Run(ctx, linters, lintCtx), nil
211206
}
212207

213208
func (e *Executor) executeRun(cmd *cobra.Command, args []string) {
214-
f := func() error {
215-
ctx, cancel := context.WithTimeout(context.Background(), e.cfg.Run.Deadline)
216-
defer cancel()
209+
ctx, cancel := context.WithTimeout(context.Background(), e.cfg.Run.Deadline)
210+
defer cancel()
217211

212+
defer func(startedAt time.Time) {
213+
logrus.Infof("Run took %s", time.Since(startedAt))
214+
}(time.Now())
215+
216+
f := func() error {
218217
issues, err := e.runAnalysis(ctx, args)
219218
if err != nil {
220219
return err
221220
}
222221

223-
if err := outputIssues(e.cfg.Run.OutFormat, issues); err != nil {
222+
gotAnyIssues, err := outputIssues(e.cfg.Run.OutFormat, issues)
223+
if err != nil {
224224
return fmt.Errorf("can't output %d issues: %s", len(issues), err)
225225
}
226226

227-
if len(issues) != 0 {
227+
if gotAnyIssues {
228228
e.exitCode = e.cfg.Run.ExitCodeIfIssuesFound
229229
return nil
230230
}
@@ -240,34 +240,42 @@ func (e *Executor) executeRun(cmd *cobra.Command, args []string) {
240240
}
241241
}
242242

243-
func outputIssues(format string, issues []result.Issue) error {
243+
func outputIssues(format string, issues chan result.Issue) (bool, error) {
244+
stdout := os.NewFile(uintptr(syscall.Stdout), "/dev/stdout") // was set to /dev/null
244245
if format == config.OutFormatLineNumber || format == config.OutFormatColoredLineNumber {
245-
if len(issues) == 0 {
246-
outStr := "Congrats! No issues were found."
246+
gotAnyIssue := false
247+
for i := range issues {
248+
gotAnyIssue = true
249+
text := i.Text
247250
if format == config.OutFormatColoredLineNumber {
248-
outStr = color.GreenString(outStr)
251+
text = color.RedString(text)
249252
}
250-
fmt.Println(outStr)
253+
fmt.Fprintf(stdout, "%s:%d: %s\n", i.File, i.LineNumber, text)
251254
}
252255

253-
for _, i := range issues {
254-
text := i.Text
256+
if !gotAnyIssue {
257+
outStr := "Congrats! No issues were found."
255258
if format == config.OutFormatColoredLineNumber {
256-
text = color.RedString(text)
259+
outStr = color.GreenString(outStr)
257260
}
258-
fmt.Printf("%s:%d: %s\n", i.File, i.LineNumber, text)
261+
fmt.Fprintln(stdout, outStr)
259262
}
260-
return nil
263+
264+
return gotAnyIssue, nil
261265
}
262266

263267
if format == config.OutFormatJSON {
264-
outputJSON, err := json.Marshal(issues)
268+
var allIssues []result.Issue
269+
for i := range issues {
270+
allIssues = append(allIssues, i)
271+
}
272+
outputJSON, err := json.Marshal(allIssues)
265273
if err != nil {
266-
return err
274+
return false, err
267275
}
268-
fmt.Print(string(outputJSON))
269-
return nil
276+
fmt.Fprint(stdout, string(outputJSON))
277+
return len(allIssues) != 0, nil
270278
}
271279

272-
return fmt.Errorf("unknown output format %q", format)
280+
return false, fmt.Errorf("unknown output format %q", format)
273281
}

pkg/enabled_linters.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
"github.com/golangci/golangci-lint/pkg/config"
99
"github.com/golangci/golangci-lint/pkg/golinters"
10-
"github.com/golangci/golangci-shared/pkg/analytics"
10+
"github.com/sirupsen/logrus"
1111
)
1212

1313
type LinterConfig struct {
@@ -192,7 +192,7 @@ func GetEnabledLinters(ctx context.Context, cfg *config.Run) ([]Linter, error) {
192192
resultLinters = append(resultLinters, linter)
193193
resultLinterNames = append(resultLinterNames, name)
194194
}
195-
analytics.Log(ctx).Infof("Enabled linters: %s", resultLinterNames)
195+
logrus.Infof("Enabled linters: %s", resultLinterNames)
196196

197197
return resultLinters, nil
198198
}

pkg/fsutils/fsutils.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"strings"
1111
"time"
1212

13-
"github.com/golangci/golangci-shared/pkg/analytics"
13+
"github.com/sirupsen/logrus"
1414
)
1515

1616
func GetProjectRoot() string {
@@ -33,21 +33,21 @@ func (p ProjectPaths) MixedPaths() []string {
3333

3434
func processPaths(root string, paths []string, maxPaths int) ([]string, error) {
3535
if len(paths) > maxPaths {
36-
analytics.Log(context.TODO()).Warnf("Gofmt: got too much paths (%d), analyze first %d", len(paths), maxPaths)
36+
logrus.Warnf("Gofmt: got too much paths (%d), analyze first %d", len(paths), maxPaths)
3737
paths = paths[:maxPaths]
3838
}
3939

4040
ret := []string{}
41-
for i := range paths {
42-
if !filepath.IsAbs(paths[i]) {
43-
ret = append(ret, paths[i])
41+
for _, p := range paths {
42+
if !filepath.IsAbs(p) {
43+
ret = append(ret, p)
4444
continue
4545
}
4646

47-
relPath, err := filepath.Rel(root, paths[i])
47+
relPath, err := filepath.Rel(root, p)
4848
if err != nil {
4949
return nil, fmt.Errorf("can't get relative path for path %s and root %s: %s",
50-
paths[i], root, err)
50+
p, root, err)
5151
}
5252
ret = append(ret, relPath)
5353
}
@@ -58,7 +58,7 @@ func processPaths(root string, paths []string, maxPaths int) ([]string, error) {
5858
func GetPathsForAnalysis(ctx context.Context, inputPaths []string) (ret *ProjectPaths, err error) {
5959
defer func(startedAt time.Time) {
6060
if ret != nil {
61-
analytics.Log(ctx).Infof("Found paths for analysis for %s: %s", time.Since(startedAt), ret.MixedPaths())
61+
logrus.Infof("Found paths for analysis for %s: %s", time.Since(startedAt), ret.MixedPaths())
6262
}
6363
}(time.Now())
6464

@@ -92,7 +92,7 @@ func GetPathsForAnalysis(ctx context.Context, inputPaths []string) (ret *Project
9292

9393
for i := range dirs {
9494
dir := dirs[i]
95-
if dir != "." {
95+
if dir != "." && !filepath.IsAbs(dir) {
9696
dirs[i] = "./" + dir
9797
}
9898
}

pkg/golinters/deadcode.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ func (Deadcode) Name() string {
1414
return "deadcode"
1515
}
1616

17-
func (d Deadcode) Run(ctx context.Context, lintCtx *Context) (*result.Result, error) {
17+
func (d Deadcode) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, error) {
1818
issues, err := deadcodeAPI.Run(lintCtx.Program)
1919
if err != nil {
2020
return nil, err
2121
}
2222

23-
res := &result.Result{}
23+
var res []result.Issue
2424
for _, i := range issues {
25-
res.Issues = append(res.Issues, result.Issue{
25+
res = append(res, result.Issue{
2626
File: i.Pos.Filename,
2727
LineNumber: i.Pos.Line,
2828
Text: fmt.Sprintf("%s is unused", formatCode(i.UnusedIdentName, lintCtx.RunCfg())),

pkg/golinters/dupl.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ func (Dupl) Name() string {
1414
return "dupl"
1515
}
1616

17-
func (d Dupl) Run(ctx context.Context, lintCtx *Context) (*result.Result, error) {
17+
func (d Dupl) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, error) {
1818
issues, err := duplAPI.Run(lintCtx.Paths.Files, lintCtx.RunCfg().Dupl.Threshold)
1919
if err != nil {
2020
return nil, err
2121
}
2222

23-
res := &result.Result{}
23+
var res []result.Issue
2424
for _, i := range issues {
2525
dupl := fmt.Sprintf("%s:%d-%d", i.To.Filename(), i.To.LineStart(), i.To.LineEnd())
2626
text := fmt.Sprintf("%d-%d lines are duplicate of %s",
2727
i.From.LineStart(), i.From.LineEnd(),
2828
formatCode(dupl, lintCtx.RunCfg()))
29-
res.Issues = append(res.Issues, result.Issue{
29+
res = append(res, result.Issue{
3030
File: i.From.Filename(),
3131
LineNumber: i.From.LineStart(),
3232
Text: text,

0 commit comments

Comments
 (0)