@@ -190,6 +190,30 @@ func discoverGoRoot() (string, error) {
190190 return strings .TrimSpace (string (output )), nil
191191}
192192
193+ // separateNotCompilingPackages moves not compiling packages into separate slices:
194+ // a lot of linters crash on such packages. Leave them only for those linters
195+ // which can work with them.
196+ func separateNotCompilingPackages (lintCtx * golinters.Context ) {
197+ prog := lintCtx .Program
198+
199+ compilingCreated := make ([]* loader.PackageInfo , 0 , len (prog .Created ))
200+ for _ , info := range prog .Created {
201+ if len (info .Errors ) != 0 {
202+ lintCtx .NotCompilingPackages = append (lintCtx .NotCompilingPackages , info )
203+ } else {
204+ compilingCreated = append (compilingCreated , info )
205+ }
206+ }
207+ prog .Created = compilingCreated
208+
209+ for k , info := range prog .Imported {
210+ if len (info .Errors ) != 0 {
211+ lintCtx .NotCompilingPackages = append (lintCtx .NotCompilingPackages , info )
212+ delete (prog .Imported , k )
213+ }
214+ }
215+ }
216+
193217func buildLintCtx (ctx context.Context , linters []pkg.Linter , cfg * config.Config ) (* golinters.Context , error ) {
194218 // Set GOROOT to have working cross-compilation: cross-compiled binaries
195219 // have invalid GOROOT. XXX: can't use runtime.GOROOT().
@@ -228,14 +252,18 @@ func buildLintCtx(ctx context.Context, linters []pkg.Linter, cfg *config.Config)
228252 astCache = astcache .LoadFromFiles (paths .Files )
229253 }
230254
231- return & golinters.Context {
255+ ret := & golinters.Context {
232256 Paths : paths ,
233257 Cfg : cfg ,
234258 Program : prog ,
235259 SSAProgram : ssaProg ,
236260 LoaderConfig : loaderConfig ,
237261 ASTCache : astCache ,
238- }, nil
262+ }
263+
264+ separateNotCompilingPackages (ret )
265+
266+ return ret , nil
239267}
240268
241269func (e * Executor ) runAnalysis (ctx context.Context , args []string ) (<- chan result.Issue , error ) {
0 commit comments