@@ -2,10 +2,12 @@ package golinters
22
33import (
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+
5374func (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 }
0 commit comments