Merge pull request #2406 from tonistiigi/print-lint-fallback

build: add fallback image for --print=lint
This commit is contained in:
Tõnis Tiigi 2024-04-15 15:58:34 -07:00 committed by GitHub
commit b91957444b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 46 additions and 20 deletions

View File

@ -53,7 +53,10 @@ var (
const (
//nolint:gosec // G101: false-positive
printFallbackImage = "docker/dockerfile:1.5.2-labs@sha256:f2e91734a84c0922ff47aa4098ab775f1dfa932430d2888dd5cad5251fafdac4"
printFallbackImage = "docker/dockerfile:1.5@sha256:dbbd5e059e8a07ff7ea6233b213b36aa516b4c53c645f1817a4dd18b83cbea56"
// https://github.com/moby/buildkit/commit/3fd813cfa5eec6633b21da8164e93673d005df02
//nolint:gosec // G101: false-positive
printLintFallbackImage = "docker/dockerfile-upstream@sha256:2e302d1450bb1c3d9ef9b52800d2f3738cdfd21ead73a403e32a9bc275290e7c"
)
type Options struct {
@ -400,25 +403,8 @@ func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opt map[s
res, err := c.Solve(ctx, req)
if err != nil {
fallback := false
var reqErr *errdefs.UnsupportedSubrequestError
if errors.As(err, &reqErr) {
switch reqErr.Name {
case "frontend.outline", "frontend.targets":
fallback = true
default:
return nil, err
}
} else {
return nil, err
}
// buildkit v0.8 vendored in Docker 20.10 does not support typed errors
if strings.Contains(err.Error(), "unsupported request frontend.outline") || strings.Contains(err.Error(), "unsupported request frontend.targets") {
fallback = true
}
if fallback {
req.FrontendOpt["build-arg:BUILDKIT_SYNTAX"] = printFallbackImage
req, ok := fallbackPrintError(err, req)
if ok {
res2, err2 := c.Solve(ctx, req)
if err2 != nil {
return nil, err
@ -879,6 +865,46 @@ func waitContextDeps(ctx context.Context, index int, results *waitmap.Map, so *c
return nil
}
func fallbackPrintError(err error, req gateway.SolveRequest) (gateway.SolveRequest, bool) {
if _, ok := req.FrontendOpt["requestid"]; !ok {
return req, false
}
fallback := false
fallbackLint := false
var reqErr *errdefs.UnsupportedSubrequestError
if errors.As(err, &reqErr) {
switch reqErr.Name {
case "frontend.lint":
fallbackLint = true
fallthrough
case "frontend.outline", "frontend.targets":
fallback = true
default:
return req, false
}
}
// buildkit v0.8 vendored in Docker 20.10 does not support typed errors
for _, req := range []string{"frontend.outline", "frontend.targets", "frontend.lint"} {
if strings.Contains(err.Error(), "unsupported request "+req) {
fallback = true
}
if req == "frontend.lint" {
fallbackLint = true
}
}
if fallback {
req.FrontendOpt["build-arg:BUILDKIT_SYNTAX"] = printFallbackImage
if fallbackLint {
req.FrontendOpt["build-arg:BUILDKIT_SYNTAX"] = printLintFallbackImage
}
return req, true
}
return req, false
}
func noPrintFunc(opt map[string]Options) bool {
for _, v := range opt {
if v.PrintFunc != nil {