Skip to content

Commit

Permalink
Merge pull request docker#2406 from tonistiigi/print-lint-fallback
Browse files Browse the repository at this point in the history
build: add fallback image for --print=lint
  • Loading branch information
tonistiigi authored Apr 15, 2024
2 parents 6aed54c + 126fe65 commit b919574
Showing 1 changed file with 46 additions and 20 deletions.
66 changes: 46 additions & 20 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b919574

Please sign in to comment.