Skip to content

Commit 5de9af6

Browse files
Backport of Return error when the template's single interpolation results in null value into v1.11 (#36661)
1 parent 311ac25 commit 5de9af6

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: BUG FIXES
2+
body: Return error when the templatestring function contains only a single interpolation that evaluates to a null value
3+
time: 2025-03-10T09:31:53.479704+01:00
4+
custom:
5+
Issue: "36652"

internal/lang/funcs/string.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,14 @@ func makeRenderTemplateFunc(funcsCb func() (funcs map[string]function.Function,
380380
if diags.HasErrors() {
381381
return cty.DynamicVal, diags
382382
}
383+
if val.IsNull() {
384+
return cty.DynamicVal, &hcl.Diagnostic{
385+
Severity: hcl.DiagError,
386+
Summary: "Template result is null",
387+
Detail: "The result of the template is null, which is not a valid result for a templatestring call.",
388+
Subject: expr.Range().Ptr(),
389+
}
390+
}
383391
return val, nil
384392
}
385393
}

internal/lang/funcs/string_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,28 @@ func TestTemplateString(t *testing.T) {
273273
want cty.Value
274274
wantErr string
275275
}{
276+
{ // a single string interpolation that evaluates to null should fail
277+
`template`,
278+
map[string]cty.Value{
279+
"template": cty.StringVal(`${test}`),
280+
},
281+
cty.ObjectVal(map[string]cty.Value{
282+
"test": cty.NullVal(cty.String),
283+
}),
284+
cty.NilVal,
285+
`<templatestring argument>:1,1-8: Template result is null; The result of the template is null, which is not a valid result for a templatestring call.`,
286+
},
287+
{ // a single string interpolation that evaluates to unknown should not fail
288+
`template`,
289+
map[string]cty.Value{
290+
"template": cty.StringVal(`${test}`),
291+
},
292+
cty.ObjectVal(map[string]cty.Value{
293+
"test": cty.UnknownVal(cty.String),
294+
}),
295+
cty.UnknownVal(cty.String).RefineNotNull(),
296+
``,
297+
},
276298
{
277299
`template`,
278300
map[string]cty.Value{

0 commit comments

Comments
 (0)