Skip to content

Commit 8ce8973

Browse files
committed
Fix required env vars ignored except the last one
During the variable substitution, if a missing variable was followed by an existing one, the empty "outerErr" variable overwrote the previous non-empty variable. Unlike with Docker Compose v1, the first found error did not guarantee that an actual error would be thrown. Example which would have run before the fix even though var1 is not defined: ```yaml services: bash: image: bash:5.0.18-alpine3.15 environment: var12: "_ ${var1:?Error1} _ ${var2:?Error2} _ " command: - env ``` ```bash ``` This change also means that, if multiple variables are missing in one string, the first one will be reported by Docker Compose Signed-off-by: Ákos Takács <[email protected]>
1 parent 7aed131 commit 8ce8973

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

interpolation/interpolation_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ func TestValidUnexistentInterpolation(t *testing.T) {
114114
{test: "{{{ ${FOO:?foo_} }}}", errMsg: "foo_"},
115115
{test: "{{{ ${FOO:?foo-bar-value} }}}", errMsg: "foo-bar-value"},
116116
{test: "{{{ ${FOO:?foo} ${BAR:-DEFAULT_VALUE} }}}", errMsg: "foo"},
117+
{test: "${FOO:?foo} ${BAR:?bar}", errMsg: "foo"},
117118
{test: "{{{ ${BAR} }}}", expected: "{{{ }}}"},
118119
{test: "${FOO:?baz} }}}", errMsg: "baz"},
119120
{test: "${FOO?baz} }}}", errMsg: "baz"},

template/template.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type SubstituteFunc func(string, Mapping) (string, bool, error)
6262
// It accepts additional substitute function.
6363
func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, subsFuncs ...SubstituteFunc) (string, error) {
6464
var outerErr error
65+
var returnErr error
6566

6667
result := pattern.ReplaceAllStringFunc(template, func(substring string) string {
6768
_, subsFunc := getSubstitutionFunctionForTemplate(substring)
@@ -91,6 +92,9 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su
9192

9293
if substitution == "" {
9394
outerErr = &InvalidTemplateError{Template: template}
95+
if returnErr == nil {
96+
returnErr = outerErr
97+
}
9498
return ""
9599
}
96100

@@ -101,6 +105,9 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su
101105
)
102106
value, applied, outerErr = subsFunc(substitution, mapping)
103107
if outerErr != nil {
108+
if returnErr == nil {
109+
returnErr = outerErr
110+
}
104111
return ""
105112
}
106113
if applied {
@@ -119,7 +126,7 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su
119126
return value
120127
})
121128

122-
return result, outerErr
129+
return result, returnErr
123130
}
124131

125132
func getSubstitutionFunctionForTemplate(template string) (string, SubstituteFunc) {

0 commit comments

Comments
 (0)