From 5586c0423d38014bbbc99b1579e75db7faca9153 Mon Sep 17 00:00:00 2001 From: Samsondeen Dare Date: Mon, 10 Mar 2025 08:27:46 +0000 Subject: [PATCH 1/3] backport of commit 10ddb3b3ff2d99124769f0160c9356819c10d0b4 --- internal/lang/funcs/string.go | 8 ++++++++ internal/lang/funcs/string_test.go | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/internal/lang/funcs/string.go b/internal/lang/funcs/string.go index c026bb9508ca..932c36da774f 100644 --- a/internal/lang/funcs/string.go +++ b/internal/lang/funcs/string.go @@ -380,6 +380,14 @@ func makeRenderTemplateFunc(funcsCb func() (funcs map[string]function.Function, if diags.HasErrors() { return cty.DynamicVal, diags } + if val.IsNull() { + return cty.DynamicVal, &hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Template result is null", + Detail: "The result of the template is null, which is not a valid result for a templatestring call.", + Subject: expr.Range().Ptr(), + } + } return val, nil } } diff --git a/internal/lang/funcs/string_test.go b/internal/lang/funcs/string_test.go index 19f2f7f13823..83b602aa1583 100644 --- a/internal/lang/funcs/string_test.go +++ b/internal/lang/funcs/string_test.go @@ -273,6 +273,28 @@ func TestTemplateString(t *testing.T) { want cty.Value wantErr string }{ + { // a single string interpolation that evaluates to null should fail + `template`, + map[string]cty.Value{ + "template": cty.StringVal(`${test}`), + }, + cty.ObjectVal(map[string]cty.Value{ + "test": cty.NullVal(cty.String), + }), + cty.NilVal, + `:1,1-8: Template result is null; The result of the template is null, which is not a valid result for a templatestring call.`, + }, + { // a single string interpolation that evaluates to unknown should not fail + `template`, + map[string]cty.Value{ + "template": cty.StringVal(`${test}`), + }, + cty.ObjectVal(map[string]cty.Value{ + "test": cty.UnknownVal(cty.String), + }), + cty.UnknownVal(cty.String).RefineNotNull(), + ``, + }, { `template`, map[string]cty.Value{ From 94186aedcd356a046cafc6611190c44b422e6657 Mon Sep 17 00:00:00 2001 From: Samsondeen Dare Date: Mon, 10 Mar 2025 08:32:02 +0000 Subject: [PATCH 2/3] backport of commit 031fb73f5f94054f36e6d94b68889c8ae6666b7a --- .changes/v1.12/BUG FIXES-20250310-093153.yaml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changes/v1.12/BUG FIXES-20250310-093153.yaml diff --git a/.changes/v1.12/BUG FIXES-20250310-093153.yaml b/.changes/v1.12/BUG FIXES-20250310-093153.yaml new file mode 100644 index 000000000000..8b69b58c0bcb --- /dev/null +++ b/.changes/v1.12/BUG FIXES-20250310-093153.yaml @@ -0,0 +1,5 @@ +kind: BUG FIXES +body: Return error when the template's single interpolation results in null value +time: 2025-03-10T09:31:53.479704+01:00 +custom: + Issue: "36652" From b8124cb85d87d2153392bbc841a4d0e379fafa01 Mon Sep 17 00:00:00 2001 From: Samsondeen <40821565+dsa0x@users.noreply.github.com> Date: Mon, 10 Mar 2025 11:24:25 +0000 Subject: [PATCH 3/3] backport of commit 774192b2919e55854491c5b1458de05b47f87e5b --- .changes/v1.12/BUG FIXES-20250310-093153.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changes/v1.12/BUG FIXES-20250310-093153.yaml b/.changes/v1.12/BUG FIXES-20250310-093153.yaml index 8b69b58c0bcb..de9f86c6a3ac 100644 --- a/.changes/v1.12/BUG FIXES-20250310-093153.yaml +++ b/.changes/v1.12/BUG FIXES-20250310-093153.yaml @@ -1,5 +1,5 @@ kind: BUG FIXES -body: Return error when the template's single interpolation results in null value +body: Return error when the templatestring function contains only a single interpolation that evaluates to a null value time: 2025-03-10T09:31:53.479704+01:00 custom: Issue: "36652"