internal/lsp: handle non-file:// URIs gracefully

Rather than panicking, we should fail to initialize the server.
Context: https://github.com/microsoft/vscode-go/issues/3081
Change-Id: Ic4622d435dffb77b72dc1e0214f0a1d181a6f767
Reviewed-on: https://go-review.googlesource.com/c/tools/+/227032
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Rebecca Stambler 2020-04-02 15:32:48 -04:00
parent 8f5be0d382
commit 72cf467e29
1 changed files with 5 additions and 2 deletions

View File

@ -7,6 +7,7 @@ package lsp
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"path"
@ -18,7 +19,6 @@ import (
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/span"
"golang.org/x/tools/internal/telemetry/event"
errors "golang.org/x/xerrors"
)
func (s *Server) initialize(ctx context.Context, params *protocol.ParamInitialize) (*protocol.InitializeResult, error) {
@ -40,6 +40,9 @@ func (s *Server) initialize(ctx context.Context, params *protocol.ParamInitializ
source.SetOptions(&options, params.InitializationOptions)
options.ForClientCapabilities(params.Capabilities)
if !params.RootURI.SpanURI().IsFile() {
return nil, fmt.Errorf("unsupported URI scheme: %v (gopls only supports file URIs)", params.RootURI)
}
s.pendingFolders = params.WorkspaceFolders
if len(s.pendingFolders) == 0 {
if params.RootURI != "" {
@ -50,7 +53,7 @@ func (s *Server) initialize(ctx context.Context, params *protocol.ParamInitializ
} else {
// No folders and no root--we are in single file mode.
// TODO: https://golang.org/issue/34160.
return nil, errors.Errorf("gopls does not yet support editing a single file. Please open a directory.")
return nil, errors.New("gopls does not yet support editing a single file. Please open a directory.")
}
}