Skip to content

Commit 5ef542f

Browse files
committed
Print first compilation error in warning about inability to run megacheck because of compilation errors
1 parent fb38e51 commit 5ef542f

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

pkg/golinters/megacheck.go

+35-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package golinters
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"strings"
78

89
megacheckAPI "github.com/golangci/go-tools/cmd/megacheck"
10+
"github.com/golangci/golangci-lint/pkg/fsutils"
911
"github.com/golangci/golangci-lint/pkg/lint/linter"
1012
"github.com/golangci/golangci-lint/pkg/result"
1113
)
@@ -50,14 +52,45 @@ func (m Megacheck) Desc() string {
5052
return descs[m.Name()]
5153
}
5254

55+
func prettifyCompilationError(err error) error {
56+
i := TypeCheck{}.parseError(err)
57+
if i == nil {
58+
return err
59+
}
60+
61+
shortFilename, pathErr := fsutils.ShortestRelPath(i.Pos.Filename, "")
62+
if pathErr != nil {
63+
return err
64+
}
65+
66+
errText := shortFilename
67+
if i.Line() != 0 {
68+
errText += fmt.Sprintf(":%d", i.Line())
69+
}
70+
errText += fmt.Sprintf(": %s", i.Text)
71+
return errors.New(errText)
72+
}
73+
5374
func (m Megacheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
5475
if len(lintCtx.NotCompilingPackages) != 0 {
5576
var packages []string
77+
var errors []error
5678
for _, p := range lintCtx.NotCompilingPackages {
5779
packages = append(packages, p.String())
80+
errors = append(errors, p.Errors...)
5881
}
59-
lintCtx.Log.Warnf("Can't run megacheck because of compilation errors in packages "+
60-
"%s: run `typecheck` linter to see errors", packages)
82+
83+
warnText := fmt.Sprintf("Can't run megacheck because of compilation errors in packages %s",
84+
packages)
85+
if len(errors) != 0 {
86+
warnText += fmt.Sprintf(": %s", prettifyCompilationError(errors[0]))
87+
if len(errors) > 1 {
88+
const runCmd = "golangci-lint run --no-config --disable-all -E typecheck"
89+
warnText += fmt.Sprintf(" and %d more errors: run `%s` to see all errors", len(errors)-1, runCmd)
90+
}
91+
}
92+
lintCtx.Log.Warnf("%s", warnText)
93+
6194
// megacheck crashes if there are not compiling packages
6295
return nil, nil
6396
}

pkg/result/processors/diff.go

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

0 commit comments

Comments
 (0)