From 142cd448dee433374ceadb2858571077ab66ae12 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Fri, 1 May 2026 13:09:57 +0200 Subject: [PATCH] Fix panic in buildx debug when solving fails Closes #3822 Signed-off-by: Mateusz Gozdek --- build/result.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/build/result.go b/build/result.go index ce83eaffc..0f4b9da5a 100644 --- a/build/result.go +++ b/build/result.go @@ -220,16 +220,24 @@ func containerConfigFromError(solveErr *errdefs.SolveError, cfg *InvokeConfig) ( if err != nil { return nil, err } + // When a solve fails before its inputs/mounts have been resolved (for + // example, when computing the cache key for an exec op fails because an + // input source cannot be found), MountIDs/InputIDs may be empty or shorter + // than the declared mount list. Guard against that so callers (such as the + // debug adapter) get a clean error instead of an index-out-of-range panic. + ids := solveErr.MountIDs + if cfg.Initial { + ids = solveErr.InputIDs + } + if len(ids) < len(exec.Mounts) { + return nil, errors.Errorf("cannot start debug container: failing step has no mount results available (got %d of %d)", len(ids), len(exec.Mounts)) + } var mounts []gateway.Mount for i, mnt := range exec.Mounts { - rid := solveErr.MountIDs[i] - if cfg.Initial { - rid = solveErr.InputIDs[i] - } mounts = append(mounts, gateway.Mount{ Selector: mnt.Selector, Dest: mnt.Dest, - ResultID: rid, + ResultID: ids[i], Readonly: mnt.Readonly, MountType: mnt.MountType, CacheOpt: mnt.CacheOpt,