Skip to content

Commit

Permalink
internal/lsp: check for invalid URIs in definition
Browse files Browse the repository at this point in the history
URI.Filename() panics on non-file URIs. Just pass UnknownKind to
beginFileRequest to allow handling template files.

Fixes golang/go#49223

Change-Id: Ic2add49d3d0c04855bf25583712102a50fb425ed
Reviewed-on: https://go-review.googlesource.com/c/tools/+/359734
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
  • Loading branch information
findleyr committed Oct 29, 2021
1 parent 170abdd commit 96715ad
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
12 changes: 12 additions & 0 deletions gopls/internal/regtest/misc/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"testing"

"golang.org/x/tools/internal/lsp/protocol"
. "golang.org/x/tools/internal/lsp/regtest"
"golang.org/x/tools/internal/testenv"

Expand Down Expand Up @@ -277,3 +278,14 @@ package client
env.GoToDefinition("client/client_role_test.go", env.RegexpSearch("client/client_role_test.go", "RoleSetup"))
})
}

// This test exercises a crashing pattern from golang/go#49223.
func TestGoToCrashingDefinition_Issue49223(t *testing.T) {
Run(t, "", func(t *testing.T, env *Env) {
params := &protocol.DefinitionParams{}
params.TextDocument.URI = protocol.DocumentURI("fugitive%3A///Users/user/src/mm/ems/.git//0/pkg/domain/treasury/provider.go")
params.Position.Character = 18
params.Position.Line = 0
env.Editor.Server.Definition(env.Ctx, params)
})
}
3 changes: 1 addition & 2 deletions internal/lsp/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import (
)

func (s *Server) definition(ctx context.Context, params *protocol.DefinitionParams) ([]protocol.Location, error) {
kind := source.DetectLanguage("", params.TextDocument.URI.SpanURI().Filename())
snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, kind)
snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.UnknownKind)
defer release()
if !ok {
return nil, err
Expand Down

0 comments on commit 96715ad

Please sign in to comment.