Skip to content

Commit

Permalink
fix: prevent crash when listing symbols (#273)
Browse files Browse the repository at this point in the history
* add test

* fix: prevent crash when listing symbols
  • Loading branch information
radeksimko authored Oct 9, 2020
1 parent 45b12ce commit 4b1d959
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
7 changes: 4 additions & 3 deletions internal/terraform/lang/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,10 @@ func (p *parser) ParseBlockFromTokens(tBlock ihcl.TokenizedBlock) (ConfigBlock,
// It is probably excessive to be parsing the whole block just for type
// but there is no avoiding it without refactoring the upstream HCL parser
// and it should not hurt the performance too much
//
// We ignore diags as we assume incomplete (invalid) configuration
block, _ := hclsyntax.ParseBlockFromTokens(tBlock.Tokens())
block, diags := hclsyntax.ParseBlockFromTokens(tBlock.Tokens())
if block == nil {
return nil, diags
}

p.logger.Printf("Parsed block type: %q", block.Type)

Expand Down
10 changes: 8 additions & 2 deletions langserver/handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,18 @@ func validTfMockCalls() exec.ExecutorFactory {
},
ReturnArguments: []interface{}{
version.Must(version.NewVersion("0.12.0")),
map[string]*version.Version{},
nil,
},
},
{
Method: "ProvidersSchema",
Method: "GetExecPath",
Repeatability: 1,
ReturnArguments: []interface{}{
"",
},
},
{
Method: "ProviderSchemas",
Repeatability: 1,
Arguments: []interface{}{
mock.AnythingOfType(""),
Expand Down
54 changes: 54 additions & 0 deletions langserver/handlers/symbols_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package handlers

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-ls/internal/terraform/rootmodule"
"github.com/hashicorp/terraform-ls/langserver"
)

func TestLangServer_symbols_basic(t *testing.T) {
tmpDir := TempDir(t)
InitPluginCache(t, tmpDir.Dir())

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
RootModules: map[string]*rootmodule.RootModuleMock{
tmpDir.Dir(): {
TfExecFactory: validTfMockCalls(),
},
},
}))
stop := ls.Start(t)
defer stop()

ls.Call(t, &langserver.CallRequest{
Method: "initialize",
ReqParams: fmt.Sprintf(`{
"capabilities": {},
"rootUri": %q,
"processId": 12345
}`, tmpDir.URI())})
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\"\n\n}\n",
"uri": "%s/main.tf"
}
}`, tmpDir.URI())})

ls.Call(t, &langserver.CallRequest{
Method: "textDocument/documentSymbol",
ReqParams: fmt.Sprintf(`{
"textDocument": {
"uri": "%s/main.tf"
}
}`, tmpDir.URI())})
}

0 comments on commit 4b1d959

Please sign in to comment.