Skip to content

Commit db97a0c

Browse files
* revert interpolation grammar to v5.1.1 ( disable support for interpolations deeper than 2 levels)
1 parent 9693d42 commit db97a0c

8 files changed

Lines changed: 38 additions & 37 deletions

File tree

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ To see all the available options, run `tox -l`.
6767

6868
## Releasing
6969

70-
To create a new releaes go to Releases page, press 'Draft a new release', create a tag
70+
To create a new release go to Releases page, press 'Draft a new release', create a tag
7171
with a version you want to be released, fill the release notes and press 'Publish release'.
7272
Github actions will take care of publishing it to PyPi.
7373

@@ -84,3 +84,23 @@ We welcome pull requests! For your pull request to be accepted smoothly, we sugg
8484
- Create a pull request. Explain why you want to make the change and what it’s for.
8585

8686
We’ll try to answer any PR’s promptly.
87+
88+
## Limitations
89+
90+
### Error parsing string interpolations nested more than 2 times
91+
92+
- Parsing following example is expected to throw out an exception and fail:
93+
```terraform
94+
locals {
95+
foo = "foo"
96+
name = "prefix1-${"prefix2-${"${local.foo}-bar"}"}" //should interpolate into "prefix1-prefix2-foo-bar" but fails
97+
}
98+
```
99+
We recommend working around this by modifying the configuration in the following manner:
100+
```terraform
101+
locals {
102+
foo = "foo"
103+
foo_bar = "${local.foo}-bar"
104+
name = "prefix1-${"prefix2-${local.foo_bar}"}" //interpolates into "prefix1-prefix2-foo-bar"
105+
}
106+
```

hcl2/hcl2.lark

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
start : body
22
body : (new_line_or_comment? (attribute | block))* new_line_or_comment?
33
attribute : identifier EQ expression
4-
block : identifier (identifier | STRING_LIT | string_with_interpolation)* new_line_or_comment? "{" body "}"
4+
block : identifier (identifier | STRING_LIT)* new_line_or_comment? "{" body "}"
55
new_line_or_comment: ( NL_OR_COMMENT )+
66
NL_OR_COMMENT: /\n[ \t]*/ | /#.*\n/ | /\/\/.*\n/ | /\/\*(.|\n)*?(\*\/)/
77

@@ -42,7 +42,6 @@ expr_term : LPAR new_line_or_comment? expression new_line_or_comment? RPAR
4242
| float_lit
4343
| int_lit
4444
| STRING_LIT
45-
| string_with_interpolation
4645
| tuple
4746
| object
4847
| function_call
@@ -57,10 +56,11 @@ expr_term : LPAR new_line_or_comment? expression new_line_or_comment? RPAR
5756
| for_tuple_expr
5857
| for_object_expr
5958

60-
STRING_LIT : "\"" STRING_CHARS? "\""
61-
STRING_CHARS : /(?:(?!\${)([^"\\]|\\.))+/ // any character except '"'
62-
string_with_interpolation: "\"" (STRING_CHARS)* interpolation_maybe_nested (STRING_CHARS | interpolation_maybe_nested)* "\""
63-
interpolation_maybe_nested: "${" expression "}"
59+
STRING_LIT : "\"" (STRING_CHARS | INTERPOLATION)* "\""
60+
STRING_CHARS : /(?:(?!\${)([^"\\]|\\.))+/+ // any character except '"" unless inside a interpolation string
61+
NESTED_INTERPOLATION : "${" /[^}]+/ "}"
62+
INTERPOLATION : "${" (/(?:(?!\${)([^}]))+/ | NESTED_INTERPOLATION)+ "}"
63+
6464

6565
int_lit : NEGATIVE_DECIMAL? DECIMAL+ | NEGATIVE_DECIMAL+
6666
!float_lit: (NEGATIVE_DECIMAL? DECIMAL+ | NEGATIVE_DECIMAL+) "." DECIMAL+ (EXP_MARK)?

test/helpers/terraform-config-json/locals_embedded_interpolation.json

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

test/helpers/terraform-config-json/multi_level_interpolation.json

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"locals": [{"simple_interpolation": "prefix:${var.foo}-suffix", "embedded_interpolation": "(long substring without interpolation); ${module.special_constants.aws_accounts[\"aaa-${local.foo}-${local.bar}\"]}/us-west-2/key_foo", "escaped_interpolation": "prefix:$${aws:username}-suffix"}]}

test/helpers/terraform-config/multi_level_interpolation.tf

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

test/helpers/terraform-config/locals_embedded_interpolation.tf renamed to test/helpers/terraform-config/string_interpolations.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
locals {
2+
simple_interpolation = "prefix:${var.foo}-suffix"
23
embedded_interpolation = "(long substring without interpolation); ${module.special_constants.aws_accounts["aaa-${local.foo}-${local.bar}"]}/us-west-2/key_foo"
4+
escaped_interpolation = "prefix:$${aws:username}-suffix"
35
}

test/unit/test_builder.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,16 @@ def test_locals_embedded_function_tf(self):
6464
def test_locals_embedded_interpolation_tf(self):
6565
builder = hcl2.Builder()
6666

67-
embedded_interpolation = (
68-
"(long substring without interpolation); ${module.special_constants.aws_accounts"
69-
'["aaa-${local.foo}-${local.bar}"]}/us-west-2/key_foo'
70-
)
67+
attributes = {
68+
"simple_interpolation": "prefix:${var.foo}-suffix",
69+
"embedded_interpolation": "(long substring without interpolation); "
70+
'${module.special_constants.aws_accounts["aaa-${local.foo}-${local.bar}"]}/us-west-2/key_foo',
71+
"escaped_interpolation": "prefix:$${aws:username}-suffix",
72+
}
7173

72-
builder.block("locals", embedded_interpolation=embedded_interpolation)
74+
builder.block("locals", **attributes)
7375

74-
self.compare_filenames(builder, "locals_embedded_interpolation.tf")
76+
self.compare_filenames(builder, "string_interpolations.tf")
7577

7678
def test_provider_function_tf(self):
7779
builder = hcl2.Builder()

0 commit comments

Comments
 (0)