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 1 commit
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
}
91 changes: 91 additions & 0 deletions langserver/diagnostics/diagnostics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package diagnostics

import (
"context"
"sync"

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

type documentContext struct {
ctx context.Context
uri lsp.DocumentURI
text []byte
}

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}
}

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
case n.hclDocs <- documentContext{ctx: ctx, uri: uri, text: text}:
}
}

func hclDiags(docs <-chan documentContext) {
for d := range docs {
diags := []lsp.Diagnostic{}

_, hclDiags := hclparse.NewParser().ParseHCL(d.text, string(d.uri))
for _, hclDiag := range hclDiags {
if hclDiag.Subject != nil {
diags = append(diags, lsp.Diagnostic{
Range: ilsp.HCLRangeToLSP(*hclDiag.Subject),
Severity: hclSeverityToLSP(hclDiag.Severity),
Source: "HCL",
Message: lspMessage(hclDiag),
})
}
}

if err := jrpc2.PushNotify(d.ctx, "textDocument/publishDiagnostics", lsp.PublishDiagnosticsParams{
URI: d.uri,
Diagnostics: diags,
}); !acceptableError(err) {
panic(err)
}
}
}

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
}

func lspMessage(diag *hcl.Diagnostic) string {
m := diag.Summary
if diag.Detail != "" {
m += ": " + diag.Detail
}
return m
}
appilon marked this conversation as resolved.
Show resolved Hide resolved

func acceptableError(err error) bool {
return true
}
10 changes: 10 additions & 0 deletions langserver/handlers/did_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ func TextDocumentDidChange(ctx context.Context, params DidChangeTextDocumentPara
return err
}

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