diff --git a/internal/lsp/server.go b/internal/lsp/server.go index 165b0972db..e057e28d61 100644 --- a/internal/lsp/server.go +++ b/internal/lsp/server.go @@ -468,6 +468,10 @@ var handlers = sync.OnceValue(func() handlerMap { func registerNotificationHandler[Req any](handlers handlerMap, info lsproto.NotificationInfo[Req], fn func(*Server, context.Context, Req) error) { handlers[info.Method] = func(s *Server, ctx context.Context, req *lsproto.RequestMessage) error { + if s.session == nil && req.Method != lsproto.MethodInitialized { + return lsproto.ErrServerNotInitialized + } + var params Req // Ignore empty params; all generated params are either pointers or any. if req.Params != nil { @@ -486,6 +490,10 @@ func registerRequestHandler[Req, Resp any]( fn func(*Server, context.Context, Req, *lsproto.RequestMessage) (Resp, error), ) { handlers[info.Method] = func(s *Server, ctx context.Context, req *lsproto.RequestMessage) error { + if s.session == nil && req.Method != lsproto.MethodInitialize { + return lsproto.ErrServerNotInitialized + } + var params Req // Ignore empty params. if req.Params != nil {