Print first compilation error in warning about inability to run megacheck because of compilation errors
This commit is contained in:
parent
fb38e51c83
commit
5ef542facd
@ -2,10 +2,12 @@ package golinters
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
megacheckAPI "github.com/golangci/go-tools/cmd/megacheck"
|
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/lint/linter"
|
||||||
"github.com/golangci/golangci-lint/pkg/result"
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
)
|
)
|
||||||
@ -50,14 +52,45 @@ func (m Megacheck) Desc() string {
|
|||||||
return descs[m.Name()]
|
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) {
|
func (m Megacheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
|
||||||
if len(lintCtx.NotCompilingPackages) != 0 {
|
if len(lintCtx.NotCompilingPackages) != 0 {
|
||||||
var packages []string
|
var packages []string
|
||||||
|
var errors []error
|
||||||
for _, p := range lintCtx.NotCompilingPackages {
|
for _, p := range lintCtx.NotCompilingPackages {
|
||||||
packages = append(packages, p.String())
|
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
|
// megacheck crashes if there are not compiling packages
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ func (p Diff) Process(issues []result.Issue) ([]result.Issue, error) {
|
|||||||
if p.patchFilePath != "" {
|
if p.patchFilePath != "" {
|
||||||
patch, err := ioutil.ReadFile(p.patchFilePath)
|
patch, err := ioutil.ReadFile(p.patchFilePath)
|
||||||
if err != nil {
|
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)
|
patchReader = bytes.NewReader(patch)
|
||||||
} else if p.patch != "" {
|
} else if p.patch != "" {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user