Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/v1.12/BUG FIXES-20250310-093153.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: BUG FIXES
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"
8 changes: 8 additions & 0 deletions internal/lang/funcs/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
22 changes: 22 additions & 0 deletions internal/lang/funcs/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
`<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.`,
},
{ // 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{
Expand Down
Loading