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 10, 2024
1 parent a854a89 commit 3c98043
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
2 changes: 2 additions & 0 deletions frontend/dockerfile/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
39 changes: 33 additions & 6 deletions frontend/dockerui/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit 3c98043

Please sign in to comment.