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>
(cherry picked from commit f8c6a97edce937c9b6576fb04408e61f787bea20)
This commit is contained in:
Tonis Tiigi 2024-05-02 18:08:45 -07:00 committed by Jonathan A. Sternberg
parent f6830f3b86
commit 3f1aaa68d5
No known key found for this signature in database
GPG Key ID: 6603D4B96394F6B1

View File

@ -784,11 +784,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 {
@ -803,7 +803,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
@ -818,6 +818,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()
@ -866,6 +868,7 @@ func waitContextDeps(ctx context.Context, index int, results *waitmap.Map, so *c
} }
} }
} }
}
return nil return nil
} }