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

Implement hcl parse diagnostics #269

Merged
merged 4 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 15 additions & 0 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/terraform-ls/internal/filesystem"
"github.com/hashicorp/terraform-ls/internal/terraform/rootmodule"
"github.com/hashicorp/terraform-ls/internal/watcher"
"github.com/hashicorp/terraform-ls/langserver/diagnostics"
"github.com/sourcegraph/go-lsp"
)

Expand All @@ -33,6 +34,7 @@ var (
ctxRootModuleWalker = &contextKey{"root module walker"}
ctxRootModuleLoader = &contextKey{"root module loader"}
ctxRootDir = &contextKey{"root directory"}
ctxDiags = &contextKey{"diagnostics"}
)

func missingContextErr(ctxKey *contextKey) *MissingContextErr {
Expand Down Expand Up @@ -211,3 +213,16 @@ func RootModuleLoader(ctx context.Context) (rootmodule.RootModuleLoader, error)
}
return w, nil
}

func WithDiagnostics(ctx context.Context, diags *diagnostics.Notifier) context.Context {
return context.WithValue(ctx, ctxDiags, diags)
}

func Diagnostics(ctx context.Context) (*diagnostics.Notifier, error) {
diags, ok := ctx.Value(ctxDiags).(*diagnostics.Notifier)
if !ok {
return nil, missingContextErr(ctxDiags)
}

return diags, nil
}
50 changes: 50 additions & 0 deletions internal/lsp/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package lsp

import (
"github.com/hashicorp/hcl/v2"
lsp "github.com/sourcegraph/go-lsp"
)

func HCLRangeToLSP(hclRng hcl.Range) lsp.Range {
return lsp.Range{
Start: lsp.Position{
Character: hclRng.Start.Column - 1,
Line: hclRng.Start.Line - 1,
},
End: lsp.Position{
Character: hclRng.End.Column - 1,
Line: hclRng.End.Line - 1,
},
}
}

func lspRangeToHCL(lspRng lsp.Range, f File) (*hcl.Range, error) {
startPos, err := lspPositionToHCL(f.Lines(), lspRng.Start)
if err != nil {
return nil, err
}

endPos, err := lspPositionToHCL(f.Lines(), lspRng.End)
if err != nil {
return nil, err
}

return &hcl.Range{
Filename: f.Filename(),
Start: startPos,
End: endPos,
}, nil
}

func HCLSeverityToLSP(severity hcl.DiagnosticSeverity) lsp.DiagnosticSeverity {
var sev lsp.DiagnosticSeverity
switch severity {
case hcl.DiagError:
sev = lsp.Error
case hcl.DiagWarning:
sev = lsp.Warning
case hcl.DiagInvalid:
panic("invalid diagnostic")
}
return sev
}
File renamed without changes.
31 changes: 0 additions & 31 deletions internal/lsp/file_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,37 +54,6 @@ func TextEdits(changes filesystem.DocumentChanges) []lsp.TextEdit {
return edits
}

func HCLRangeToLSP(hclRng hcl.Range) lsp.Range {
return lsp.Range{
Start: lsp.Position{
Character: hclRng.Start.Column - 1,
Line: hclRng.Start.Line - 1,
},
End: lsp.Position{
Character: hclRng.End.Column - 1,
Line: hclRng.End.Line - 1,
},
}
}

func lspRangeToHCL(lspRng lsp.Range, f File) (*hcl.Range, error) {
startPos, err := lspPositionToHCL(f.Lines(), lspRng.Start)
if err != nil {
return nil, err
}

endPos, err := lspPositionToHCL(f.Lines(), lspRng.End)
if err != nil {
return nil, err
}

return &hcl.Range{
Filename: f.Filename(),
Start: startPos,
End: endPos,
}, nil
}

func (fc *contentChange) Text() string {
return fc.text
}
Expand Down
82 changes: 82 additions & 0 deletions langserver/diagnostics/diagnostics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package diagnostics

import (
"context"
"sync"

"github.com/creachadair/jrpc2"
"github.com/hashicorp/hcl/v2/hclparse"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
"github.com/sourcegraph/go-lsp"
)

// documentContext encapsulates the data needed to diagnose the file and push diagnostics to the client
type documentContext struct {
ctx context.Context
uri lsp.DocumentURI
text []byte
}

// Notifier is a type responsible for processing documents and pushing diagnostics to the client
type Notifier struct {
sessCtx context.Context
hclDocs chan documentContext
closeHclDocsOnce sync.Once
}

func NewNotifier(sessCtx context.Context) *Notifier {
hclDocs := make(chan documentContext, 10)
go hclDiags(hclDocs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we need the asynchronicity?

I mean each request is already being processed in a dedicated goroutine and parallelism is controlled in the jRPC library and that's limited to 1 for ordering reasons, so we'd never actually process more than 1 at a time anyway and even if for some reason we do in the future, then I reckon the performance/parallelism would be handled by the jRPC library on handler level?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are push requests limited by the parallelism? Seems like this is just pushing each chan value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is we have a diagnostics worker thread that takes in document requests and pushes diags to the client as soon as possible, but it does not ever block or add milliseconds to the didOpen and didChange handlers (and upcoming didSave for validation).

return &Notifier{hclDocs: hclDocs, sessCtx: sessCtx}
}

// DiagnoseHCL enqueues the document for HCL parsing. Documents will be parsed and notifications delivered in order that
// they are enqueued. Files that are actively changing should be enqueued in order, so that diagnostics remain insync with
// the current content of the file. This is the responsibility of the caller.
func (n *Notifier) DiagnoseHCL(ctx context.Context, uri lsp.DocumentURI, text []byte) {
select {
case <-n.sessCtx.Done():
n.closeHclDocsOnce.Do(func() {
close(n.hclDocs)
})
return
default:
}
n.hclDocs <- documentContext{ctx: ctx, uri: uri, text: text}
}

func hclParse(doc documentContext) []lsp.Diagnostic {
diags := []lsp.Diagnostic{}

_, hclDiags := hclparse.NewParser().ParseHCL(doc.text, string(doc.uri))
for _, hclDiag := range hclDiags {
// only process diagnostics with an attributable spot in the code
if hclDiag.Subject != nil {
msg := hclDiag.Summary
if hclDiag.Detail != "" {
msg += ": " + hclDiag.Detail
}
diags = append(diags, lsp.Diagnostic{
Range: ilsp.HCLRangeToLSP(*hclDiag.Subject),
Severity: ilsp.HCLSeverityToLSP(hclDiag.Severity),
Source: "HCL",
Message: msg,
})
}
}
return diags
}

func hclDiags(docs <-chan documentContext) {
for doc := range docs {
// always push diagnostics, even if the slice is empty, this is how previous diagnostics are cleared
// any push error will result in a panic since this is executing in its own thread and we can't bubble
// an error to a jrpc response
if err := jrpc2.PushNotify(doc.ctx, "textDocument/publishDiagnostics", lsp.PublishDiagnosticsParams{
URI: doc.uri,
Diagnostics: hclParse(doc),
}); err != nil {
panic(err)
}
}
}
50 changes: 50 additions & 0 deletions langserver/diagnostics/diagnostics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package diagnostics

import (
"context"
"testing"
)

func TestDiagnoseHCL_Closes(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
n := NewNotifier(ctx)
cancel()
n.DiagnoseHCL(context.Background(), "", []byte{})
if _, open := <-n.hclDocs; open {
t.Fatal("documents channel should be closed")
}
}

func TestDiagnoseHCL_DoesNotSendAfterClose(t *testing.T) {
defer func() {
if err := recover(); err != nil {
t.Fatal(err)
}
}()
ctx, cancel := context.WithCancel(context.Background())
n := NewNotifier(ctx)
cancel()
n.DiagnoseHCL(context.Background(), "", []byte{})
n.DiagnoseHCL(context.Background(), "", []byte{})
}

func TestHCLParse_ReturnsEmptySliceWhenValid(t *testing.T) {
diags := hclParse(documentContext{ctx: context.Background(), uri: "test", text: hcl(`provider "test" {}`)})
if diags == nil {
t.Fatal("slice needs to be initialized")
}
if len(diags) > 0 {
t.Fatalf("valid hcl should return an empty slice: %v", diags)
}
}

func TestHCLParse_ReturnsDiagsWhenInvalid(t *testing.T) {
diags := hclParse(documentContext{ctx: context.Background(), uri: "test", text: hcl(`provider test" {}`)})
if len(diags) == 0 {
t.Fatal("invalid hcl should return diags")
}
}

func hcl(text string) []byte {
return append([]byte(text), '\n')
}
11 changes: 11 additions & 0 deletions langserver/handlers/did_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ func TextDocumentDidChange(ctx context.Context, params DidChangeTextDocumentPara
return err
}

// now that the file content has been sync'd run diagnostics
diags, err := lsctx.Diagnostics(ctx)
if err != nil {
return err
}
text, err := f.Text()
if err != nil {
return err
}
diags.DiagnoseHCL(ctx, params.TextDocument.URI, text)

cf, err := lsctx.RootModuleCandidateFinder(ctx)
if err != nil {
return err
Expand Down
7 changes: 7 additions & 0 deletions langserver/handlers/did_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import (
)

func (lh *logHandler) TextDocumentDidOpen(ctx context.Context, params lsp.DidOpenTextDocumentParams) error {

diags, err := lsctx.Diagnostics(ctx)
if err != nil {
return err
}
diags.DiagnoseHCL(ctx, params.TextDocument.URI, []byte(params.TextDocument.Text))

fs, err := lsctx.DocumentStorage(ctx)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions langserver/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/terraform-ls/internal/filesystem"
"github.com/hashicorp/terraform-ls/internal/terraform/rootmodule"
"github.com/hashicorp/terraform-ls/internal/watcher"
"github.com/hashicorp/terraform-ls/langserver/diagnostics"
"github.com/hashicorp/terraform-ls/langserver/session"
"github.com/sourcegraph/go-lsp"
)
Expand Down Expand Up @@ -141,6 +142,7 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) {
}

rmLoader := rootmodule.NewRootModuleLoader(svc.sessCtx, svc.modMgr)
diags := diagnostics.NewNotifier(svc.sessCtx)

rootDir := ""

Expand Down Expand Up @@ -173,6 +175,7 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) {
if err != nil {
return nil, err
}
ctx = lsctx.WithDiagnostics(ctx, diags)
ctx = lsctx.WithDocumentStorage(ctx, fs)
ctx = lsctx.WithRootModuleCandidateFinder(ctx, svc.modMgr)
return handle(ctx, req, TextDocumentDidChange)
Expand All @@ -182,6 +185,7 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) {
if err != nil {
return nil, err
}
ctx = lsctx.WithDiagnostics(ctx, diags)
ctx = lsctx.WithDocumentStorage(ctx, fs)
ctx = lsctx.WithRootDirectory(ctx, &rootDir)
ctx = lsctx.WithRootModuleCandidateFinder(ctx, svc.modMgr)
Expand Down