Skip to content

Commit

Permalink
Enable variable validation based on module metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed May 8, 2024
1 parent 444ef4b commit b6ab5a2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/features/modules/modules_feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/terraform-ls/internal/algolia"
"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/eventbus"
fdecoder "github.com/hashicorp/terraform-ls/internal/features/modules/decoder"
"github.com/hashicorp/terraform-ls/internal/features/modules/hooks"
Expand Down Expand Up @@ -266,3 +267,7 @@ func (f *ModulesFeature) Telemetry(path string) map[string]interface{} {

return properties
}

func (f *ModulesFeature) MetadataReady(dir document.DirHandle) (<-chan struct{}, bool, error) {
return f.store.MetadataReady(dir)
}
14 changes: 14 additions & 0 deletions internal/features/modules/state/module_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,3 +727,17 @@ func (s *ModuleStore) queueModuleChange(oldMod, newMod *ModuleRecord) error {

return s.changeStore.QueueChange(modHandle, changes)
}

func (f *ModuleStore) MetadataReady(dir document.DirHandle) (<-chan struct{}, bool, error) {
rTxn := f.db.Txn(false)

wCh, recordObj, err := rTxn.FirstWatch(f.tableName, "module_state", dir.Path(), op.OpStateLoaded)
if err != nil {
return nil, false, err
}
if recordObj != nil {
return wCh, true, nil
}

return wCh, false, nil
}
9 changes: 9 additions & 0 deletions internal/features/modules/state/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ var dbSchema = &memdb.DBSchema{
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "path"},
},
"module_state": {
Name: "module_state",
Indexer: &memdb.CompoundIndex{
Indexes: []memdb.Indexer{
&memdb.StringFieldIndex{Field: "path"},
&memdb.UintFieldIndex{Field: "MetaState"},
},
},
},
},
},
moduleIdsTableName: {
Expand Down
2 changes: 2 additions & 0 deletions internal/features/variables/decoder/path_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/features/variables/state"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
tfmod "github.com/hashicorp/terraform-schema/module"
Expand All @@ -20,6 +21,7 @@ type StateReader interface {

type ModuleReader interface {
ModuleInputs(modPath string) (map[string]tfmod.Variable, error)
MetadataReady(dir document.DirHandle) (<-chan struct{}, bool, error)
}

type PathReader struct {
Expand Down
33 changes: 33 additions & 0 deletions internal/features/variables/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package variables
import (
"context"

lsctx "github.com/hashicorp/terraform-ls/internal/context"
"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/features/variables/ast"
"github.com/hashicorp/terraform-ls/internal/features/variables/jobs"
Expand Down Expand Up @@ -74,5 +75,37 @@ func (f *VariablesFeature) didOpen(ctx context.Context, dir document.DirHandle,
}
ids = append(ids, varsRefsId)

validationOptions, err := lsctx.ValidationOptions(ctx)
if err != nil {
return ids, err
}
if validationOptions.EnableEnhancedValidation {
wCh, moduleReady, err := f.moduleFeature.MetadataReady(dir)
if err != nil {
return ids, err
}
if !moduleReady {
select {
// Wait for module to be ready
case <-wCh:
case <-ctx.Done(): // TODO can we cancel via context here?
return ids, ctx.Err()
}
}

_, err = f.jobStore.EnqueueJob(ctx, job.Job{
Dir: dir,
Func: func(ctx context.Context) error {
return jobs.SchemaVariablesValidation(ctx, f.store, f.moduleFeature, path)
},
Type: op.OpTypeSchemaVarsValidation.String(),
DependsOn: job.IDs{parseVarsId},
IgnoreState: true,
})
if err != nil {
return ids, err
}
}

return ids, nil
}

0 comments on commit b6ab5a2

Please sign in to comment.