From e63a1dfb96a6c34e024b6421eda2e9986be5c019 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Wed, 5 Dec 2018 18:14:17 -0800 Subject: [PATCH] lang: EvalExpr only convert if wantType is not dynamic This actually seems to be a bug in the underlying cty Convert function since converting to cty.DynamicPseudoType should always just return the input verbatim, but it seems like it's actually converting unknown values of any type to be cty.DynamicVal, losing the type information. We should eventually fix this in cty too, but having this extra check in the Terraform layer is harmless and allows us to make progress without context-switching. --- lang/eval.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lang/eval.go b/lang/eval.go index a176ff2863..e3f470d81a 100644 --- a/lang/eval.go +++ b/lang/eval.go @@ -88,16 +88,18 @@ func (s *Scope) EvalExpr(expr hcl.Expression, wantType cty.Type) (cty.Value, tfd val, evalDiags := expr.Value(ctx) diags = diags.Append(evalDiags) - var convErr error - val, convErr = convert.Convert(val, wantType) - if convErr != nil { - val = cty.UnknownVal(wantType) - diags = diags.Append(&hcl.Diagnostic{ - Severity: hcl.DiagError, - Summary: "Incorrect value type", - Detail: fmt.Sprintf("Invalid expression value: %s.", tfdiags.FormatError(convErr)), - Subject: expr.Range().Ptr(), - }) + if wantType != cty.DynamicPseudoType { + var convErr error + val, convErr = convert.Convert(val, wantType) + if convErr != nil { + val = cty.UnknownVal(wantType) + diags = diags.Append(&hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Incorrect value type", + Detail: fmt.Sprintf("Invalid expression value: %s.", tfdiags.FormatError(convErr)), + Subject: expr.Range().Ptr(), + }) + } } return val, diags