Skip to content

Commit

Permalink
add handling of error flag in dockerfile lint directive
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 May 28, 2024
1 parent 4180deb commit 78e0a40
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
10 changes: 9 additions & 1 deletion frontend/dockerfile/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func Build(ctx context.Context, c client.Client) (_ *client.Result, err error) {
return nil, err
}

var errorFromLint bool
if _, ok := opts["cmdline"]; !ok {
if cmdline, ok := opts[keySyntaxArg]; ok {
p := strings.SplitN(strings.TrimSpace(cmdline), " ", 2)
Expand Down Expand Up @@ -77,14 +78,16 @@ func Build(ctx context.Context, c client.Client) (_ *client.Result, err error) {
if err != nil {
return nil, errors.Wrapf(err, "failed to parse lint options")
}
linter.SetLintOptions(skipSet, errorOn)
linter.SetLintOptions(skipSet)
errorFromLint = errorOn
}
}

if capsError != nil {
return nil, capsError
}

violatedRules := []string{}
convertOpt := dockerfile2llb.ConvertOpt{
Config: bc.Config,
Client: bc,
Expand All @@ -97,6 +100,7 @@ func Build(ctx context.Context, c client.Client) (_ *client.Result, err error) {
}
msg = linter.LintFormatShort(rulename, msg, startLine)
src.Warn(ctx, msg, warnOpts(location, [][]byte{[]byte(description)}, url))
violatedRules = append(violatedRules, rulename)
},
}

Expand Down Expand Up @@ -152,6 +156,10 @@ func Build(ctx context.Context, c client.Client) (_ *client.Result, err error) {
return nil, nil, nil, err
}

if errorFromLint && len(violatedRules) > 0 {
return nil, nil, nil, errors.Errorf("violated lint rules: %s", strings.Join(violatedRules, ", "))
}

def, err := st.Marshal(ctx)
if err != nil {
return nil, nil, nil, errors.Wrapf(err, "failed to marshal LLB definition")
Expand Down
9 changes: 1 addition & 8 deletions frontend/dockerfile/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ func (rule *LinterRule[F]) Run(warn LintWarnFunc, location []parser.Range, txt .
}
short := strings.Join(txt, " ")
warn(rule.Name, rule.Description, rule.URL, short, location)
if ErrorOnLintWarn {
return fmt.Errorf(short)
}
return nil
}

Expand All @@ -45,11 +42,7 @@ func LintFormatShort(rulename, msg string, startLine int) string {

type LintWarnFunc func(rulename, description, url, fmtmsg string, location []parser.Range)

func SetLintOptions(skipSet []string, errorOn bool) {
if errorOn {
ErrorOnLintWarn = true
}

func SetLintOptions(skipSet []string) {
if len(skipSet) == 1 && skipSet[0] == "all" {
for _, rule := range AllRules {
rule.SetSkip(true)
Expand Down

0 comments on commit 78e0a40

Please sign in to comment.