Skip to content

Commit

Permalink
Handle multiple platforms when using the lint rule check functionalities
Browse files Browse the repository at this point in the history
Signed-off-by: Talon Bowler <talon.bowler@docker.com>
  • Loading branch information
daghack committed Oct 7, 2024
1 parent c1dacbc commit 3de6f7f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions frontend/dockerfile/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ 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.PlatformContextKey).(*ocispecs.Platform)
return dockerfile2llb.DockerfileLint(ctx, src.Data, convertOpt)
},
}); err != nil {
Expand Down
40 changes: 33 additions & 7 deletions frontend/dockerui/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
)

const (
keyRequestID = "requestid"
keyRequestID = "requestid"
PlatformContextKey = "platformCtx"
)

type RequestHandler struct {
Expand Down Expand Up @@ -59,14 +60,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, PlatformContextKey, &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
}
}
Expand Down

0 comments on commit 3de6f7f

Please sign in to comment.