Skip to content

Commit ad473c0

Browse files
committed
internal/lsp: add handling for go.mod files in internal/lsp functions
When we are processing a go.mod file, we are calling go/packages.load when we should not be. It will always return 0 packages since it is not a .go file. This CL adds branching inside each internal/lsp protocol function and also adds a check in snapshot.PackageHandles for the file type and returns an error. This will prevent `go list` from running on go.mod files for now. Updates golang/go#31999 Change-Id: Ic6d0e9b7c81e1f404342b98e10b9c5387adde2ee Reviewed-on: https://go-review.googlesource.com/c/tools/+/210757 Reviewed-by: Rebecca Stambler <[email protected]> Run-TryBot: Rohan Challa <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 4da4485 commit ad473c0

13 files changed

+87
-12
lines changed

internal/lsp/cache/snapshot.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ func (s *snapshot) View() source.View {
6262
}
6363

6464
func (s *snapshot) PackageHandles(ctx context.Context, fh source.FileHandle) ([]source.PackageHandle, error) {
65+
// If the file is a go.mod file, go.Packages.Load will always return 0 packages.
66+
if fh.Identity().Kind == source.Mod {
67+
return nil, errors.Errorf("attempting to get PackageHandles of .mod file %s", fh.Identity().URI)
68+
}
69+
6570
ctx = telemetry.File.With(ctx, fh.Identity().URI)
6671
meta := s.getMetadataForURI(fh.Identity().URI)
6772
// Determine if we need to type-check the package.

internal/lsp/completion.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,17 @@ func (s *Server) completion(ctx context.Context, params *protocol.CompletionPara
2828
if err != nil {
2929
return nil, err
3030
}
31-
options.Completion.FullDocumentation = options.HoverKind == source.FullDocumentation
32-
candidates, surrounding, err := source.Completion(ctx, snapshot, f, params.Position, options.Completion)
31+
32+
var candidates []source.CompletionItem
33+
var surrounding *source.Selection
34+
switch f.Kind() {
35+
case source.Go:
36+
options.Completion.FullDocumentation = options.HoverKind == source.FullDocumentation
37+
candidates, surrounding, err = source.Completion(ctx, snapshot, f, params.Position, options.Completion)
38+
case source.Mod:
39+
candidates, surrounding = nil, nil
40+
}
41+
3342
if err != nil {
3443
log.Print(ctx, "no completions found", tag.Of("At", params.Position), tag.Of("Failure", err))
3544
}

internal/lsp/definition.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ func (s *Server) definition(ctx context.Context, params *protocol.DefinitionPara
2323
if err != nil {
2424
return nil, err
2525
}
26+
if f.Kind() != source.Go {
27+
return nil, nil
28+
}
2629
ident, err := source.Identifier(ctx, snapshot, f, params.Position, source.WidestCheckPackageHandle)
2730
if err != nil {
2831
return nil, err
@@ -50,6 +53,9 @@ func (s *Server) typeDefinition(ctx context.Context, params *protocol.TypeDefini
5053
if err != nil {
5154
return nil, err
5255
}
56+
if f.Kind() != source.Go {
57+
return nil, nil
58+
}
5359
ident, err := source.Identifier(ctx, snapshot, f, params.Position, source.WidestCheckPackageHandle)
5460
if err != nil {
5561
return nil, err

internal/lsp/folding_range.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ func (s *Server) foldingRange(ctx context.Context, params *protocol.FoldingRange
1919
if err != nil {
2020
return nil, err
2121
}
22-
ranges, err := source.FoldingRange(ctx, snapshot, f, view.Options().LineFoldingOnly)
22+
23+
var ranges []*source.FoldingRangeInfo
24+
switch f.Kind() {
25+
case source.Go:
26+
ranges, err = source.FoldingRange(ctx, snapshot, f, view.Options().LineFoldingOnly)
27+
case source.Mod:
28+
ranges = nil
29+
}
30+
2331
if err != nil {
2432
return nil, err
2533
}

internal/lsp/format.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,17 @@ func (s *Server) formatting(ctx context.Context, params *protocol.DocumentFormat
2323
if err != nil {
2424
return nil, err
2525
}
26-
return source.Format(ctx, snapshot, f)
26+
27+
var edits []protocol.TextEdit
28+
switch f.Kind() {
29+
case source.Go:
30+
edits, err = source.Format(ctx, snapshot, f)
31+
case source.Mod:
32+
return nil, nil
33+
}
34+
35+
if err != nil {
36+
return nil, err
37+
}
38+
return edits, nil
2739
}

internal/lsp/highlight.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ func (s *Server) documentHighlight(ctx context.Context, params *protocol.Documen
2525
if err != nil {
2626
return nil, err
2727
}
28-
rngs, err := source.Highlight(ctx, snapshot, f, params.Position)
28+
29+
var rngs []protocol.Range
30+
switch f.Kind() {
31+
case source.Go:
32+
rngs, err = source.Highlight(ctx, snapshot, f, params.Position)
33+
case source.Mod:
34+
return nil, nil
35+
}
36+
2937
if err != nil {
3038
log.Error(ctx, "no highlight", err, telemetry.URI.Of(uri))
3139
}

internal/lsp/hover.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ func (s *Server) hover(ctx context.Context, params *protocol.HoverParams) (*prot
2626
if err != nil {
2727
return nil, err
2828
}
29+
if f.Kind() != source.Go {
30+
return nil, nil
31+
}
2932
ident, err := source.Identifier(ctx, snapshot, f, params.Position, source.WidestCheckPackageHandle)
3033
if err != nil {
3134
return nil, nil

internal/lsp/implementation.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ func (s *Server) implementation(ctx context.Context, params *protocol.Implementa
2525
if err != nil {
2626
return nil, err
2727
}
28-
28+
if f.Kind() != source.Go {
29+
return nil, nil
30+
}
2931
phs, err := snapshot.PackageHandles(ctx, snapshot.Handle(ctx, f))
3032
if err != nil {
3133
return nil, err
3234
}
33-
3435
var (
3536
allLocs []protocol.Location
3637
seen = make(map[protocol.Location]bool)

internal/lsp/link.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ func (s *Server) documentLink(ctx context.Context, params *protocol.DocumentLink
3232
return nil, err
3333
}
3434
fh := view.Snapshot().Handle(ctx, f)
35+
if fh.Identity().Kind == source.Mod {
36+
return nil, nil
37+
}
3538
file, m, _, err := view.Session().Cache().ParseGoHandle(fh, source.ParseFull).Parse(ctx)
3639
if err != nil {
3740
return nil, err
@@ -98,8 +101,8 @@ func findLinksInString(src string, pos token.Pos, view source.View, mapper *prot
98101
end := urlIndex[1]
99102
startPos := token.Pos(int(pos) + start)
100103
endPos := token.Pos(int(pos) + end)
101-
target = src[start:end]
102-
l, err := toProtocolLink(view, mapper, target, startPos, endPos)
104+
target = src[start:end]
105+
l, err := toProtocolLink(view, mapper, target, startPos, endPos)
103106
if err != nil {
104107
return nil, err
105108
}

internal/lsp/references.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ func (s *Server) references(ctx context.Context, params *protocol.ReferenceParam
2626
return nil, err
2727
}
2828
// Find all references to the identifier at the position.
29+
if f.Kind() != source.Go {
30+
return nil, nil
31+
}
2932
ident, err := source.Identifier(ctx, snapshot, f, params.Position, source.WidestCheckPackageHandle)
3033
if err != nil {
31-
return nil, err
34+
return nil, nil
3235
}
3336
references, err := ident.References(ctx)
3437
if err != nil {

internal/lsp/rename.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ func (s *Server) rename(ctx context.Context, params *protocol.RenameParams) (*pr
2323
if err != nil {
2424
return nil, err
2525
}
26+
if f.Kind() != source.Go {
27+
return nil, nil
28+
}
2629
ident, err := source.Identifier(ctx, snapshot, f, params.Position, source.WidestCheckPackageHandle)
2730
if err != nil {
28-
return nil, err
31+
return nil, nil
2932
}
3033
edits, err := ident.Rename(ctx, params.NewName)
3134
if err != nil {
@@ -56,6 +59,9 @@ func (s *Server) prepareRename(ctx context.Context, params *protocol.PrepareRena
5659
if err != nil {
5760
return nil, err
5861
}
62+
if f.Kind() != source.Go {
63+
return nil, nil
64+
}
5965
ident, err := source.Identifier(ctx, snapshot, f, params.Position, source.WidestCheckPackageHandle)
6066
if err != nil {
6167
return nil, nil // ignore errors

internal/lsp/signature_help.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ func (s *Server) signatureHelp(ctx context.Context, params *protocol.SignatureHe
2525
if err != nil {
2626
return nil, err
2727
}
28+
if f.Kind() != source.Go {
29+
return nil, nil
30+
}
2831
info, err := source.SignatureHelp(ctx, snapshot, f, params.Position)
2932
if err != nil {
3033
log.Print(ctx, "no signature help", tag.Of("At", params.Position), tag.Of("Failure", err))

internal/lsp/symbols.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@ func (s *Server) documentSymbol(ctx context.Context, params *protocol.DocumentSy
2929
if err != nil {
3030
return nil, err
3131
}
32-
symbols, err := source.DocumentSymbols(ctx, snapshot, f)
32+
33+
var symbols []protocol.DocumentSymbol
34+
switch f.Kind() {
35+
case source.Go:
36+
symbols, err = source.DocumentSymbols(ctx, snapshot, f)
37+
case source.Mod:
38+
return []protocol.DocumentSymbol{}, nil
39+
}
40+
3341
if err != nil {
3442
log.Error(ctx, "DocumentSymbols failed", err, telemetry.URI.Of(uri))
3543
return []protocol.DocumentSymbol{}, nil

0 commit comments

Comments
 (0)