From 9972d82609bc911cd9fb3152ffaf0df1643a55f0 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 8 Mar 2024 16:39:46 +0100 Subject: [PATCH] fix: store severity from linters in the cache --- pkg/golinters/goanalysis/issue.go | 1 + pkg/golinters/goanalysis/runners.go | 2 + pkg/lint/runner.go | 68 ++++++++++++++--------------- 3 files changed, 37 insertions(+), 34 deletions(-) diff --git a/pkg/golinters/goanalysis/issue.go b/pkg/golinters/goanalysis/issue.go index 65734ce3ef227..15d8dd2b333fd 100644 --- a/pkg/golinters/goanalysis/issue.go +++ b/pkg/golinters/goanalysis/issue.go @@ -23,6 +23,7 @@ func NewIssue(issue *result.Issue, pass *analysis.Pass) Issue { type EncodingIssue struct { FromLinter string Text string + Severity string Pos token.Position LineRange *result.Range Replacement *result.Replacement diff --git a/pkg/golinters/goanalysis/runners.go b/pkg/golinters/goanalysis/runners.go index 559fb1392b96c..d5a9c157ee8c2 100644 --- a/pkg/golinters/goanalysis/runners.go +++ b/pkg/golinters/goanalysis/runners.go @@ -151,6 +151,7 @@ func saveIssuesToCache(allPkgs []*packages.Package, pkgsFromCache map[*packages. encodedIssues = append(encodedIssues, EncodingIssue{ FromLinter: i.FromLinter, Text: i.Text, + Severity: i.Severity, Pos: i.Pos, LineRange: i.LineRange, Replacement: i.Replacement, @@ -222,6 +223,7 @@ func loadIssuesFromCache(pkgs []*packages.Package, lintCtx *linter.Context, issues = append(issues, result.Issue{ FromLinter: i.FromLinter, Text: i.Text, + Severity: i.Severity, Pos: i.Pos, LineRange: i.LineRange, Replacement: i.Replacement, diff --git a/pkg/lint/runner.go b/pkg/lint/runner.go index dc6b2e73a0033..aa97d69770a67 100644 --- a/pkg/lint/runner.go +++ b/pkg/lint/runner.go @@ -22,6 +22,11 @@ import ( "github.com/golangci/golangci-lint/pkg/timeutils" ) +type processorStat struct { + inCount int + outCount int +} + type Runner struct { Processors []processors.Processor Log logutils.Log @@ -111,6 +116,33 @@ func NewRunner(log logutils.Log, cfg *config.Config, goenv *goutil.Env, }, nil } +func (r *Runner) Run(ctx context.Context, linters []*linter.Config, lintCtx *linter.Context) ([]result.Issue, error) { + sw := timeutils.NewStopwatch("linters", r.Log) + defer sw.Print() + + var ( + lintErrors error + issues []result.Issue + ) + + for _, lc := range linters { + lc := lc + sw.TrackStage(lc.Name(), func() { + linterIssues, err := r.runLinterSafe(ctx, lintCtx, lc) + if err != nil { + lintErrors = errors.Join(lintErrors, fmt.Errorf("can't run linter %s", lc.Linter.Name()), err) + r.Log.Warnf("Can't run linter %s: %v", lc.Linter.Name(), err) + + return + } + + issues = append(issues, linterIssues...) + }) + } + + return r.processLintResults(issues), lintErrors +} + func (r *Runner) runLinterSafe(ctx context.Context, lintCtx *linter.Context, lc *linter.Config) (ret []result.Issue, err error) { defer func() { @@ -151,12 +183,7 @@ func (r *Runner) runLinterSafe(ctx context.Context, lintCtx *linter.Context, return issues, nil } -type processorStat struct { - inCount int - outCount int -} - -func (r Runner) processLintResults(inIssues []result.Issue) []result.Issue { +func (r *Runner) processLintResults(inIssues []result.Issue) []result.Issue { sw := timeutils.NewStopwatch("processing", r.Log) var issuesBefore, issuesAfter int @@ -187,7 +214,7 @@ func (r Runner) processLintResults(inIssues []result.Issue) []result.Issue { return outIssues } -func (r Runner) printPerProcessorStat(stat map[string]processorStat) { +func (r *Runner) printPerProcessorStat(stat map[string]processorStat) { parts := make([]string, 0, len(stat)) for name, ps := range stat { if ps.inCount != 0 { @@ -199,33 +226,6 @@ func (r Runner) printPerProcessorStat(stat map[string]processorStat) { } } -func (r Runner) Run(ctx context.Context, linters []*linter.Config, lintCtx *linter.Context) ([]result.Issue, error) { - sw := timeutils.NewStopwatch("linters", r.Log) - defer sw.Print() - - var ( - lintErrors error - issues []result.Issue - ) - - for _, lc := range linters { - lc := lc - sw.TrackStage(lc.Name(), func() { - linterIssues, err := r.runLinterSafe(ctx, lintCtx, lc) - if err != nil { - lintErrors = errors.Join(lintErrors, fmt.Errorf("can't run linter %s", lc.Linter.Name()), err) - r.Log.Warnf("Can't run linter %s: %v", lc.Linter.Name(), err) - - return - } - - issues = append(issues, linterIssues...) - }) - } - - return r.processLintResults(issues), lintErrors -} - func (r *Runner) processIssues(issues []result.Issue, sw *timeutils.Stopwatch, statPerProcessor map[string]processorStat) []result.Issue { for _, p := range r.Processors { var newIssues []result.Issue