Skip to content

Commit f5c826d

Browse files
committed
internal/lsp: hold notifications until gopls is initialized
Errors in options generate showMessage notifications, but these were lost if they were sent before the initialization handshake completes. Change-Id: I011da2387467bb6bf188201a7294e1bb30805cbc Reviewed-on: https://go-review.googlesource.com/c/tools/+/263518 Run-TryBot: Peter Weinberger <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: Peter Weinberger <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent b894a32 commit f5c826d

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

internal/lsp/general.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ func (s *Server) initialized(ctx context.Context, params *protocol.InitializedPa
161161
s.state = serverInitialized
162162
s.stateMu.Unlock()
163163

164+
for _, not := range s.notifications {
165+
s.client.ShowMessage(ctx, not)
166+
}
167+
s.notifications = nil
168+
164169
options := s.session.Options()
165170
defer func() { s.session.SetOptions(options) }()
166171

@@ -421,30 +426,42 @@ func (s *Server) fetchConfig(ctx context.Context, name string, folder span.URI,
421426
return nil
422427
}
423428

429+
func (s *Server) eventuallyShowMessage(ctx context.Context, msg *protocol.ShowMessageParams) error {
430+
s.stateMu.Lock()
431+
defer s.stateMu.Unlock()
432+
if s.state == serverInitialized {
433+
return s.client.ShowMessage(ctx, msg)
434+
}
435+
s.notifications = append(s.notifications, msg)
436+
return nil
437+
}
438+
424439
func (s *Server) handleOptionResults(ctx context.Context, results source.OptionResults) error {
425440
for _, result := range results {
426441
if result.Error != nil {
427-
if err := s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
442+
msg := &protocol.ShowMessageParams{
428443
Type: protocol.Error,
429444
Message: result.Error.Error(),
430-
}); err != nil {
445+
}
446+
if err := s.eventuallyShowMessage(ctx, msg); err != nil {
431447
return err
432448
}
433449
}
434450
switch result.State {
435451
case source.OptionUnexpected:
436-
if err := s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
452+
msg := &protocol.ShowMessageParams{
437453
Type: protocol.Error,
438454
Message: fmt.Sprintf("unexpected gopls setting %q", result.Name),
439-
}); err != nil {
455+
}
456+
if err := s.eventuallyShowMessage(ctx, msg); err != nil {
440457
return err
441458
}
442459
case source.OptionDeprecated:
443460
msg := fmt.Sprintf("gopls setting %q is deprecated", result.Name)
444461
if result.Replacement != "" {
445462
msg = fmt.Sprintf("%s, use %q instead", msg, result.Replacement)
446463
}
447-
if err := s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
464+
if err := s.eventuallyShowMessage(ctx, &protocol.ShowMessageParams{
448465
Type: protocol.Warning,
449466
Message: msg,
450467
}); err != nil {

internal/lsp/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ type Server struct {
6767

6868
session source.Session
6969

70+
// notifications generated before serverInitialized
71+
notifications []*protocol.ShowMessageParams
72+
7073
// changedFiles tracks files for which there has been a textDocument/didChange.
7174
changedFilesMu sync.Mutex
7275
changedFiles map[span.URI]struct{}

0 commit comments

Comments
 (0)