diff --git a/command/validate.go b/command/validate.go index cc7b4703ca..7c9188ac74 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)