diff --git a/hcl2template/types.packer_config.go b/hcl2template/types.packer_config.go index a0897995f..ff889719b 100644 --- a/hcl2template/types.packer_config.go +++ b/hcl2template/types.packer_config.go @@ -33,13 +33,11 @@ type PackerConfig struct { // decoder in order to tell what is the actual value of a var or a local and // the list of defined functions. func (cfg *PackerConfig) EvalContext() *hcl.EvalContext { - v, _ := cfg.InputVariables.Values() - l, _ := cfg.LocalVariables.Values() ectx := &hcl.EvalContext{ Functions: Functions(cfg.Basedir), Variables: map[string]cty.Value{ - "var": cty.ObjectVal(v), - "local": cty.ObjectVal(l), + "var": cty.ObjectVal(cfg.InputVariables.Values()), + "local": cty.ObjectVal(cfg.LocalVariables.Values()), }, } return ectx diff --git a/hcl2template/types.variables.go b/hcl2template/types.variables.go index 1b928f07f..b15071e2a 100644 --- a/hcl2template/types.variables.go +++ b/hcl2template/types.variables.go @@ -68,9 +68,6 @@ func (v *Variable) Value() (cty.Value, *hcl.Diagnostic) { } value := cty.NullVal(cty.DynamicPseudoType) - if v.Type != cty.NilType { - cty.NullVal(v.Type) - } return value, &hcl.Diagnostic{ Severity: hcl.DiagError, @@ -83,17 +80,16 @@ func (v *Variable) Value() (cty.Value, *hcl.Diagnostic) { type Variables map[string]*Variable -func (variables Variables) Values() (map[string]cty.Value, hcl.Diagnostics) { +func (variables Variables) Values() map[string]cty.Value { res := map[string]cty.Value{} - var diags hcl.Diagnostics for k, v := range variables { - var diag *hcl.Diagnostic - res[k], diag = v.Value() - if diag != nil { - diags = append(diags, diag) - } + res[k], _ = v.Value() + // here the value might not be used and in that case we don't want to + // force users to set it. So this error is ignored. we still set it as + // it can be a `cty.NullVal(cty.DynamicPseudoType)`, which is the go + // cty value for 'unknown value' and should error when used. } - return res, diags + return res } // decodeVariable decodes a variable key and value into Variables