@@ -82,27 +82,44 @@ func prettifyCompilationError(err packages.Error) error {
82
82
return errors .New (errText )
83
83
}
84
84
85
- func (m Megacheck ) Run (ctx context.Context , lintCtx * linter.Context ) ([]result.Issue , error ) {
86
- if len (lintCtx .NotCompilingPackages ) != 0 {
87
- var errPkgs []string
88
- var errors []packages.Error
89
- for _ , p := range lintCtx .NotCompilingPackages {
90
- errPkgs = append (errPkgs , p .String ())
91
- errors = append (errors , libpackages .ExtractErrors (p , lintCtx .ASTCache )... )
85
+ func (m Megacheck ) canAnalyze (lintCtx * linter.Context ) bool {
86
+ if len (lintCtx .NotCompilingPackages ) == 0 {
87
+ return true
88
+ }
89
+
90
+ var errPkgs []string
91
+ var errors []packages.Error
92
+ for _ , p := range lintCtx .NotCompilingPackages {
93
+ if p .Name == "main" {
94
+ // megacheck crashes on not compiling packages but main packages
95
+ // aren't reachable by megacheck: other packages can't depend on them.
96
+ continue
92
97
}
93
98
94
- warnText := fmt .Sprintf ("Can't run megacheck because of compilation errors in packages %s" ,
95
- errPkgs )
96
- if len (errors ) != 0 {
97
- warnText += fmt .Sprintf (": %s" , prettifyCompilationError (errors [0 ]))
98
- if len (errors ) > 1 {
99
- const runCmd = "golangci-lint run --no-config --disable-all -E typecheck"
100
- warnText += fmt .Sprintf (" and %d more errors: run `%s` to see all errors" , len (errors )- 1 , runCmd )
101
- }
99
+ errPkgs = append (errPkgs , p .String ())
100
+ errors = append (errors , libpackages .ExtractErrors (p , lintCtx .ASTCache )... )
101
+ }
102
+
103
+ if len (errPkgs ) == 0 { // only main packages do not compile
104
+ return true
105
+ }
106
+
107
+ warnText := fmt .Sprintf ("Can't run megacheck because of compilation errors in packages %s" , errPkgs )
108
+ if len (errors ) != 0 {
109
+ warnText += fmt .Sprintf (": %s" , prettifyCompilationError (errors [0 ]))
110
+ if len (errors ) > 1 {
111
+ const runCmd = "golangci-lint run --no-config --disable-all -E typecheck"
112
+ warnText += fmt .Sprintf (" and %d more errors: run `%s` to see all errors" , len (errors )- 1 , runCmd )
102
113
}
103
- lintCtx .Log .Warnf ("%s" , warnText )
114
+ }
115
+ lintCtx .Log .Warnf ("%s" , warnText )
104
116
105
- // megacheck crashes if there are not compiling packages
117
+ // megacheck crashes if there are not compiling packages
118
+ return false
119
+ }
120
+
121
+ func (m Megacheck ) Run (ctx context.Context , lintCtx * linter.Context ) ([]result.Issue , error ) {
122
+ if ! m .canAnalyze (lintCtx ) {
106
123
return nil , nil
107
124
}
108
125
0 commit comments