From 444ef4be046f5fa62ed10dc8b574f6df510f27d1 Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Tue, 7 May 2024 10:21:14 +0200 Subject: [PATCH] Use any schema when collecting variables --- .../features/variables/decoder/path_reader.go | 3 ++- .../variables/decoder/variable_context.go | 19 +++++++++++++------ .../features/variables/jobs/references.go | 1 + 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/internal/features/variables/decoder/path_reader.go b/internal/features/variables/decoder/path_reader.go index 4e661afff..89ddca140 100644 --- a/internal/features/variables/decoder/path_reader.go +++ b/internal/features/variables/decoder/path_reader.go @@ -25,6 +25,7 @@ type ModuleReader interface { type PathReader struct { StateReader StateReader ModuleReader ModuleReader + UseAnySchema bool } var _ decoder.PathReader = &PathReader{} @@ -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) } diff --git a/internal/features/variables/decoder/variable_context.go b/internal/features/variables/decoder/variable_context.go index 7e26f699f..f101e867e 100644 --- a/internal/features/variables/decoder/variable_context.go +++ b/internal/features/variables/decoder/variable_context.go @@ -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. diff --git a/internal/features/variables/jobs/references.go b/internal/features/variables/jobs/references.go index 08440e0ed..d25703511 100644 --- a/internal/features/variables/jobs/references.go +++ b/internal/features/variables/jobs/references.go @@ -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))