Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print lint warnings #4761

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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