Skip to content

Commit cf5a3d4

Browse files
committed
resolveRelativePaths can run with non-canonical model
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent c69a570 commit cf5a3d4

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

loader/extends.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func getExtendsBaseFromFile(
190190
)
191191
}
192192

193-
// Attempt to make a canonical model so ResolveRelativePaths can operate on source:target short syntaxes
193+
// Attempt to make a canonical model so ResolveRelativePaths can operate on source:target short syntax
194194
source, err = transform.Canonical(source, true)
195195
if err != nil {
196196
return nil, nil, err

loader/extends_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,20 @@ services:
484484
assert.NilError(t, err)
485485
assert.Check(t, p.Services["test"].Command == nil)
486486
}
487+
488+
func TestExtendsWithInterpolation(t *testing.T) {
489+
yaml := `
490+
name: test-extends-with-interpolation
491+
services:
492+
test:
493+
extends: { file: testdata/extends/interpolated.yaml, service: foo }
494+
`
495+
p, err := Load(types.ConfigDetails{
496+
ConfigFiles: []types.ConfigFile{{
497+
Content: []byte(yaml),
498+
Filename: "-",
499+
}},
500+
})
501+
assert.NilError(t, err)
502+
assert.Check(t, p.Services["test"].Volumes[0].Source == "/dev/null")
503+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
foo:
3+
image: bash
4+
volumes:
5+
- ${SOURCE:-/dev/null}:/tmp/foo:ro

paths/resolve.go

+18-12
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,30 @@ func (r *relativePathsResolver) absPath(value any) (any, error) {
116116
}
117117
return v, nil
118118
}
119+
119120
return nil, fmt.Errorf("unexpected type %T", value)
120121
}
121122

122123
func (r *relativePathsResolver) absVolumeMount(a any) (any, error) {
123-
vol := a.(map[string]any)
124-
if vol["type"] != types.VolumeTypeBind {
124+
switch vol := a.(type) {
125+
case map[string]any:
126+
if vol["type"] != types.VolumeTypeBind {
127+
return vol, nil
128+
}
129+
src, ok := vol["source"]
130+
if !ok {
131+
return nil, errors.New(`invalid mount config for type "bind": field Source must not be empty`)
132+
}
133+
abs, err := r.maybeUnixPath(src.(string))
134+
if err != nil {
135+
return nil, err
136+
}
137+
vol["source"] = abs
125138
return vol, nil
139+
default:
140+
// not using canonical format, skip
141+
return a, nil
126142
}
127-
src, ok := vol["source"]
128-
if !ok {
129-
return nil, errors.New(`invalid mount config for type "bind": field Source must not be empty`)
130-
}
131-
abs, err := r.maybeUnixPath(src.(string))
132-
if err != nil {
133-
return nil, err
134-
}
135-
vol["source"] = abs
136-
return vol, nil
137143
}
138144

139145
func (r *relativePathsResolver) volumeDriverOpts(a any) (any, error) {

0 commit comments

Comments
 (0)