From 72cf467e29a22aff24d0bfba5a7d35ae44e5939b Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Thu, 2 Apr 2020 15:32:48 -0400 Subject: [PATCH] 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 Reviewed-by: Heschi Kreinick --- internal/lsp/general.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/internal/lsp/general.go b/internal/lsp/general.go index c9d2efb19c..f253dec96f 100644 --- a/internal/lsp/general.go +++ b/internal/lsp/general.go @@ -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.") } }