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 2 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
4 changes: 4 additions & 0 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 @@ -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
36 changes: 36 additions & 0 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 @@ -110,6 +111,41 @@ 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{}
opt.Warn = func(short, url string, detail [][]byte, location []parser.Range) {
var ruleName, ruleDetail string
if strings.HasPrefix(short, "Lint Rule ") {
daghack marked this conversation as resolved.
Show resolved Hide resolved
ruleName = strings.TrimPrefix(short, "Lint Rule ")
ruleParts := strings.Split(ruleName, ":")
ruleName = strings.Trim(ruleParts[0], "'")
ruleDetail = strings.TrimSpace(ruleParts[1])
} else {
return
}
sourceIndex := results.AddSource(opt.SourceMap.Filename, "Dockerfile", opt.SourceMap.Data)
daghack marked this conversation as resolved.
Show resolved Hide resolved
sourceLocation := []lint.Range{}
for _, loc := range location {
sourceLocation = append(sourceLocation, lint.Range{
Start: lint.Position{
Line: loc.Start.Line,
Column: loc.Start.Character,
},
End: lint.Position{
Line: loc.End.Line,
Column: loc.End.Character,
},
})
}
results.AddWarning(ruleName, ruleDetail, sourceIndex, sourceLocation)
}
_, 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
Loading
Loading