Skip to content

Commit 332fbaf

Browse files
committed
fix regression in cycle detection
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 21da725 commit 332fbaf

File tree

4 files changed

+84
-6
lines changed

4 files changed

+84
-6
lines changed

loader/extends.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"github.com/compose-spec/compose-go/v2/types"
2727
)
2828

29-
func ApplyExtends(ctx context.Context, dict map[string]any, workingdir string, opts *Options, ct *cycleTracker, post ...PostProcessor) error {
29+
func ApplyExtends(ctx context.Context, dict map[string]any, workingdir string, opts *Options, tracker *cycleTracker, post ...PostProcessor) error {
3030
a, ok := dict["services"]
3131
if !ok {
3232
return nil
@@ -38,7 +38,8 @@ func ApplyExtends(ctx context.Context, dict map[string]any, workingdir string, o
3838
if !ok {
3939
continue
4040
}
41-
if err := ct.Add(ctx.Value(consts.ComposeFileKey{}).(string), name); err != nil {
41+
ct, err := tracker.Add(ctx.Value(consts.ComposeFileKey{}).(string), name)
42+
if err != nil {
4243
return err
4344
}
4445
var (

loader/extends_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright 2020 The Compose Specification Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package loader
18+
19+
import (
20+
"context"
21+
"path/filepath"
22+
"testing"
23+
24+
"github.com/compose-spec/compose-go/v2/types"
25+
"gotest.tools/v3/assert"
26+
)
27+
28+
func TestExtends(t *testing.T) {
29+
yaml := `
30+
name: test-extends
31+
services:
32+
test1:
33+
extends:
34+
file: testdata/extends/base.yaml
35+
service: base
36+
hostname: test1
37+
38+
test2:
39+
extends:
40+
file: testdata/extends/base.yaml
41+
service: base
42+
hostname: test2
43+
44+
test3:
45+
extends:
46+
file: testdata/extends/base.yaml
47+
service: another
48+
hostname: test3
49+
`
50+
abs, err := filepath.Abs(".")
51+
assert.NilError(t, err)
52+
53+
p, err := LoadWithContext(context.Background(), types.ConfigDetails{
54+
ConfigFiles: []types.ConfigFile{
55+
{
56+
Content: []byte(yaml),
57+
Filename: "(inline)",
58+
},
59+
},
60+
WorkingDir: abs,
61+
})
62+
assert.NilError(t, err)
63+
assert.DeepEqual(t, p.Services["test1"].Hostname, "test1")
64+
assert.Equal(t, p.Services["test2"].Hostname, "test2")
65+
assert.Equal(t, p.Services["test3"].Hostname, "test3")
66+
}

loader/loader.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ type cycleTracker struct {
144144
loaded []serviceRef
145145
}
146146

147-
func (ct *cycleTracker) Add(filename, service string) error {
147+
func (ct *cycleTracker) Add(filename, service string) (*cycleTracker, error) {
148148
toAdd := serviceRef{filename: filename, service: service}
149149
for _, loaded := range ct.loaded {
150150
if toAdd == loaded {
@@ -161,12 +161,16 @@ func (ct *cycleTracker) Add(filename, service string) error {
161161
errLines = append(errLines, fmt.Sprintf(" extends %s in %s", service.service, service.filename))
162162
}
163163

164-
return errors.New(strings.Join(errLines, "\n"))
164+
return nil, errors.New(strings.Join(errLines, "\n"))
165165
}
166166
}
167167

168-
ct.loaded = append(ct.loaded, toAdd)
169-
return nil
168+
var branch []serviceRef
169+
branch = append(branch, ct.loaded...)
170+
branch = append(branch, toAdd)
171+
return &cycleTracker{
172+
loaded: branch,
173+
}, nil
170174
}
171175

172176
// WithDiscardEnvFiles sets the Options to discard the `env_file` section after resolving to

loader/testdata/extends/base.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
base:
3+
image: base
4+
5+
another:
6+
extends: base
7+

0 commit comments

Comments
 (0)