From 3c9804314df02a8ea9f696f9e146ca74688220e9 Mon Sep 17 00:00:00 2001 From: Talon Bowler Date: Mon, 30 Sep 2024 07:19:48 -0700 Subject: [PATCH] Handle multiple platforms when using the lint rule check functionalities Signed-off-by: Talon Bowler --- frontend/dockerfile/builder/build.go | 2 ++ frontend/dockerui/requests.go | 39 +++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/frontend/dockerfile/builder/build.go b/frontend/dockerfile/builder/build.go index 31ca7381c761..8ec623753f6e 100644 --- a/frontend/dockerfile/builder/build.go +++ b/frontend/dockerfile/builder/build.go @@ -94,6 +94,8 @@ func Build(ctx context.Context, c client.Client) (_ *client.Result, err error) { return dockerfile2llb.ListTargets(ctx, src.Data) }, Lint: func(ctx context.Context) (*lint.LintResults, error) { + convertOpt.TargetPlatform, _ = ctx.Value(dockerui.PlatformCtxKey{}).(*ocispecs.Platform) + return dockerfile2llb.DockerfileLint(ctx, src.Data, convertOpt) }, }); err != nil { diff --git a/frontend/dockerui/requests.go b/frontend/dockerui/requests.go index 9b84c52c07b3..8ff89ab0741a 100644 --- a/frontend/dockerui/requests.go +++ b/frontend/dockerui/requests.go @@ -17,6 +17,8 @@ const ( keyRequestID = "requestid" ) +type PlatformCtxKey struct{} + type RequestHandler struct { Outline func(context.Context) (*outline.Outline, error) ListTargets func(context.Context) (*targets.List, error) @@ -59,14 +61,39 @@ func (bc *Client) HandleSubrequest(ctx context.Context, h RequestHandler) (*clie } case lint.SubrequestLintDefinition.Name: if f := h.Lint; f != nil { - warnings, err := f(ctx) - if err != nil { - return nil, false, err + type warningKey struct { + description string + startLine int32 } - if warnings == nil { - return nil, true, nil + lintResults := lint.LintResults{} + uniqueWarnings := map[warningKey]lint.Warning{} + for _, tp := range bc.Config.TargetPlatforms { + ctx := context.WithValue(ctx, PlatformCtxKey{}, &tp) + results, err := f(ctx) + if err != nil { + return nil, false, err + } + sourceStart := len(lintResults.Sources) + lintResults.Sources = append(lintResults.Sources, results.Sources...) + for _, warning := range results.Warnings { + var startLine int32 + if len(warning.Location.Ranges) > 0 || warning.Location.Ranges[0] != nil { + startLine = warning.Location.Ranges[0].Start.Line + } + key := warningKey{warning.Description, startLine} + if _, ok := uniqueWarnings[key]; !ok { + uniqueWarnings[key] = warning + // Update the location to be relative to the combined source infos + warning.Location.SourceIndex += int32(sourceStart) + lintResults.Warnings = append(lintResults.Warnings, warning) + } + } + if results.Error != nil { + lintResults.Error = results.Error + break + } } - res, err := warnings.ToResult(nil) + res, err := lintResults.ToResult(nil) return res, true, err } }