From cf83efe03cf89b09f33be7e464f1c95f6c3393aa Mon Sep 17 00:00:00 2001 From: Pontus Leitzler Date: Sun, 16 Aug 2020 20:45:47 +0200 Subject: [PATCH] internal/lsp: return err if ExecuteCommand prerequisites aren't met Some Commands require that the buffer is saved before running them (i.e. generate, test and toggle details). If the buffer isn't saved gopls sends a ShowMessage request to the client. Before this change it did not return any error from the ExecuteCommand request itself (unless ShowMessage failed). A progress token can be provided by the client as a part of the ExecuteCommand request, and that is, according to the LSP spec one way to start a WorkDoneProgress. At this point the client expects that gopls send progress updates. With this change, ExecuteCommand now return an error if the buffer isn't saved, so that the client know that it shouldn't expect any progress updates with this specific token. Change-Id: I8a7af20a0c1532fc4ad03efd4e04bfb1a6871644 Reviewed-on: https://go-review.googlesource.com/c/tools/+/248766 Run-TryBot: Pontus Leitzler TryBot-Result: Gobot Gobot Reviewed-by: Robert Findley --- internal/lsp/command.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/lsp/command.go b/internal/lsp/command.go index 537f94d04e..c8cb44864a 100644 --- a/internal/lsp/command.go +++ b/internal/lsp/command.go @@ -53,10 +53,12 @@ func (s *Server) executeCommand(ctx context.Context, params *protocol.ExecuteCom switch params.Command { case source.CommandTest.Name, source.CommandGenerate.Name, source.CommandToggleDetails.Name: // TODO(PJW): for Toggle, not an error if it is being disabled - return nil, s.client.ShowMessage(ctx, &protocol.ShowMessageParams{ + err := fmt.Errorf("cannot run command %s: unsaved files in the view", params.Command) + s.client.ShowMessage(ctx, &protocol.ShowMessageParams{ Type: protocol.Error, - Message: fmt.Sprintf("cannot run command %s: unsaved files in the view", params.Command), + Message: err.Error(), }) + return nil, err } } // If the command has a suggested fix function available, use it and apply