Skip to content

Commit

Permalink
bake: fix skipped group when already visited by another one
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
(cherry picked from commit 0b8dde1)
  • Loading branch information
crazy-max committed Mar 23, 2022
1 parent 5fac64c commit 10fe8f3
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
9 changes: 5 additions & 4 deletions bake/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,12 @@ func (c Config) newOverrides(v []string) (map[string]map[string]Override, error)
}

func (c Config) ResolveGroup(name string) []string {
return c.group(name, map[string]struct{}{})
return dedupString(c.group(name, map[string][]string{}))
}

func (c Config) group(name string, visited map[string]struct{}) []string {
func (c Config) group(name string, visited map[string][]string) []string {
if _, ok := visited[name]; ok {
return nil
return visited[name]
}
var g *Group
for _, group := range c.Groups {
Expand All @@ -431,7 +431,7 @@ func (c Config) group(name string, visited map[string]struct{}) []string {
if g == nil {
return []string{name}
}
visited[name] = struct{}{}
visited[name] = []string{}
targets := make([]string, 0, len(g.Targets))
for _, t := range g.Targets {
tgroup := c.group(t, visited)
Expand All @@ -441,6 +441,7 @@ func (c Config) group(name string, visited map[string]struct{}) []string {
targets = append(targets, t)
}
}
visited[name] = targets
return targets
}

Expand Down
76 changes: 76 additions & 0 deletions bake/bake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1045,3 +1045,79 @@ func TestTargetName(t *testing.T) {
})
}
}

func TestNestedGroupsWithSameTarget(t *testing.T) {
ctx := context.TODO()

f := File{
Name: "docker-bake.hcl",
Data: []byte(`
group "a" {
targets = ["b", "c"]
}
group "b" {
targets = ["d"]
}
group "c" {
targets = ["b"]
}
target "d" {
context = "."
dockerfile = "./testdockerfile"
}
group "e" {
targets = ["a", "f"]
}
target "f" {
context = "./foo"
}`)}

cases := []struct {
name string
targets []string
ntargets int
}{
{
name: "a",
targets: []string{"b", "c"},
ntargets: 1,
},
{
name: "b",
targets: []string{"d"},
ntargets: 1,
},
{
name: "c",
targets: []string{"b"},
ntargets: 1,
},
{
name: "d",
targets: []string{"d"},
ntargets: 1,
},
{
name: "e",
targets: []string{"a", "f"},
ntargets: 2,
},
}
for _, tt := range cases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
m, g, err := ReadTargets(ctx, []File{f}, []string{tt.name}, nil, nil)
require.NoError(t, err)
require.Equal(t, 1, len(g))
require.Equal(t, tt.targets, g[0].Targets)
require.Equal(t, tt.ntargets, len(m))
require.Equal(t, ".", *m["d"].Context)
require.Equal(t, "./testdockerfile", *m["d"].Dockerfile)
})
}
}

0 comments on commit 10fe8f3

Please sign in to comment.