@@ -182,34 +182,26 @@ func (s *Server) diagnose(ctx context.Context, snapshot source.Snapshot, alwaysA
182
182
183
183
// Diagnose all of the packages in the workspace.
184
184
wsPkgs , err := snapshot .WorkspacePackages (ctx )
185
+ if s .shouldIgnoreError (ctx , snapshot , err ) {
186
+ return nil , nil
187
+ }
188
+ // Show the error as a progress error report so that it appears in the
189
+ // status bar. If a client doesn't support progress reports, the error
190
+ // will still be shown as a ShowMessage. If there is no error, any running
191
+ // error progress reports will be closed.
192
+ s .showCriticalErrorStatus (ctx , err )
193
+
185
194
if err != nil {
186
- if errors .Is (err , context .Canceled ) {
187
- return nil , nil
188
- }
189
- // Some error messages can be displayed as diagnostics.
195
+ // Some error messages can also be displayed as diagnostics.
190
196
if errList := (* source .ErrorList )(nil ); errors .As (err , & errList ) {
191
197
if err := errorsToDiagnostic (ctx , snapshot , * errList , reports ); err == nil {
192
198
return reports , nil
193
199
}
194
200
}
195
- // Try constructing a more helpful error message out of this error.
196
- if s .handleFatalErrors (ctx , snapshot , modErr , err ) {
197
- return nil , nil
198
- }
199
201
event .Error (ctx , "errors diagnosing workspace" , err , tag .Snapshot .Of (snapshot .ID ()), tag .Directory .Of (snapshot .View ().Folder ()))
200
- // Present any `go list` errors directly to the user.
201
- if errors .Is (err , source .PackagesLoadError ) {
202
- if err := s .client .ShowMessage (ctx , & protocol.ShowMessageParams {
203
- Type : protocol .Error ,
204
- Message : fmt .Sprintf (`The code in the workspace failed to compile (see the error message below).
205
- If you believe this is a mistake, please file an issue: https://github.com/golang/go/issues/new.
206
- %v` , err ),
207
- }); err != nil {
208
- event .Error (ctx , "ShowMessage failed" , err , tag .Directory .Of (snapshot .View ().Folder ().Filename ()))
209
- }
210
- }
211
202
return nil , nil
212
203
}
204
+
213
205
var (
214
206
showMsgMu sync.Mutex
215
207
showMsg * protocol.ShowMessageParams
@@ -299,6 +291,36 @@ If you believe this is a mistake, please file an issue: https://github.com/golan
299
291
return reports , showMsg
300
292
}
301
293
294
+ // showCriticalErrorStatus shows the error as a progress report.
295
+ // If the error is nil, it clears any existing error progress report.
296
+ func (s * Server ) showCriticalErrorStatus (ctx context.Context , err error ) {
297
+ s .criticalErrorStatusMu .Lock ()
298
+ defer s .criticalErrorStatusMu .Unlock ()
299
+
300
+ // Remove all newlines so that the error message can be formatted in a
301
+ // status bar.
302
+ var errMsg string
303
+ if err != nil {
304
+ errMsg = strings .Replace (err .Error (), "\n " , " " , - 1 )
305
+ }
306
+
307
+ if s .criticalErrorStatus == nil {
308
+ if errMsg != "" {
309
+ s .criticalErrorStatus = s .progress .start (ctx , "Error loading workspace" , errMsg , nil , nil )
310
+ }
311
+ return
312
+ }
313
+
314
+ // If an error is already shown to the user, update it or mark it as
315
+ // resolved.
316
+ if errMsg == "" {
317
+ s .criticalErrorStatus .end ("Done." )
318
+ s .criticalErrorStatus = nil
319
+ } else {
320
+ s .criticalErrorStatus .report (errMsg , 0 )
321
+ }
322
+ }
323
+
302
324
// checkForOrphanedFile checks that the given URIs can be mapped to packages.
303
325
// If they cannot and the workspace is not otherwise unloaded, it also surfaces
304
326
// a warning, suggesting that the user check the file for build tags.
@@ -470,7 +492,13 @@ func toProtocolDiagnostics(diagnostics []*source.Diagnostic) []protocol.Diagnost
470
492
return reports
471
493
}
472
494
473
- func (s * Server ) handleFatalErrors (ctx context.Context , snapshot source.Snapshot , modErr , loadErr error ) bool {
495
+ func (s * Server ) shouldIgnoreError (ctx context.Context , snapshot source.Snapshot , err error ) bool {
496
+ if err == nil { // if there is no error at all
497
+ return false
498
+ }
499
+ if errors .Is (err , context .Canceled ) {
500
+ return true
501
+ }
474
502
// If the folder has no Go code in it, we shouldn't spam the user with a warning.
475
503
var hasGo bool
476
504
_ = filepath .Walk (snapshot .View ().Folder ().Filename (), func (path string , info os.FileInfo , err error ) error {
0 commit comments