Skip to content

Commit

Permalink
fix(ls): panic when failing to load root config. (#1441)
Browse files Browse the repository at this point in the history
## What this PR does / why we need it:

When an error happens while looking for the root config, the language
server panics with the stack trace below:

```
2024-02-10T14:38:52Z DBG handling request. action=server.Handler() method=textDocument/didOpen params={"textDocument":{"languageId":"terramate","text":"terramate {\n    config {\n        git {\n            default_branch = \"trunk\"\n        }\n    }\n}\n","uri":"file:///Users/tiagodemoura/src/vscode-terramate/testFixture/invalid/multiple-tm-config-git/tm2.tm","version":1}} workspace=/Users/tiagodemoura/src/vscode-terramate
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x1012a3d04]

goroutine 8 [running]:
github.com/terramate-io/terramate/config.(*Root).Tree(...)
	/Users/tiagodemoura/src/terramate/config/config.go:125
github.com/terramate-io/terramate/ls.(*Server).checkFiles(0x140001102d0, {0x140002ce380, 0x2, 0x101287df4?}, {0x14000024697, 0x5a}, {0x1400001ab40, 0x5e})
	/Users/tiagodemoura/src/terramate/ls/ls.go:370 +0x64
github.com/terramate-io/terramate/ls.(*Server).checkAndReply(0x14000024690?, {0x101414268, 0x140001d1040}, 0x140002ce320, {0x14000024697, 0x5a}, {0x1400001ab40, 0x5e})
	/Users/tiagodemoura/src/terramate/ls/ls.go:324 +0x10c
github.com/terramate-io/terramate/ls.(*Server).handleDocumentOpen(0x73?, {0x101414268, 0x140001d1040}, 0x1400014cab8?, {0x1264f6e98, 0x140002c2e70}, {{0x101412b48, 0x14000027ff0}, 0xff, {0x0, ...}, ...})
	/Users/tiagodemoura/src/terramate/ls/ls.go:184 +0xf4
github.com/terramate-io/terramate/ls.(*Server).Handler(0x140001102d0, {0x101414268, 0x140001d1040}, 0x140002ce320, {0x1264f6e98?, 0x140002c2e70?})
	/Users/tiagodemoura/src/terramate/ls/ls.go:90 +0x418
go.lsp.dev/jsonrpc2.(*conn).run(0x140001d2ff0, {0x101414268, 0x140001d1040}, 0x140001e81f0)
	/Users/tiagodemoura/go/pkg/mod/go.lsp.dev/jsonrpc2@v0.10.0/conn.go:206 +0x258
created by go.lsp.dev/jsonrpc2.(*conn).Go in goroutine 1
	/Users/tiagodemoura/go/pkg/mod/go.lsp.dev/jsonrpc2@v0.10.0/conn.go:189 +0xa8
[Error - 14:38:52] Connection to server got closed. Server will not be restarted.
```

## Which issue(s) this PR fixes:

## Special notes for your reviewer:

## Does this PR introduce a user-facing change?
```
yes, fixes a LS bug.
```
  • Loading branch information
i4ki committed Feb 10, 2024
2 parents 828c852 + b4d54d8 commit fd4bd4c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Given a version number `MAJOR.MINOR.PATCH`, we increment the:
- Add `terramate.config.generate.hcl_magic_header_comment_style` option for setting the generated comment style.
- Add support for formatting specific files and stdin (`terramate fmt [file...]` or `terramate fmt -`).

### Fixed

- Fix language server panic when root directory contain errors.

## 0.4.5

### Added
Expand Down
8 changes: 6 additions & 2 deletions ls/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,16 @@ func listFiles(fromFile string) ([]string, error) {
// is handled separately because it can be unsaved.
func (s *Server) checkFiles(files []string, currentFile string, currentContent string) error {
dir := filepath.Dir(currentFile)
root, rootdir, found, _ := config.TryLoadConfig(dir)
var experiments []string
root, rootdir, found, err := config.TryLoadConfig(dir)
if err == nil {
experiments = root.Tree().Node.Experiments()
}
if !found {
rootdir = s.workspace
}

parser, err := hcl.NewTerramateParser(rootdir, dir, root.Tree().Node.Experiments()...)
parser, err := hcl.NewTerramateParser(rootdir, dir, experiments...)
if err != nil {
return errors.E(err, "failed to create terramate parser")
}
Expand Down
29 changes: 29 additions & 0 deletions ls/ls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,35 @@ func TestDocumentOpen(t *testing.T) {
params.URI.Filename())
}

func TestDocumentRegressionErrorLoadingRootConfig(t *testing.T) {
t.Parallel()
f := lstest.Setup(t)

file := f.Sandbox.RootEntry().CreateFile("test.tm", "attr = 1")
f.Editor.CheckInitialize(f.Sandbox.RootDir())
f.Editor.Open("test.tm")

// root.config.tm diagnostic
r := <-f.Editor.Requests
assert.EqualStrings(t, "textDocument/publishDiagnostics", r.Method(),
"unexpected notification request")

var params lsp.PublishDiagnosticsParams
assert.NoError(t, json.Unmarshal(r.Params(), &params), "unmarshaling params")
assert.EqualInts(t, 0, len(params.Diagnostics))
assert.EqualStrings(t, filepath.Join(f.Sandbox.RootDir(), "root.config.tm"), params.URI.Filename())

// test.tm diagnostic
r = <-f.Editor.Requests
assert.EqualStrings(t, "textDocument/publishDiagnostics", r.Method(),
"unexpected notification request")

params = lsp.PublishDiagnosticsParams{}
assert.NoError(t, json.Unmarshal(r.Params(), &params), "unmarshaling params")
assert.EqualInts(t, 1, len(params.Diagnostics))
assert.EqualStrings(t, file.Path(), params.URI.Filename())
}

func TestDocumentChange(t *testing.T) {
t.Skip("not ready")

Expand Down

0 comments on commit fd4bd4c

Please sign in to comment.