Print first compilation error in warning about inability to run megacheck because of compilation errors

This commit is contained in:
Denis Isaev 2018-06-28 09:10:02 +03:00 committed by Isaev Denis
parent fb38e51c83
commit 5ef542facd
2 changed files with 36 additions and 3 deletions

View File

@ -2,10 +2,12 @@ package golinters
import (
"context"
"errors"
"fmt"
"strings"
megacheckAPI "github.com/golangci/go-tools/cmd/megacheck"
"github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
)
@ -50,14 +52,45 @@ func (m Megacheck) Desc() string {
return descs[m.Name()]
}
func prettifyCompilationError(err error) error {
i := TypeCheck{}.parseError(err)
if i == nil {
return err
}
shortFilename, pathErr := fsutils.ShortestRelPath(i.Pos.Filename, "")
if pathErr != nil {
return err
}
errText := shortFilename
if i.Line() != 0 {
errText += fmt.Sprintf(":%d", i.Line())
}
errText += fmt.Sprintf(": %s", i.Text)
return errors.New(errText)
}
func (m Megacheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
if len(lintCtx.NotCompilingPackages) != 0 {
var packages []string
var errors []error
for _, p := range lintCtx.NotCompilingPackages {
packages = append(packages, p.String())
errors = append(errors, p.Errors...)
}
lintCtx.Log.Warnf("Can't run megacheck because of compilation errors in packages "+
"%s: run `typecheck` linter to see errors", packages)
warnText := fmt.Sprintf("Can't run megacheck because of compilation errors in packages %s",
packages)
if len(errors) != 0 {
warnText += fmt.Sprintf(": %s", prettifyCompilationError(errors[0]))
if len(errors) > 1 {
const runCmd = "golangci-lint run --no-config --disable-all -E typecheck"
warnText += fmt.Sprintf(" and %d more errors: run `%s` to see all errors", len(errors)-1, runCmd)
}
}
lintCtx.Log.Warnf("%s", warnText)
// megacheck crashes if there are not compiling packages
return nil, nil
}

View File

@ -43,7 +43,7 @@ func (p Diff) Process(issues []result.Issue) ([]result.Issue, error) {
if p.patchFilePath != "" {
patch, err := ioutil.ReadFile(p.patchFilePath)
if err != nil {
return nil, fmt.Errorf("can't read from pathc file %s: %s", p.patchFilePath, err)
return nil, fmt.Errorf("can't read from patch file %s: %s", p.patchFilePath, err)
}
patchReader = bytes.NewReader(patch)
} else if p.patch != "" {