diff --git a/pkg/golinters/megacheck.go b/pkg/golinters/megacheck.go index ac452119..6b9d84a0 100644 --- a/pkg/golinters/megacheck.go +++ b/pkg/golinters/megacheck.go @@ -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 } diff --git a/pkg/result/processors/diff.go b/pkg/result/processors/diff.go index d6c0cc78..42b9d8a0 100644 --- a/pkg/result/processors/diff.go +++ b/pkg/result/processors/diff.go @@ -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 != "" {