Skip to content

Commit

Permalink
Merge pull request #274 from compose-spec/nicksieger/compose/9526
Browse files Browse the repository at this point in the history
project: ensure withServices doesn't blow stack on cycles
  • Loading branch information
nicksieger committed Jun 10, 2022
2 parents d1bc5d8 + 38b955a commit 0056c09
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 4 additions & 4 deletions types/project.go
Expand Up @@ -142,26 +142,26 @@ func (p Project) WithServices(names []string, fn ServiceFunc) error {
return p.withServices(names, fn, map[string]bool{})
}

func (p Project) withServices(names []string, fn ServiceFunc, done map[string]bool) error {
func (p Project) withServices(names []string, fn ServiceFunc, seen map[string]bool) error {
services, err := p.GetServices(names...)
if err != nil {
return err
}
for _, service := range services {
if done[service.Name] {
if seen[service.Name] {
continue
}
seen[service.Name] = true
dependencies := service.GetDependencies()
if len(dependencies) > 0 {
err := p.withServices(dependencies, fn, done)
err := p.withServices(dependencies, fn, seen)
if err != nil {
return err
}
}
if err := fn(service); err != nil {
return err
}
done[service.Name] = true
}
return nil
}
Expand Down
7 changes: 7 additions & 0 deletions types/project_test.go
Expand Up @@ -83,6 +83,13 @@ func Test_ForServices(t *testing.T) {
assert.Equal(t, p.DisabledServices[0].Name, "service_3")
}

func Test_ForServicesCycle(t *testing.T) {
p := makeProject()
p.Services[0].Links = []string{"service_2"}
err := p.ForServices([]string{"service_2"})
assert.NilError(t, err)
}

func makeProject() Project {
return Project{
Services: append(Services{},
Expand Down

0 comments on commit 0056c09

Please sign in to comment.