Skip to content

Commit

Permalink
Check early validation setting before scheduling jobs (#1363)
Browse files Browse the repository at this point in the history
* Introduce new context for valiation settings

* Check context before scheduling jobs
  • Loading branch information
dbanck committed Sep 12, 2023
1 parent e752667 commit 0a1f681
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 22 deletions.
23 changes: 23 additions & 0 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
ctxLsVersion = &contextKey{"language server version"}
ctxProgressToken = &contextKey{"progress token"}
ctxExperimentalFeatures = &contextKey{"experimental features"}
ctxValidationOptions = &contextKey{"validation options"}
)

func missingContextErr(ctxKey *contextKey) *MissingContextErr {
Expand Down Expand Up @@ -165,3 +166,25 @@ func ExperimentalFeatures(ctx context.Context) (settings.ExperimentalFeatures, e
}
return *expFeatures, nil
}

func WithValidationOptions(ctx context.Context, validationOptions *settings.ValidationOptions) context.Context {
return context.WithValue(ctx, ctxValidationOptions, validationOptions)
}

func SetValidationOptions(ctx context.Context, validationOptions settings.ValidationOptions) error {
e, ok := ctx.Value(ctxValidationOptions).(*settings.ValidationOptions)
if !ok {
return missingContextErr(ctxValidationOptions)
}

*e = validationOptions
return nil
}

func ValidationOptions(ctx context.Context) (settings.ValidationOptions, error) {
validationOptions, ok := ctx.Value(ctxValidationOptions).(*settings.ValidationOptions)
if !ok {
return settings.ValidationOptions{}, missingContextErr(ctxValidationOptions)
}
return *validationOptions, nil
}
52 changes: 30 additions & 22 deletions internal/indexer/document_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package indexer
import (
"context"

lsctx "github.com/hashicorp/terraform-ls/internal/context"
"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/job"
"github.com/hashicorp/terraform-ls/internal/schemas"
Expand Down Expand Up @@ -121,20 +122,26 @@ func (idx *Indexer) decodeModule(ctx context.Context, modHandle document.DirHand
}
ids = append(ids, metaId)

// TODO! check if early validation setting is enabled
_, err = idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
return module.SchemaValidation(ctx, idx.modStore, idx.schemaStore, modHandle.Path())
},
Type: op.OpTypeSchemaValidation.String(),
DependsOn: job.IDs{metaId},
IgnoreState: ignoreState,
})
validationOptions, err := lsctx.ValidationOptions(ctx)
if err != nil {
return ids, err
}

if validationOptions.EarlyValidation {
_, err = idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
return module.SchemaValidation(ctx, idx.modStore, idx.schemaStore, modHandle.Path())
},
Type: op.OpTypeSchemaValidation.String(),
DependsOn: job.IDs{metaId},
IgnoreState: ignoreState,
})
if err != nil {
return ids, err
}
}

refTargetsId, err := idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
Expand All @@ -149,18 +156,19 @@ func (idx *Indexer) decodeModule(ctx context.Context, modHandle document.DirHand
}
ids = append(ids, refTargetsId)

// TODO! check if early validation setting is enabled
_, err = idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
return module.ReferenceValidation(ctx, idx.modStore, idx.schemaStore, modHandle.Path())
},
Type: op.OpTypeReferenceValidation.String(),
DependsOn: job.IDs{metaId, refTargetsId},
IgnoreState: ignoreState,
})
if err != nil {
return ids, err
if validationOptions.EarlyValidation {
_, err = idx.jobStore.EnqueueJob(ctx, job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
return module.ReferenceValidation(ctx, idx.modStore, idx.schemaStore, modHandle.Path())
},
Type: op.OpTypeReferenceValidation.String(),
DependsOn: job.IDs{metaId, refTargetsId},
IgnoreState: ignoreState,
})
if err != nil {
return ids, err
}
}

// This job may make an HTTP request, and we schedule it in
Expand Down
4 changes: 4 additions & 0 deletions internal/langserver/handlers/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ func (svc *service) Initialize(ctx context.Context, params lsp.InitializeParams)

// set experimental feature flags
lsctx.SetExperimentalFeatures(ctx, out.Options.ExperimentalFeatures)
// set validation options for jobs
lsctx.SetValidationOptions(ctx, out.Options.Validation)

if len(out.UnusedKeys) > 0 {
jrpc2.ServerFromContext(ctx).Notify(ctx, "window/showMessage", &lsp.ShowMessageParams{
Expand Down Expand Up @@ -200,6 +202,7 @@ func getTelemetryProperties(out *settings.DecodedOptions) map[string]interface{}
"options.terraform.path": false,
"options.terraform.timeout": "",
"options.terraform.logFilePath": false,
"options.validation.earlyValidation": false,
"root_uri": "dir",
"lsVersion": "",
}
Expand All @@ -215,6 +218,7 @@ func getTelemetryProperties(out *settings.DecodedOptions) map[string]interface{}
properties["options.terraform.path"] = len(out.Options.Terraform.Path) > 0
properties["options.terraform.timeout"] = out.Options.Terraform.Timeout
properties["options.terraform.logFilePath"] = len(out.Options.Terraform.LogFilePath) > 0
properties["options.validation.earlyValidation"] = out.Options.Validation.EarlyValidation

return properties
}
Expand Down
7 changes: 7 additions & 0 deletions internal/langserver/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) {
commandPrefix := ""
clientName := ""
var expFeatures settings.ExperimentalFeatures
var validationOptions settings.ValidationOptions

m := map[string]rpch.Func{
"initialize": func(ctx context.Context, req *jrpc2.Request) (interface{}, error) {
Expand All @@ -133,6 +134,7 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) {
ctx = lsctx.WithCommandPrefix(ctx, &commandPrefix)
ctx = ilsp.ContextWithClientName(ctx, &clientName)
ctx = lsctx.WithExperimentalFeatures(ctx, &expFeatures)
ctx = lsctx.WithValidationOptions(ctx, &validationOptions)

version, ok := lsctx.LanguageServerVersion(svc.srvCtx)
if ok {
Expand All @@ -156,13 +158,17 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) {
if err != nil {
return nil, err
}
ctx = lsctx.WithValidationOptions(ctx, &validationOptions)

return handle(ctx, req, svc.TextDocumentDidChange)
},
"textDocument/didOpen": func(ctx context.Context, req *jrpc2.Request) (interface{}, error) {
err := session.CheckInitializationIsConfirmed()
if err != nil {
return nil, err
}
ctx = lsctx.WithValidationOptions(ctx, &validationOptions)

return handle(ctx, req, svc.TextDocumentDidOpen)
},
"textDocument/didClose": func(ctx context.Context, req *jrpc2.Request) (interface{}, error) {
Expand Down Expand Up @@ -325,6 +331,7 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) {
if err != nil {
return nil, err
}
ctx = lsctx.WithValidationOptions(ctx, &validationOptions)

return handle(ctx, req, svc.DidChangeWatchedFiles)
},
Expand Down

0 comments on commit 0a1f681

Please sign in to comment.