From eac87798708e714fd3a7e2ae496bddc021a32fa0 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 22 Jun 2018 12:59:50 -0700 Subject: [PATCH] command: validate must set values for root variables Since the intent of the validate command is to check config validity regardless of context (input variables, state, etc), we use unknown values of the requested type here, which will then allow us to complete type checking against the specified types of the variables without assuming any particular values. --- command/validate.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/command/validate.go b/command/validate.go index cc7b4703c..7c9188ac7 100644 --- a/command/validate.go +++ b/command/validate.go @@ -6,6 +6,8 @@ import ( "path/filepath" "strings" + "github.com/zclconf/go-cty/cty" + "github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/tfdiags" ) @@ -119,8 +121,27 @@ func (c *ValidateCommand) validate(dir string) tfdiags.Diagnostics { return diags } + // "validate" is to check if the given module is valid regardless of + // input values, current state, etc. Therefore we populate all of the + // input values with unknown values of the expected type, allowing us + // to perform a type check without assuming any particular values. + varValues := make(terraform.InputValues) + for name, variable := range cfg.Module.Variables { + ty := variable.Type + if ty == cty.NilType { + // Can't predict the type at all, so we'll just mark it as + // cty.DynamicVal (unknown value of cty.DynamicPseudoType). + ty = cty.DynamicPseudoType + } + varValues[name] = &terraform.InputValue{ + Value: cty.UnknownVal(ty), + SourceType: terraform.ValueFromCLIArg, + } + } + opts := c.contextOpts() opts.Config = cfg + opts.Variables = varValues tfCtx, ctxDiags := terraform.NewContext(opts) diags = diags.Append(ctxDiags)