Skip to content

Commit

Permalink
Use any schema when collecting variables
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed May 7, 2024
1 parent ccd3209 commit 444ef4b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
3 changes: 2 additions & 1 deletion internal/features/variables/decoder/path_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ModuleReader interface {
type PathReader struct {
StateReader StateReader
ModuleReader ModuleReader
UseAnySchema bool
}

var _ decoder.PathReader = &PathReader{}
Expand Down Expand Up @@ -53,5 +54,5 @@ func (pr *PathReader) PathContext(path lang.Path) (*decoder.PathContext, error)
if err != nil {
return nil, err
}
return variablePathContext(mod, pr.ModuleReader)
return variablePathContext(mod, pr.ModuleReader, pr.UseAnySchema)
}
19 changes: 13 additions & 6 deletions internal/features/variables/decoder/variable_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,34 @@ package decoder
import (
"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform-ls/internal/features/variables/ast"
"github.com/hashicorp/terraform-ls/internal/features/variables/state"
tfschema "github.com/hashicorp/terraform-schema/schema"
)

func variablePathContext(mod *state.VariableRecord, moduleReader ModuleReader) (*decoder.PathContext, error) {
func variablePathContext(mod *state.VariableRecord, moduleReader ModuleReader, useAnySchema bool) (*decoder.PathContext, error) {
variables, _ := moduleReader.ModuleInputs(mod.Path())
schema, err := tfschema.SchemaForVariables(variables, mod.Path())
if err != nil {
return nil, err
bodySchema := &schema.BodySchema{}
if useAnySchema {
bodySchema = tfschema.AnySchemaForVariableCollection(mod.Path())
} else {
var err error
bodySchema, err = tfschema.SchemaForVariables(variables, mod.Path())
if err != nil {
return nil, err
}
}

pathCtx := &decoder.PathContext{
Schema: schema,
Schema: bodySchema,
ReferenceOrigins: make(reference.Origins, 0),
ReferenceTargets: make(reference.Targets, 0),
Files: make(map[string]*hcl.File),
}

if len(schema.Attributes) > 0 {
if len(bodySchema.Attributes) > 0 {
// Only validate if this is actually a module
// as we may come across standalone tfvars files
// for which we have no context.
Expand Down
1 change: 1 addition & 0 deletions internal/features/variables/jobs/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func DecodeVarsReferences(ctx context.Context, varStore *state.VariableStore, mo
d := decoder.NewDecoder(&fdecoder.PathReader{
StateReader: varStore,
ModuleReader: moduleFeature,
UseAnySchema: true,
})
d.SetContext(idecoder.DecoderContext(ctx))

Expand Down

0 comments on commit 444ef4b

Please sign in to comment.