From defcd18f44a6da04d86ea3d9b71bea8323982c6c Mon Sep 17 00:00:00 2001 From: Virgil Date: Wed, 1 Apr 2026 22:47:37 +0000 Subject: [PATCH] Fix default template fallback for missing vars --- executor.go | 6 +++++- modules_infra_test.go | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/executor.go b/executor.go index 56ab214..5150bc7 100644 --- a/executor.go +++ b/executor.go @@ -2279,7 +2279,7 @@ func (e *Executor) applyFilter(value, filter string) string { // Handle default filter if corexHasPrefix(filter, "default(") { - if value == "" || value == "{{ "+filter+" }}" { + if value == "" || isUnresolvedTemplateValue(value) { // Extract default value re := regexp.MustCompile(`default\(([^)]*)\)`) if match := re.FindStringSubmatch(filter); len(match) > 1 { @@ -2318,6 +2318,10 @@ func (e *Executor) applyFilter(value, filter string) string { return value } +func isUnresolvedTemplateValue(value string) bool { + return corexHasPrefix(value, "{{ ") && corexHasSuffix(value, " }}") +} + // handleLookup handles lookup() expressions. func (e *Executor) handleLookup(expr string) string { // Parse lookup('type', 'arg') diff --git a/modules_infra_test.go b/modules_infra_test.go index 21fd61e..9372456 100644 --- a/modules_infra_test.go +++ b/modules_infra_test.go @@ -518,6 +518,14 @@ func TestModulesInfra_TemplateString_Good_FilterInTemplate(t *testing.T) { assert.Equal(t, "hello", result) } +func TestModulesInfra_TemplateString_Good_DefaultFilterMissingVar(t *testing.T) { + e := NewExecutor("/tmp") + + result := e.templateString("{{ missing_var | default('fallback') }}", "", nil) + + assert.Equal(t, "fallback", result) +} + func TestModulesInfra_TemplateString_Good_DefaultFilterEmptyVar(t *testing.T) { e := NewExecutor("/tmp") e.vars["empty_var"] = ""