Skip to content

Commit 96715ad

Browse files
committed
internal/lsp: check for invalid URIs in definition
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 <[email protected]> Run-TryBot: Robert Findley <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent 170abdd commit 96715ad

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

gopls/internal/regtest/misc/definition_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"testing"
1111

12+
"golang.org/x/tools/internal/lsp/protocol"
1213
. "golang.org/x/tools/internal/lsp/regtest"
1314
"golang.org/x/tools/internal/testenv"
1415

@@ -277,3 +278,14 @@ package client
277278
env.GoToDefinition("client/client_role_test.go", env.RegexpSearch("client/client_role_test.go", "RoleSetup"))
278279
})
279280
}
281+
282+
// This test exercises a crashing pattern from golang/go#49223.
283+
func TestGoToCrashingDefinition_Issue49223(t *testing.T) {
284+
Run(t, "", func(t *testing.T, env *Env) {
285+
params := &protocol.DefinitionParams{}
286+
params.TextDocument.URI = protocol.DocumentURI("fugitive%3A///Users/user/src/mm/ems/.git//0/pkg/domain/treasury/provider.go")
287+
params.Position.Character = 18
288+
params.Position.Line = 0
289+
env.Editor.Server.Definition(env.Ctx, params)
290+
})
291+
}

internal/lsp/definition.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import (
1313
)
1414

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

0 commit comments

Comments
 (0)