Skip to content

Commit e5274f9

Browse files
author
Pam Selle
authored
Merge pull request #27835 from hashicorp/pselle/warn-interpolation
Remove interpolation-only warning
2 parents 6871d0c + fa7c3d7 commit e5274f9

File tree

11 files changed

+13
-317
lines changed

11 files changed

+13
-317
lines changed

command/testdata/validate-invalid/incorrectmodulename/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ module "super#module" {
22
}
33

44
module "super" {
5-
source = "${var.modulename}"
5+
source = var.modulename
66
}

command/testdata/validate-invalid/incorrectmodulename/output.json

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"valid": false,
33
"error_count": 6,
4-
"warning_count": 1,
4+
"warning_count": 0,
55
"diagnostics": [
66
{
77
"severity": "error",
@@ -40,9 +40,9 @@
4040
}
4141
},
4242
{
43-
"severity": "warning",
44-
"summary": "Interpolation-only expressions are deprecated",
45-
"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.",
43+
"severity": "error",
44+
"summary": "Variables not allowed",
45+
"detail": "Variables may not be used here.",
4646
"range": {
4747
"filename": "testdata/validate-invalid/incorrectmodulename/main.tf",
4848
"start": {
@@ -51,27 +51,9 @@
5151
"byte": 55
5252
},
5353
"end": {
54-
"line": 5,
55-
"column": 31,
56-
"byte": 74
57-
}
58-
}
59-
},
60-
{
61-
"severity": "error",
62-
"summary": "Variables not allowed",
63-
"detail": "Variables may not be used here.",
64-
"range": {
65-
"filename": "testdata/validate-invalid/incorrectmodulename/main.tf",
66-
"start": {
6754
"line": 5,
6855
"column": 15,
6956
"byte": 58
70-
},
71-
"end": {
72-
"line": 5,
73-
"column": 18,
74-
"byte": 61
7557
}
7658
}
7759
},
@@ -88,8 +70,8 @@
8870
},
8971
"end": {
9072
"line": 5,
91-
"column": 31,
92-
"byte": 74
73+
"column": 26,
74+
"byte": 69
9375
}
9476
}
9577
},

command/testdata/validate-invalid/missing_var/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ resource "test_instance" "foo" {
33

44
network_interface {
55
device_index = 0
6-
description = "${var.description}"
6+
description = var.description
77
}
88
}

command/testdata/validate-invalid/missing_var/output.json

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,8 @@
11
{
22
"valid": false,
33
"error_count": 1,
4-
"warning_count": 1,
4+
"warning_count": 0,
55
"diagnostics": [
6-
{
7-
"severity": "warning",
8-
"summary": "Interpolation-only expressions are deprecated",
9-
"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.",
10-
"range": {
11-
"filename": "testdata/validate-invalid/missing_var/main.tf",
12-
"start": {
13-
"line": 6,
14-
"column": 21,
15-
"byte": 117
16-
},
17-
"end": {
18-
"line": 6,
19-
"column": 41,
20-
"byte": 137
21-
}
22-
}
23-
},
246
{
257
"severity": "error",
268
"summary": "Reference to undeclared input variable",
@@ -29,13 +11,13 @@
2911
"filename": "testdata/validate-invalid/missing_var/main.tf",
3012
"start": {
3113
"line": 6,
32-
"column": 24,
33-
"byte": 120
14+
"column": 21,
15+
"byte": 117
3416
},
3517
"end": {
3618
"line": 6,
37-
"column": 39,
38-
"byte": 135
19+
"column": 36,
20+
"byte": 132
3921
}
4022
}
4123
}

configs/compat_shim.go

Lines changed: 0 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -107,121 +107,3 @@ func shimIsIgnoreChangesStar(expr hcl.Expression) bool {
107107
}
108108
return val.AsString() == "*"
109109
}
110-
111-
// warnForDeprecatedInterpolations returns warning diagnostics if the given
112-
// body can be proven to contain attributes whose expressions are native
113-
// syntax expressions consisting entirely of a single template interpolation,
114-
// which is a deprecated way to include a non-literal value in configuration.
115-
//
116-
// This is a best-effort sort of thing which relies on the physical HCL native
117-
// syntax AST, so it might not catch everything. The main goal is to catch the
118-
// "obvious" cases in order to help spread awareness that this old form is
119-
// deprecated, when folks copy it from older examples they've found on the
120-
// internet that were written for Terraform 0.11 or earlier.
121-
func warnForDeprecatedInterpolationsInBody(body hcl.Body) hcl.Diagnostics {
122-
var diags hcl.Diagnostics
123-
124-
nativeBody, ok := body.(*hclsyntax.Body)
125-
if !ok {
126-
// If it's not native syntax then we've nothing to do here.
127-
return diags
128-
}
129-
130-
for _, attr := range nativeBody.Attributes {
131-
moreDiags := warnForDeprecatedInterpolationsInExpr(attr.Expr)
132-
diags = append(diags, moreDiags...)
133-
}
134-
135-
for _, block := range nativeBody.Blocks {
136-
// We'll also go hunting in nested blocks
137-
moreDiags := warnForDeprecatedInterpolationsInBody(block.Body)
138-
diags = append(diags, moreDiags...)
139-
}
140-
141-
return diags
142-
}
143-
144-
func warnForDeprecatedInterpolationsInExpr(expr hcl.Expression) hcl.Diagnostics {
145-
node, ok := expr.(hclsyntax.Node)
146-
if !ok {
147-
return nil
148-
}
149-
150-
walker := warnForDeprecatedInterpolationsWalker{
151-
// create some capacity so that we can deal with simple expressions
152-
// without any further allocation during our walk.
153-
contextStack: make([]warnForDeprecatedInterpolationsContext, 0, 16),
154-
}
155-
return hclsyntax.Walk(node, &walker)
156-
}
157-
158-
// warnForDeprecatedInterpolationsWalker is an implementation of
159-
// hclsyntax.Walker that we use to generate deprecation warnings for template
160-
// expressions that consist entirely of a single interpolation directive.
161-
// That's always redundant in Terraform v0.12 and later, but tends to show up
162-
// when people work from examples written for Terraform v0.11 or earlier.
163-
type warnForDeprecatedInterpolationsWalker struct {
164-
contextStack []warnForDeprecatedInterpolationsContext
165-
}
166-
167-
var _ hclsyntax.Walker = (*warnForDeprecatedInterpolationsWalker)(nil)
168-
169-
type warnForDeprecatedInterpolationsContext int
170-
171-
const (
172-
warnForDeprecatedInterpolationsNormal warnForDeprecatedInterpolationsContext = 0
173-
warnForDeprecatedInterpolationsObjKey warnForDeprecatedInterpolationsContext = 1
174-
)
175-
176-
func (w *warnForDeprecatedInterpolationsWalker) Enter(node hclsyntax.Node) hcl.Diagnostics {
177-
var diags hcl.Diagnostics
178-
179-
context := warnForDeprecatedInterpolationsNormal
180-
switch node := node.(type) {
181-
case *hclsyntax.ObjectConsKeyExpr:
182-
context = warnForDeprecatedInterpolationsObjKey
183-
case *hclsyntax.TemplateWrapExpr:
184-
// hclsyntax.TemplateWrapExpr is a special node type used by HCL only
185-
// for the situation where a template is just a single interpolation,
186-
// so we don't need to do anything further to distinguish that
187-
// situation. ("normal" templates are *hclsyntax.TemplateExpr.)
188-
189-
const summary = "Interpolation-only expressions are deprecated"
190-
switch w.currentContext() {
191-
case warnForDeprecatedInterpolationsObjKey:
192-
// This case requires a different resolution in order to retain
193-
// the same meaning, so we have a different detail message for
194-
// it.
195-
diags = append(diags, &hcl.Diagnostic{
196-
Severity: hcl.DiagWarning,
197-
Summary: summary,
198-
Detail: "Terraform 0.11 and earlier required all non-constant expressions to be provided via interpolation syntax, but this pattern is now deprecated.\n\nTo silence this warning, replace the \"${ opening sequence and the }\" closing sequence with opening and closing parentheses respectively. Parentheses are needed here to mark this as an expression to be evaluated, rather than as a literal string key.\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.",
199-
Subject: node.Range().Ptr(),
200-
})
201-
default:
202-
diags = append(diags, &hcl.Diagnostic{
203-
Severity: hcl.DiagWarning,
204-
Summary: summary,
205-
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.",
206-
Subject: node.Range().Ptr(),
207-
})
208-
}
209-
}
210-
211-
// Note the context of the current node for when we potentially visit
212-
// child nodes.
213-
w.contextStack = append(w.contextStack, context)
214-
return diags
215-
}
216-
217-
func (w *warnForDeprecatedInterpolationsWalker) Exit(node hclsyntax.Node) hcl.Diagnostics {
218-
w.contextStack = w.contextStack[:len(w.contextStack)-1]
219-
return nil
220-
}
221-
222-
func (w *warnForDeprecatedInterpolationsWalker) currentContext() warnForDeprecatedInterpolationsContext {
223-
if len(w.contextStack) == 0 {
224-
return warnForDeprecatedInterpolationsNormal
225-
}
226-
return w.contextStack[len(w.contextStack)-1]
227-
}

configs/compat_shim_test.go

Lines changed: 0 additions & 61 deletions
This file was deleted.

configs/module_call.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ type ModuleCall struct {
3333
func decodeModuleBlock(block *hcl.Block, override bool) (*ModuleCall, hcl.Diagnostics) {
3434
var diags hcl.Diagnostics
3535

36-
// Produce deprecation messages for any pre-0.12-style
37-
// single-interpolation-only expressions.
38-
moreDiags := warnForDeprecatedInterpolationsInBody(block.Body)
39-
diags = append(diags, moreDiags...)
40-
4136
mc := &ModuleCall{
4237
Name: block.Labels[0],
4338
DeclRange: block.DefRange,

configs/named_values.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,6 @@ func decodeOutputBlock(block *hcl.Block, override bool) (*Output, hcl.Diagnostic
453453
schema = schemaForOverrides(schema)
454454
}
455455

456-
// Produce deprecation messages for any pre-0.12-style
457-
// single-interpolation-only expressions.
458-
moreDiags := warnForDeprecatedInterpolationsInBody(block.Body)
459-
diags = append(diags, moreDiags...)
460-
461456
content, moreDiags := block.Body.Content(schema)
462457
diags = append(diags, moreDiags...)
463458

@@ -522,11 +517,6 @@ func decodeLocalsBlock(block *hcl.Block) ([]*Local, hcl.Diagnostics) {
522517
})
523518
}
524519

525-
// Produce deprecation messages for any pre-0.12-style
526-
// single-interpolation-only expressions.
527-
moreDiags := warnForDeprecatedInterpolationsInExpr(attr.Expr)
528-
diags = append(diags, moreDiags...)
529-
530520
locals = append(locals, &Local{
531521
Name: name,
532522
Expr: attr.Expr,

configs/provider.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@ type Provider struct {
3030
func decodeProviderBlock(block *hcl.Block) (*Provider, hcl.Diagnostics) {
3131
var diags hcl.Diagnostics
3232

33-
// Produce deprecation messages for any pre-0.12-style
34-
// single-interpolation-only expressions. We do this up front here because
35-
// then we can also catch instances inside special blocks like "connection",
36-
// before PartialContent extracts them.
37-
moreDiags := warnForDeprecatedInterpolationsInBody(block.Body)
38-
diags = append(diags, moreDiags...)
39-
4033
content, config, moreDiags := block.Body.PartialContent(providerBlockSchema)
4134
diags = append(diags, moreDiags...)
4235

configs/resource.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,6 @@ func decodeResourceBlock(block *hcl.Block) (*Resource, hcl.Diagnostics) {
9292
Managed: &ManagedResource{},
9393
}
9494

95-
// Produce deprecation messages for any pre-0.12-style
96-
// single-interpolation-only expressions. We do this up front here because
97-
// then we can also catch instances inside special blocks like "connection",
98-
// before PartialContent extracts them.
99-
moreDiags := warnForDeprecatedInterpolationsInBody(block.Body)
100-
diags = append(diags, moreDiags...)
101-
10295
content, remain, moreDiags := block.Body.PartialContent(resourceBlockSchema)
10396
diags = append(diags, moreDiags...)
10497
r.Config = remain
@@ -303,11 +296,6 @@ func decodeDataBlock(block *hcl.Block) (*Resource, hcl.Diagnostics) {
303296
TypeRange: block.LabelRanges[0],
304297
}
305298

306-
// Produce deprecation messages for any pre-0.12-style
307-
// single-interpolation-only expressions.
308-
moreDiags := warnForDeprecatedInterpolationsInBody(block.Body)
309-
diags = append(diags, moreDiags...)
310-
311299
content, remain, moreDiags := block.Body.PartialContent(dataBlockSchema)
312300
diags = append(diags, moreDiags...)
313301
r.Config = remain

0 commit comments

Comments
 (0)