Skip to content

Commit 82be0b4

Browse files
findleyrgopherbot
authored andcommitted
gopls/internal/server: return a non-nil slice for empty token result
Some clients encounter errors in the presence of a nil semantic tokens data slice. Since the field is not marked optional, it seems most appropriate to return a non-nil empty slice when semantic tokens are disabled. Fixes golang/go#67885 Change-Id: I85b1e856e0829d73508edae06b373e135340d9ac Reviewed-on: https://go-review.googlesource.com/c/tools/+/591415 Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 413a491 commit 82be0b4

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

gopls/internal/server/semantic.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,25 @@ func (s *server) semanticTokens(ctx context.Context, td protocol.TextDocumentIde
3232
return nil, err
3333
}
3434
defer release()
35-
if !snapshot.Options().SemanticTokens {
36-
// Note: returning new(protocol.SemanticTokens) is necessary here to
37-
// invalidate semantic tokens in VS Code (and perhaps other editors).
38-
// Previously, an error was returned here to achieve the same effect, but
39-
// that had the side effect of very noisy "semantictokens are disabled"
40-
// logs on every keystroke.
41-
return new(protocol.SemanticTokens), nil
42-
}
43-
44-
switch snapshot.FileKind(fh) {
45-
case file.Tmpl:
46-
return template.SemanticTokens(ctx, snapshot, fh.URI())
4735

48-
case file.Go:
49-
return golang.SemanticTokens(ctx, snapshot, fh, rng)
50-
51-
default:
52-
// TODO(adonovan): should return an error!
53-
return nil, nil // empty result
36+
if snapshot.Options().SemanticTokens {
37+
switch snapshot.FileKind(fh) {
38+
case file.Tmpl:
39+
return template.SemanticTokens(ctx, snapshot, fh.URI())
40+
case file.Go:
41+
return golang.SemanticTokens(ctx, snapshot, fh, rng)
42+
}
5443
}
44+
45+
// Not enabled, or unsupported file type: return empty result.
46+
//
47+
// Returning an empty response is necessary to invalidate
48+
// semantic tokens in VS Code (and perhaps other editors).
49+
// Previously, we returned an error, but that had the side effect
50+
// of noisy "semantictokens are disabled" logs on every keystroke.
51+
//
52+
// We must return a non-nil Data slice for JSON serialization.
53+
// We do not return an empty field with "omitempty" set,
54+
// as it is not marked optional in the protocol (golang/go#67885).
55+
return &protocol.SemanticTokens{Data: []uint32{}}, nil
5556
}

0 commit comments

Comments
 (0)