From 13ed5af5c8fc1c23d1d6c3ec9d69ee2c6f2d78b6 Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Tue, 22 Sep 2020 13:40:08 -0400 Subject: [PATCH] configs: Deprecate nested redundant interpolations Previous deprecations only included direct assignment of template-only expressions to arguments. That is, this was not deprecated: locals { foo = ["${var.foo}"] } This commit uses hclsyntax.VisitAll to detect and show deprecations for all template-only expressions, no matter how deep they are in a given expression. --- configs/compat_shim.go | 36 ++++++++++--------- .../warning-files/redundant_interp.tf | 8 +++-- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/configs/compat_shim.go b/configs/compat_shim.go index b645ac890..4c6c1b75e 100644 --- a/configs/compat_shim.go +++ b/configs/compat_shim.go @@ -142,23 +142,27 @@ func warnForDeprecatedInterpolationsInBody(body hcl.Body) hcl.Diagnostics { } func warnForDeprecatedInterpolationsInExpr(expr hcl.Expression) hcl.Diagnostics { - var diags hcl.Diagnostics - - if _, ok := expr.(*hclsyntax.TemplateWrapExpr); !ok { - // We're only interested in TemplateWrapExpr, because that's how - // the HCL native syntax parser represents the case of a template - // that consists entirely of a single interpolation expression, which - // is therefore subject to the special case of passing through the - // inner value without conversion to string. - return diags + node, ok := expr.(hclsyntax.Node) + if !ok { + return nil } - diags = append(diags, &hcl.Diagnostic{ - Severity: hcl.DiagWarning, - Summary: "Interpolation-only expressions are deprecated", - Detail: "Terraform 0.11 and earlier required all non-constant expressions to be provided via interpolation syntax, but this pattern is now deprecated. To silence this warning, remove the \"${ sequence from the start and the }\" sequence from the end of this expression, leaving just the inner expression.\n\nTemplate interpolation syntax is still used to construct strings from expressions when the template includes multiple interpolation sequences or a mixture of literal strings and interpolations. This deprecation applies only to templates that consist entirely of a single interpolation sequence.", - Subject: expr.Range().Ptr(), - }) + return hclsyntax.VisitAll(node, func(n hclsyntax.Node) hcl.Diagnostics { + e, ok := n.(*hclsyntax.TemplateWrapExpr) + if !ok { + // We're only interested in TemplateWrapExpr, because that's how + // the HCL native syntax parser represents the case of a template + // that consists entirely of a single interpolation expression, which + // is therefore subject to the special case of passing through the + // inner value without conversion to string. + return nil + } - return diags + return hcl.Diagnostics{&hcl.Diagnostic{ + Severity: hcl.DiagWarning, + Summary: "Interpolation-only expressions are deprecated", + Detail: "Terraform 0.11 and earlier required all non-constant expressions to be provided via interpolation syntax, but this pattern is now deprecated. To silence this warning, remove the \"${ sequence from the start and the }\" sequence from the end of this expression, leaving just the inner expression.\n\nTemplate interpolation syntax is still used to construct strings from expressions when the template includes multiple interpolation sequences or a mixture of literal strings and interpolations. This deprecation applies only to templates that consist entirely of a single interpolation sequence.", + Subject: e.Range().Ptr(), + }} + }) } diff --git a/configs/testdata/warning-files/redundant_interp.tf b/configs/testdata/warning-files/redundant_interp.tf index a64d9940a..d1a3522d9 100644 --- a/configs/testdata/warning-files/redundant_interp.tf +++ b/configs/testdata/warning-files/redundant_interp.tf @@ -29,9 +29,7 @@ resource "null_resource" "a" { # in the template. template = " ${var.triggers["greeting"]} " - # No warning for this one, because it's embedded inside a more complex - # expression and our check is only for direct assignment to attributes. - wrapped = ["${var.triggers["greeting"]}"] + wrapped = ["${var.triggers["greeting"]}"] # WARNING: Interpolation-only expressions are deprecated } } @@ -41,6 +39,10 @@ module "foo" { } data "null_data_source" "b" { + inputs = { + host = "${var.triggers["host"]}" # WARNING: Interpolation-only expressions are deprecated + } + has_computed_default = "${var.foo}" # WARNING: Interpolation-only expressions are deprecated }