build: fix multiple named contexts pointing to same bake target

Contexts using target: schema are replaced by input: pointing
to previous build result before build request is sent. Currently
this replacement did not work if multiple contexts pointed to
the same target name.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi 2024-05-02 18:08:45 -07:00
parent d4f088e689
commit f8c6a97edc
No known key found for this signature in database
GPG Key ID: AFA9DE5F8AB7AF39

View File

@ -782,11 +782,11 @@ func calculateChildTargets(reqs map[string][]*reqForNode, opt map[string]Options
} }
func waitContextDeps(ctx context.Context, index int, results *waitmap.Map, so *client.SolveOpt) error { func waitContextDeps(ctx context.Context, index int, results *waitmap.Map, so *client.SolveOpt) error {
m := map[string]string{} m := map[string][]string{}
for k, v := range so.FrontendAttrs { for k, v := range so.FrontendAttrs {
if strings.HasPrefix(k, "context:") && strings.HasPrefix(v, "target:") { if strings.HasPrefix(k, "context:") && strings.HasPrefix(v, "target:") {
target := resultKey(index, strings.TrimPrefix(v, "target:")) target := resultKey(index, strings.TrimPrefix(v, "target:"))
m[target] = k m[target] = append(m[target], k)
} }
} }
if len(m) == 0 { if len(m) == 0 {
@ -801,7 +801,7 @@ func waitContextDeps(ctx context.Context, index int, results *waitmap.Map, so *c
return err return err
} }
for k, v := range m { for k, contexts := range m {
r, ok := res[k] r, ok := res[k]
if !ok { if !ok {
continue continue
@ -816,6 +816,8 @@ func waitContextDeps(ctx context.Context, index int, results *waitmap.Map, so *c
if so.FrontendInputs == nil { if so.FrontendInputs == nil {
so.FrontendInputs = map[string]llb.State{} so.FrontendInputs = map[string]llb.State{}
} }
for _, v := range contexts {
if len(rr.Refs) > 0 { if len(rr.Refs) > 0 {
for platform, r := range rr.Refs { for platform, r := range rr.Refs {
st, err := r.ToState() st, err := r.ToState()
@ -864,6 +866,7 @@ func waitContextDeps(ctx context.Context, index int, results *waitmap.Map, so *c
} }
} }
} }
}
return nil return nil
} }