diff --git a/internal/lsp/handlers.go b/internal/lsp/handlers.go index 175ccf4266..a2425ea0df 100644 --- a/internal/lsp/handlers.go +++ b/internal/lsp/handlers.go @@ -198,9 +198,12 @@ func (s *Server) getCompiledContents(path lsp.DocumentURI, files *persistent.Map } justCompiled, derr, err := development.CompileSchema(file.contents) - if err != nil || derr != nil { + if err != nil { return nil, err } + if derr != nil { + return nil, &jsonrpc2.Error{Code: jsonrpc2.CodeInternalError, Message: derr.String()} + } files.Set(path, trackedFile{file.contents, justCompiled}, nil) return justCompiled, nil diff --git a/internal/lsp/lsp_test.go b/internal/lsp/lsp_test.go index c51b26703c..558d31b3f9 100644 --- a/internal/lsp/lsp_test.go +++ b/internal/lsp/lsp_test.go @@ -148,6 +148,14 @@ func TestDocumentFormat(t *testing.T) { End: lsp.Position{Line: 10000000, Character: 100000000}, }, resp[0].Range) require.Equal(t, "definition user {}", resp[0].NewText) + + // test formatting malformed content without panicing + tester.setFileContents("file:///test", "dfinition user{}") + err, _ := sendAndExpectError(tester, "textDocument/formatting", + lsp.DocumentFormattingParams{ + TextDocument: lsp.TextDocumentIdentifier{URI: "file:///test"}, + }) + require.Error(t, err) } func TestDocumentOpenedClosed(t *testing.T) { @@ -204,4 +212,27 @@ definition resource { require.Equal(t, "definition user {}", resp.Contents.Value) require.Equal(t, "spicedb", resp.Contents.Language) + + // test hovering malformed content without panicing + sendAndReceive[any](tester, "textDocument/didOpen", lsp.DidOpenTextDocumentParams{ + TextDocument: lsp.TextDocumentItem{ + URI: lsp.DocumentURI("file:///test"), + LanguageID: "test", + Version: 1, + Text: `definition user {} + +dfinition resource { + relation viewer: user +} +`, + }, + }) + + err, _ := sendAndExpectError(tester, "textDocument/hover", lsp.TextDocumentPositionParams{ + TextDocument: lsp.TextDocumentIdentifier{ + URI: lsp.DocumentURI("file:///test"), + }, + Position: lsp.Position{Line: 3, Character: 18}, + }) + require.Error(t, err) }