Skip to content

Commit 57c3a74

Browse files
committed
internal/lsp: get file URI from beginFileRequest in SemanticTokens
Unguarded calls to span.URI.Filename() can panic. beginFileRequest handles this, so use the URI of the returned FileHandle instead. Fixes golang/vscode-go#1498 Change-Id: Ie48c27854e4a8ed8cca52ff6547ff580eccb5fd5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/319529 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 8287d5d commit 57c3a74

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package misc
6+
7+
import (
8+
"testing"
9+
10+
"golang.org/x/tools/internal/lsp/protocol"
11+
. "golang.org/x/tools/internal/lsp/regtest"
12+
)
13+
14+
func TestBadURICrash_VSCodeIssue1498(t *testing.T) {
15+
const src = `
16+
-- go.mod --
17+
module example.com
18+
19+
go 1.12
20+
21+
-- main.go --
22+
package main
23+
24+
func main() {}
25+
26+
`
27+
WithOptions(
28+
Modes(Singleton),
29+
EditorConfig{
30+
AllExperiments: true,
31+
},
32+
).Run(t, src, func(t *testing.T, env *Env) {
33+
params := &protocol.SemanticTokensParams{}
34+
const badURI = "http://foo"
35+
params.TextDocument.URI = badURI
36+
// This call panicked in the past: golang/vscode-go#1498.
37+
if _, err := env.Editor.Server.SemanticTokensFull(env.Ctx, params); err != nil {
38+
// Requests to an invalid URI scheme shouldn't result in an error, we
39+
// simply don't support this so return empty result. This could be
40+
// changed, but for now assert on the current behavior.
41+
t.Errorf("SemanticTokensFull(%q): %v", badURI, err)
42+
}
43+
})
44+
}

internal/lsp/semantic.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ func (s *Server) computeSemanticTokens(ctx context.Context, td protocol.TextDocu
4949
ans := protocol.SemanticTokens{
5050
Data: []uint32{},
5151
}
52-
kind := source.DetectLanguage("", td.URI.SpanURI().Filename())
53-
snapshot, _, ok, release, err := s.beginFileRequest(ctx, td.URI, kind)
52+
snapshot, fh, ok, release, err := s.beginFileRequest(ctx, td.URI, source.UnknownKind)
5453
defer release()
5554
if !ok {
5655
return nil, err
@@ -61,7 +60,7 @@ func (s *Server) computeSemanticTokens(ctx context.Context, td protocol.TextDocu
6160
// the client won't remember the wrong answer
6261
return nil, errors.Errorf("semantictokens are disabled")
6362
}
64-
if kind == source.Tmpl {
63+
if fh.Kind() == source.Tmpl {
6564
// this is a little cumbersome to avoid both exporting 'encoded' and its methods
6665
// and to avoid import cycles
6766
e := &encoded{
@@ -76,14 +75,14 @@ func (s *Server) computeSemanticTokens(ctx context.Context, td protocol.TextDocu
7675
data := func() ([]uint32, error) {
7776
return e.Data()
7877
}
79-
return template.SemanticTokens(ctx, snapshot, td.URI.SpanURI(), add, data)
78+
return template.SemanticTokens(ctx, snapshot, fh.URI(), add, data)
8079
}
81-
pkg, err := snapshot.PackageForFile(ctx, td.URI.SpanURI(), source.TypecheckFull, source.WidestPackage)
80+
pkg, err := snapshot.PackageForFile(ctx, fh.URI(), source.TypecheckFull, source.WidestPackage)
8281
if err != nil {
8382
return nil, err
8483
}
8584
info := pkg.GetTypesInfo()
86-
pgf, err := pkg.File(td.URI.SpanURI())
85+
pgf, err := pkg.File(fh.URI())
8786
if err != nil {
8887
return nil, err
8988
}
@@ -92,7 +91,7 @@ func (s *Server) computeSemanticTokens(ctx context.Context, td protocol.TextDocu
9291
}
9392
if rng == nil && len(pgf.Src) > maxFullFileSize {
9493
err := fmt.Errorf("semantic tokens: file %s too large for full (%d>%d)",
95-
td.URI.SpanURI().Filename(), len(pgf.Src), maxFullFileSize)
94+
fh.URI().Filename(), len(pgf.Src), maxFullFileSize)
9695
return nil, err
9796
}
9897
e := &encoded{

0 commit comments

Comments
 (0)