Skip to content

Commit

Permalink
update lint subrequest to return build error and warnings up to error…
Browse files Browse the repository at this point in the history
… rather than a failed grpc response

Signed-off-by: Talon Bowler <talon.bowler@docker.com>
  • Loading branch information
daghack committed Apr 27, 2024
1 parent 60b0747 commit d635c7e
Show file tree
Hide file tree
Showing 3 changed files with 288 additions and 131 deletions.
61 changes: 60 additions & 1 deletion frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,17 @@ func DockerfileLint(ctx context.Context, dt []byte, opt ConvertOpt) (*lint.LintR
results.AddWarning(rulename, description, url, fmtmsg, sourceIndex, location)
}
_, err := toDispatchState(ctx, dt, opt)

var errLoc *parser.ErrorLocation
if err != nil {
return nil, err
buildErr := &lint.BuildError{
Message: err.Error(),
}
if errors.As(err, &errLoc) {
ranges := mergeLocations(errLoc.Locations...)
buildErr.Location = toPBLocation(sourceIndex, ranges)
}
results.Error = buildErr
}
return results, nil
}
Expand Down Expand Up @@ -2015,3 +2024,53 @@ func validateStageNames(stages []instructions.Stage, warn linter.LintWarnFunc) {
}
}
}

func mergeLocations(locations ...[]parser.Range) []parser.Range {
allRanges := []parser.Range{}
for _, ranges := range locations {
allRanges = append(allRanges, ranges...)
}
if len(allRanges) == 0 {
return []parser.Range{}
}
if len(allRanges) == 1 {
return allRanges
}

sort.Slice(allRanges, func(i, j int) bool {
return allRanges[i].Start.Line < allRanges[j].Start.Line
})

location := []parser.Range{}
currentRange := allRanges[0]
for _, r := range allRanges[1:] {
if r.Start.Line <= currentRange.End.Line {
currentRange.End = r.End
} else {
location = append(location, currentRange)
currentRange = r
}
}
location = append(location, currentRange)
return location
}

func toPBLocation(sourceIndex int, location []parser.Range) pb.Location {
loc := make([]*pb.Range, 0, len(location))
for _, l := range location {
loc = append(loc, &pb.Range{
Start: pb.Position{
Line: int32(l.Start.Line),
Character: int32(l.Start.Character),
},
End: pb.Position{
Line: int32(l.End.Line),
Character: int32(l.End.Character),
},
})
}
return pb.Location{
SourceIndex: int32(sourceIndex),
Ranges: loc,
}
}

0 comments on commit d635c7e

Please sign in to comment.