diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b0eedea1..07912d328 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/ls/ls.go b/ls/ls.go index 577eda86f..30a8e35f3 100644 --- a/ls/ls.go +++ b/ls/ls.go @@ -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") } diff --git a/ls/ls_test.go b/ls/ls_test.go index d22f163c1..0de436a61 100644 --- a/ls/ls_test.go +++ b/ls/ls_test.go @@ -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(), ¶ms), "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(), ¶ms), "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")