Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LS panic on missing symbols in client capabilities #1619

Merged
merged 3 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions internal/langserver/handlers/symbols_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,82 @@ func TestLangServer_symbols_basic(t *testing.T) {
]
}`)
}

func TestLangServer_symbols_missing(t *testing.T) {
tmpDir := TempDir(t)
InitPluginCache(t, tmpDir.Path())

ss, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}
wc := walker.NewWalkerCollector()

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
tmpDir.Path(): validTfMockCalls(),
},
},
StateStore: ss,
WalkerCollector: wc,
}))
stop := ls.Start(t)
defer stop()

ls.Call(t, &langserver.CallRequest{
Method: "initialize",
ReqParams: fmt.Sprintf(`{
"capabilities": {
"textDocument": {
"documentSymbol": {
"hierarchicalDocumentSymbolSupport": true,
"labelSupport": true
}
}
},
"rootUri": %q,
"processId": 12345
}`, tmpDir.URI)})
waitForWalkerPath(t, ss, wc, tmpDir)
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
})
ls.Call(t, &langserver.CallRequest{
Method: "textDocument/didOpen",
ReqParams: fmt.Sprintf(`{
"textDocument": {
"version": 0,
"languageId": "terraform",
"text": "provider \"github\" {}",
"uri": "%s/main.tf"
}
}`, tmpDir.URI)})
waitForAllJobs(t, ss)

ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/documentSymbol",
ReqParams: fmt.Sprintf(`{
"textDocument": {
"uri": "%s/main.tf"
}
}`, tmpDir.URI)}, `{
"jsonrpc": "2.0",
"id": 3,
"result": [
{
"name": "provider \"github\"",
"kind": 5,
"range": {
"start": {"line": 0, "character": 0},
"end": {"line": 0, "character": 20}
},
"selectionRange": {
"start": {"line": 0, "character": 0},
"end": {"line": 0, "character": 20}
}
}
]
}`)
}
138 changes: 138 additions & 0 deletions internal/langserver/handlers/workspace_symbol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,141 @@ func TestLangServer_workspace_symbol_basic(t *testing.T) {
]
}`, tmpDir.URI))
}

func TestLangServer_workspace_symbol_missing(t *testing.T) {
tmpDir := TempDir(t)
InitPluginCache(t, tmpDir.Path())

ss, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}
wc := walker.NewWalkerCollector()

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
tmpDir.Path(): validTfMockCalls(),
},
},
StateStore: ss,
WalkerCollector: wc,
}))
stop := ls.Start(t)
defer stop()

ls.Call(t, &langserver.CallRequest{
Method: "initialize",
ReqParams: fmt.Sprintf(`{
"capabilities": {
"workspace": {
"symbol": {
}
}
},
"rootUri": %q,
"processId": 12345
}`, tmpDir.URI)})
waitForWalkerPath(t, ss, wc, tmpDir)
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
})
ls.Call(t, &langserver.CallRequest{
Method: "textDocument/didOpen",
ReqParams: fmt.Sprintf(`{
"textDocument": {
"version": 0,
"languageId": "terraform",
"text": "provider \"github\" {}",
"uri": "%s/first.tf"
}
}`, tmpDir.URI)})
ls.Call(t, &langserver.CallRequest{
Method: "textDocument/didOpen",
ReqParams: fmt.Sprintf(`{
"textDocument": {
"version": 0,
"languageId": "terraform",
"text": "provider \"google\" {}",
"uri": "%s/second.tf"
}
}`, tmpDir.URI)})
ls.Call(t, &langserver.CallRequest{
Method: "textDocument/didOpen",
ReqParams: fmt.Sprintf(`{
"textDocument": {
"version": 0,
"languageId": "terraform",
"text": "myblock \"custom\" {}",
"uri": "%s/blah/third.tf"
}
}`, tmpDir.URI)})
waitForAllJobs(t, ss)

ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "workspace/symbol",
ReqParams: `{
"query": ""
}`}, fmt.Sprintf(`{
"jsonrpc": "2.0",
"id": 5,
"result": [
{
"location": {
"uri": "%s/first.tf",
"range": {
"start": {"line": 0, "character": 0},
"end": {"line": 0, "character": 20}
}
},
"name": "provider \"github\"",
"kind": 5
},
{
"location": {
"uri": "%s/second.tf",
"range": {
"start": {"line": 0, "character": 0},
"end": {"line": 0, "character": 20}
}
},
"name": "provider \"google\"",
"kind": 5
},
{
"location": {
"uri": "%s/blah/third.tf",
"range": {
"start": {"line": 0, "character": 0},
"end": {"line": 0, "character": 19}
}
},
"name": "myblock \"custom\"",
"kind": 5
}
]
}`, tmpDir.URI, tmpDir.URI, tmpDir.URI))

ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "workspace/symbol",
ReqParams: `{
"query": "myb"
}`}, fmt.Sprintf(`{
"jsonrpc": "2.0",
"id": 6,
"result": [
{
"location": {
"uri": "%s/blah/third.tf",
"range": {
"start": {"line": 0, "character": 0},
"end": {"line": 0, "character": 19}
}
},
"name": "myblock \"custom\"",
"kind": 5
}
]
}`, tmpDir.URI))
}
38 changes: 36 additions & 2 deletions internal/lsp/symbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,39 @@ import (
"github.com/zclconf/go-cty/cty"
)

// defaultSymbols is the list of symbols that were supported by the initial
// version of the LSP. This list is used as a fallback when the client does
// not provide a list of supported symbols.
var defaultSymbols = []lsp.SymbolKind{
lsp.File,
lsp.Module,
lsp.Namespace,
lsp.Package,
lsp.Class,
lsp.Method,
lsp.Property,
lsp.Field,
lsp.Constructor,
lsp.Enum,
lsp.Interface,
lsp.Function,
lsp.Variable,
lsp.Constant,
lsp.String,
lsp.Number,
lsp.Boolean,
lsp.Array,
}

func WorkspaceSymbols(sbs []decoder.Symbol, caps *lsp.WorkspaceSymbolClientCapabilities) []lsp.SymbolInformation {
symbols := make([]lsp.SymbolInformation, len(sbs))
supportedSymbols := defaultSymbols
if caps != nil && caps.SymbolKind != nil {
supportedSymbols = caps.SymbolKind.ValueSet
}
ansgarm marked this conversation as resolved.
Show resolved Hide resolved

for i, s := range sbs {
kind, ok := symbolKind(s, caps.SymbolKind.ValueSet)
kind, ok := symbolKind(s, supportedSymbols)
if !ok {
// skip symbol not supported by client
continue
Expand Down Expand Up @@ -50,7 +79,12 @@ func DocumentSymbols(sbs []decoder.Symbol, caps lsp.DocumentSymbolClientCapabili
}

func documentSymbol(symbol decoder.Symbol, caps lsp.DocumentSymbolClientCapabilities) (lsp.DocumentSymbol, bool) {
kind, ok := symbolKind(symbol, caps.SymbolKind.ValueSet)
supportedSymbols := defaultSymbols
if caps.SymbolKind != nil {
supportedSymbols = caps.SymbolKind.ValueSet
}

kind, ok := symbolKind(symbol, supportedSymbols)
if !ok {
return lsp.DocumentSymbol{}, false
}
Expand Down
Loading