Skip to content

Support SuggestedFixes #2609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ require (
github.com/nishanths/exhaustive v0.7.11
github.com/nishanths/predeclared v0.2.1
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0
github.com/polyfloyd/go-errorlint v0.0.0-20211125173453-6d6d39c5bb8b
github.com/prometheus/procfs v0.6.0 // indirect
github.com/quasilyte/go-ruleguard/dsl v0.3.17
github.com/ryancurrah/gomodguard v1.2.3
github.com/ryanrolds/sqlclosecheck v0.3.0
github.com/sanposhiho/wastedassign/v2 v2.0.6
github.com/securego/gosec/v2 v2.9.6
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c
github.com/shirou/gopsutil/v3 v3.22.1
github.com/sirupsen/logrus v1.8.1
github.com/sivchari/containedctx v1.0.1
Expand Down
2 changes: 0 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package golinters
package goanalysis

import (
"bytes"
"fmt"
"go/token"
"strings"
"sync"

"github.com/pkg/errors"
diffpkg "github.com/sourcegraph/go-diff/diff"
Expand Down Expand Up @@ -207,29 +208,30 @@ func (p *hunkChangesParser) parse(h *diffpkg.Hunk) []Change {
return p.ret
}

type TextModifier = func(lintCtx *linter.Context, text string) string

var (
textModifier = make(map[string]TextModifier)
textModifierLock = sync.RWMutex{}
)

func SetTextModifier(linterName string, modifier TextModifier) {
textModifierLock.Lock()
defer textModifierLock.Unlock()
textModifier[linterName] = modifier
}

func getErrorTextForLinter(lintCtx *linter.Context, linterName string) string {
text := "File is not formatted"
switch linterName {
case gofumptName:
text = "File is not `gofumpt`-ed"
if lintCtx.Settings().Gofumpt.ExtraRules {
text += " with `-extra`"
}
case gofmtName:
text = "File is not `gofmt`-ed"
if lintCtx.Settings().Gofmt.Simplify {
text += " with `-s`"
}
case goimportsName:
text = "File is not `goimports`-ed"
if lintCtx.Settings().Goimports.LocalPrefixes != "" {
text += " with -local " + lintCtx.Settings().Goimports.LocalPrefixes
}
textModifierLock.RLock()
defer textModifierLock.RUnlock()
if f, ok := textModifier[linterName]; ok {
return f(lintCtx, text)
}
return text
}

func extractIssuesFromPatch(patch string, log logutils.Log, lintCtx *linter.Context, linterName string) ([]result.Issue, error) {
func ExtractIssuesFromPatch(patch string, log logutils.Log, lintCtx *linter.Context, linterName string) ([]result.Issue, error) {
diffs, err := diffpkg.ParseMultiFileDiff([]byte(patch))
if err != nil {
return nil, errors.Wrap(err, "can't parse patch")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package golinters
package goanalysis

import (
"testing"
Expand Down
57 changes: 36 additions & 21 deletions pkg/golinters/goanalysis/runners.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func runAnalyzers(cfg runAnalyzersConfig, lintCtx *linter.Context) ([]result.Iss
}
}()

buildAllIssues := func() []result.Issue {
buildAllIssues := func() ([]result.Issue, error) {
var retIssues []result.Issue
reportedIssues := cfg.reportIssues(lintCtx)
for i := range reportedIssues {
Expand All @@ -69,8 +69,13 @@ func runAnalyzers(cfg runAnalyzersConfig, lintCtx *linter.Context) ([]result.Iss
}
retIssues = append(retIssues, *issue)
}
retIssues = append(retIssues, buildIssues(diags, cfg.getLinterNameForDiagnostic)...)
return retIssues
newIssues, err := buildIssues(lintCtx, diags, cfg.getLinterNameForDiagnostic)
if err != nil {
return nil, err
}

retIssues = append(retIssues, newIssues...)
return retIssues, nil
}

errIssues, err := buildIssuesFromIllTypedError(errs, lintCtx)
Expand All @@ -79,12 +84,16 @@ func runAnalyzers(cfg runAnalyzersConfig, lintCtx *linter.Context) ([]result.Iss
}

issues = append(issues, errIssues...)
issues = append(issues, buildAllIssues()...)
newIssues, err := buildAllIssues()
if err != nil {
return nil, err
}
issues = append(issues, newIssues...)

return issues, nil
}

func buildIssues(diags []Diagnostic, linterNameBuilder func(diag *Diagnostic) string) []result.Issue {
func buildIssues(lintCtx *linter.Context, diags []Diagnostic, linterNameBuilder func(diag *Diagnostic) string) ([]result.Issue, error) {
var issues []result.Issue
for i := range diags {
diag := &diags[i]
Expand All @@ -97,25 +106,31 @@ func buildIssues(diags []Diagnostic, linterNameBuilder func(diag *Diagnostic) st
text = fmt.Sprintf("%s: %s", diag.Analyzer.Name, diag.Message)
}

issues = append(issues, result.Issue{
FromLinter: linterName,
Text: text,
Pos: diag.Position,
Pkg: diag.Pkg,
})

if len(diag.Related) > 0 {
for _, info := range diag.Related {
issues = append(issues, result.Issue{
FromLinter: linterName,
Text: fmt.Sprintf("%s(related information): %s", diag.Analyzer.Name, info.Message),
Pos: diag.Pkg.Fset.Position(info.Pos),
Pkg: diag.Pkg,
})
if lintCtx.Cfg.Issues.NeedFix && len(diag.SuggestedFixes) > 0 {
newIssues, err := convertSuggestedFixes(lintCtx, linterName, diag)
if err != nil {
return nil, err
}
issues = append(issues, newIssues...)
} else {
issues = append(issues, result.Issue{
FromLinter: linterName,
Text: text,
Pos: diag.Position,
Pkg: diag.Pkg,
})
}

for _, info := range diag.Related {
issues = append(issues, result.Issue{
FromLinter: linterName,
Text: fmt.Sprintf("%s(related information): %s", diag.Analyzer.Name, info.Message),
Pos: diag.Pkg.Fset.Position(info.Pos),
Pkg: diag.Pkg,
})
}
}
return issues
return issues, nil
}

func getIssuesCacheKey(analyzers []*analysis.Analyzer) string {
Expand Down
103 changes: 103 additions & 0 deletions pkg/golinters/goanalysis/suggested_fixes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package goanalysis

import (
"bytes"
"fmt"
"go/token"
"io"
"os"

"github.com/pkg/errors"
"github.com/pmezard/go-difflib/difflib"

"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
)

func convertSuggestedFixes(lintCtx *linter.Context, linterName string, diag *Diagnostic) ([]result.Issue, error) {
var issues []result.Issue

for _, fix := range diag.SuggestedFixes {
origContent := make(map[string][]byte)
readers := make(map[string]io.ReadSeeker)
bufs := make(map[string]*bytes.Buffer)

for _, edit := range fix.TextEdits {
if edit.End == token.NoPos {
edit.End = edit.Pos
}
start, end := diag.Pkg.Fset.Position(edit.Pos), diag.Pkg.Fset.Position(edit.End)
orig, ok := readers[start.Filename]
if !ok {
data, err := os.ReadFile(start.Filename)
if err != nil {
return nil, errors.Wrapf(err, "can't read file: %s", start.Filename)
}
origContent[start.Filename] = data
orig = bytes.NewReader(data)
readers[start.Filename] = orig
bufs[start.Filename] = &bytes.Buffer{}
}

buf := bufs[start.Filename]
cur, err := orig.Seek(0, io.SeekCurrent)
if err != nil {
return nil, errors.Wrapf(err, "can't get current position of reader of file %q", start.Filename)
}
if l := start.Offset - int(cur); l > 0 {
b := make([]byte, l)
if _, err := orig.Read(b); err != nil {
return nil, errors.Wrapf(err, "can't read form stored reader of file %q", start.Filename)
}
buf.Write(b)
}
buf.Write(edit.NewText)
if _, err := orig.Seek(int64(end.Offset), io.SeekStart); err != nil {
return nil, errors.Wrapf(err, "can't change position of reader of file %q", start.Filename)
}
}
for filename, f := range readers {
data, err := io.ReadAll(f)
if err != nil {
return nil, err
}
bufs[filename].Write(data)
}

for filename, data := range origContent {
newIssues, err := createPatchAndExtractIssues(lintCtx, linterName, filename, data, bufs[filename].Bytes())
if err != nil {
return nil, err
}
for i := range newIssues {
newIssues[i].Text = fmt.Sprintf("%s: %s", diag.Message, fix.Message)
}
issues = append(issues, newIssues...)
}
}

return issues, nil
}

func createPatchAndExtractIssues(lintCtx *linter.Context, linterName, filename string, src, dst []byte) ([]result.Issue, error) {
out := bytes.Buffer{}
if _, err := out.WriteString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", filename)); err != nil {
return nil, errors.Wrap(err, "can't write diff header")
}

d := difflib.UnifiedDiff{
A: difflib.SplitLines(string(src)),
B: difflib.SplitLines(string(dst)),
Context: 3,
}
if err := difflib.WriteUnifiedDiff(&out, d); err != nil {
return nil, errors.Wrap(err, "can't create diff")
}

newIssues, err := ExtractIssuesFromPatch(out.String(), lintCtx.Log, lintCtx, linterName)
if err != nil {
return nil, errors.Wrap(err, "can't extract issues from diff")
}

return newIssues, nil
}
10 changes: 9 additions & 1 deletion pkg/golinters/gofmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ import (
const gofmtName = "gofmt"

func NewGofmt() *goanalysis.Linter {
goanalysis.SetTextModifier(gofmtName, func(lintCtx *linter.Context, _ string) string {
text := "File is not `gofmt`-ed"
if lintCtx.Settings().Gofmt.Simplify {
text += " with `-s`"
}
return text
})

var mu sync.Mutex
var resIssues []goanalysis.Issue

Expand Down Expand Up @@ -46,7 +54,7 @@ func NewGofmt() *goanalysis.Linter {
continue
}

is, err := extractIssuesFromPatch(string(diff), lintCtx.Log, lintCtx, gofmtName)
is, err := goanalysis.ExtractIssuesFromPatch(string(diff), lintCtx.Log, lintCtx, gofmtName)
if err != nil {
return nil, errors.Wrapf(err, "can't extract issues from gofmt diff output %q", string(diff))
}
Expand Down
23 changes: 17 additions & 6 deletions pkg/golinters/gofumpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync"

"github.com/pkg/errors"
"github.com/shazow/go-diff/difflib"
"github.com/pmezard/go-difflib/difflib"
"golang.org/x/tools/go/analysis"
"mvdan.cc/gofumpt/format"

Expand All @@ -19,9 +19,16 @@ import (
const gofumptName = "gofumpt"

func NewGofumpt() *goanalysis.Linter {
goanalysis.SetTextModifier(gofumptName, func(lintCtx *linter.Context, _ string) string {
text := "File is not `gofumpt`-ed"
if lintCtx.Settings().Gofumpt.ExtraRules {
text += " with `-extra`"
}
return text
})

var mu sync.Mutex
var resIssues []goanalysis.Issue
differ := difflib.New()

analyzer := &analysis.Analyzer{
Name: gofumptName,
Expand Down Expand Up @@ -67,13 +74,17 @@ func NewGofumpt() *goanalysis.Linter {
return nil, fmt.Errorf("error while running gofumpt: %w", err)
}

err = differ.Diff(&out, bytes.NewReader(input), bytes.NewReader(output))
if err != nil {
return nil, fmt.Errorf("error while running gofumpt: %w", err)
d := difflib.UnifiedDiff{
A: difflib.SplitLines(string(input)),
B: difflib.SplitLines(string(output)),
Context: 3,
}
if err := difflib.WriteUnifiedDiff(&out, d); err != nil {
return nil, errors.Wrap(err, "can't create diff")
}

diff := out.String()
is, err := extractIssuesFromPatch(diff, lintCtx.Log, lintCtx, gofumptName)
is, err := goanalysis.ExtractIssuesFromPatch(diff, lintCtx.Log, lintCtx, gofumptName)
if err != nil {
return nil, errors.Wrapf(err, "can't extract issues from gofumpt diff output %q", diff)
}
Expand Down
10 changes: 9 additions & 1 deletion pkg/golinters/goimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ import (
const goimportsName = "goimports"

func NewGoimports() *goanalysis.Linter {
goanalysis.SetTextModifier(goimportsName, func(lintCtx *linter.Context, _ string) string {
text := "File is not `goimports`-ed"
if lintCtx.Settings().Goimports.LocalPrefixes != "" {
text += " with -local " + lintCtx.Settings().Goimports.LocalPrefixes
}
return text
})

var mu sync.Mutex
var resIssues []goanalysis.Issue

Expand Down Expand Up @@ -47,7 +55,7 @@ func NewGoimports() *goanalysis.Linter {
continue
}

is, err := extractIssuesFromPatch(string(diff), lintCtx.Log, lintCtx, goimportsName)
is, err := goanalysis.ExtractIssuesFromPatch(string(diff), lintCtx.Log, lintCtx, goimportsName)
if err != nil {
return nil, errors.Wrapf(err, "can't extract issues from gofmt diff output %q", string(diff))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/result/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Range struct {

type Replacement struct {
NeedOnlyDelete bool // need to delete all lines of the issue without replacement with new lines
NewLines []string // if NeedDelete is false it's the replacement lines
NewLines []string // if NeedOnlyDelete is false it's the replacement lines
Inline *InlineFix
}

Expand Down
Loading