Skip to content

Commit

Permalink
Merge pull request #4761 from daghack/print-lint-warnings
Browse files Browse the repository at this point in the history
Print lint warnings
  • Loading branch information
tonistiigi authored Apr 11, 2024
2 parents 647a997 + e81c6c6 commit 549891b
Show file tree
Hide file tree
Showing 7 changed files with 579 additions and 254 deletions.
8 changes: 6 additions & 2 deletions frontend/dockerfile/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/moby/buildkit/frontend/dockerui"
"github.com/moby/buildkit/frontend/gateway/client"
gwpb "github.com/moby/buildkit/frontend/gateway/pb"
"github.com/moby/buildkit/frontend/subrequests/lint"
"github.com/moby/buildkit/frontend/subrequests/outline"
"github.com/moby/buildkit/frontend/subrequests/targets"
"github.com/moby/buildkit/solver/errdefs"
Expand Down Expand Up @@ -73,8 +74,8 @@ func Build(ctx context.Context, c client.Client) (_ *client.Result, err error) {
Client: bc,
SourceMap: src.SourceMap,
MetaResolver: c,
Warn: func(msg, url string, detail [][]byte, location []parser.Range) {
src.Warn(ctx, msg, warnOpts(location, detail, url))
Warn: func(rulename, description, url, msg string, location []parser.Range) {
src.Warn(ctx, msg, warnOpts(location, [][]byte{[]byte(description)}, url))
},
}

Expand All @@ -85,6 +86,9 @@ func Build(ctx context.Context, c client.Client) (_ *client.Result, err error) {
ListTargets: func(ctx context.Context) (*targets.List, error) {
return dockerfile2llb.ListTargets(ctx, src.Data)
},
Lint: func(ctx context.Context) (*lint.LintResults, error) {
return dockerfile2llb.DockerfileLint(ctx, src.Data, convertOpt)
},
}); err != nil {
return nil, err
} else if ok {
Expand Down
20 changes: 17 additions & 3 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/moby/buildkit/frontend/dockerfile/parser"
"github.com/moby/buildkit/frontend/dockerfile/shell"
"github.com/moby/buildkit/frontend/dockerui"
"github.com/moby/buildkit/frontend/subrequests/lint"
"github.com/moby/buildkit/frontend/subrequests/outline"
"github.com/moby/buildkit/frontend/subrequests/targets"
"github.com/moby/buildkit/identity"
Expand Down Expand Up @@ -63,7 +64,7 @@ type ConvertOpt struct {
TargetPlatform *ocispecs.Platform
MetaResolver llb.ImageMetaResolver
LLBCaps *apicaps.CapSet
Warn func(short, url string, detail [][]byte, location []parser.Range)
Warn linter.LintWarnFunc
}

type SBOMTargets struct {
Expand Down Expand Up @@ -110,6 +111,19 @@ func Dockefile2Outline(ctx context.Context, dt []byte, opt ConvertOpt) (*outline
return &o, nil
}

func DockerfileLint(ctx context.Context, dt []byte, opt ConvertOpt) (*lint.LintResults, error) {
results := &lint.LintResults{}
sourceIndex := results.AddSource(opt.SourceMap)
opt.Warn = func(rulename, description, url, fmtmsg string, location []parser.Range) {
results.AddWarning(rulename, description, url, fmtmsg, sourceIndex, location)
}
_, err := toDispatchState(ctx, dt, opt)
if err != nil {
return nil, err
}
return results, nil
}

func ListTargets(ctx context.Context, dt []byte) (*targets.List, error) {
dockerfile, err := parser.Parse(bytes.NewReader(dt))
if err != nil {
Expand Down Expand Up @@ -162,7 +176,7 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
}

if opt.Warn == nil {
opt.Warn = func(string, string, [][]byte, []parser.Range) {}
opt.Warn = func(string, string, string, string, []parser.Range) {}
}

if opt.Client != nil && opt.LLBCaps == nil {
Expand Down Expand Up @@ -1946,7 +1960,7 @@ func isSelfConsistentCasing(s string) bool {
return s == strings.ToLower(s) || s == strings.ToUpper(s)
}

func validateCommandCasing(dockerfile *parser.Result, warn func(short, url string, detail [][]byte, location []parser.Range)) {
func validateCommandCasing(dockerfile *parser.Result, warn linter.LintWarnFunc) {
var lowerCount, upperCount int
for _, node := range dockerfile.AST.Children {
if isSelfConsistentCasing(node.Value) {
Expand Down
Loading

0 comments on commit 549891b

Please sign in to comment.