Skip to content

Commit

Permalink
dockerfile: add hint suggestions to UndefinedVar
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed May 14, 2024
1 parent 555ea7d commit 94ee93a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
24 changes: 15 additions & 9 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ func dispatch(d *dispatchState, cmd command, opt dispatchOpt) error {
}

newword, unmatched, err := opt.shlex.ProcessWord(word, env)
reportUnmatchedVariables(cmd, d.buildArgs, unmatched, &opt)
reportUnmatchedVariables(cmd, d.buildArgs, env, unmatched, &opt)
return newword, err
})
if err != nil {
Expand All @@ -782,7 +782,7 @@ func dispatch(d *dispatchState, cmd command, opt dispatchOpt) error {
lex := shell.NewLex('\\')
lex.SkipProcessQuotes = true
newword, unmatched, err := lex.ProcessWord(word, env)
reportUnmatchedVariables(cmd, d.buildArgs, unmatched, &opt)
reportUnmatchedVariables(cmd, d.buildArgs, env, unmatched, &opt)
return newword, err
})
if err != nil {
Expand Down Expand Up @@ -2103,19 +2103,25 @@ func validateStageNames(stages []instructions.Stage, warn linter.LintWarnFunc) {
}
}

func reportUnmatchedVariables(cmd instructions.Command, buildArgs []instructions.KeyValuePairOptional, unmatched map[string]struct{}, opt *dispatchOpt) {
func reportUnmatchedVariables(cmd instructions.Command, buildArgs []instructions.KeyValuePairOptional, env []string, unmatched map[string]struct{}, opt *dispatchOpt) {
for _, buildArg := range buildArgs {
delete(unmatched, buildArg.Key)
}
if len(unmatched) == 0 {
return
}
for _, buildArg := range buildArgs {
delete(unmatched, buildArg.Key)
options := metaArgsKeys(opt.metaArgs)
for _, envVar := range env {
key, _ := parseKeyValue(envVar)
options = append(options, key)
}
for cmdVar := range unmatched {
_, nonEnvOk := nonEnvArgs[cmdVar]
if !nonEnvOk {
msg := linter.RuleUndefinedVar.Format(cmdVar)
linter.RuleUndefinedVar.Run(opt.lintWarn, cmd.Location(), msg)
if _, nonEnvOk := nonEnvArgs[cmdVar]; nonEnvOk {
continue
}
match, _ := suggest.Search(cmdVar, options, true)
msg := linter.RuleUndefinedVar.Format(cmdVar, match)
linter.RuleUndefinedVar.Run(opt.lintWarn, cmd.Location(), msg)
}
}

Expand Down
37 changes: 37 additions & 0 deletions frontend/dockerfile/dockerfile_lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,43 @@ RUN echo $foo
},
},
})

dockerfile = []byte(`
FROM alpine
ARG DIR_BINARIES=binaries/
ARG DIR_ASSETS=assets/
ARG DIR_CONFIG=config/
COPY $DIR_ASSET .
`)
checkLinterWarnings(t, sb, &lintTestParams{
Dockerfile: dockerfile,
Warnings: []expectedLintWarning{
{
RuleName: "UndefinedVar",
Description: "Variables should be defined before their use",
Detail: "Usage of undefined variable '$DIR_ASSET' (did you mean $DIR_ASSETS?)",
Level: 1,
Line: 6,
},
},
})

dockerfile = []byte(`
FROM alpine
ENV PATH=$PAHT:/tmp/bin
`)
checkLinterWarnings(t, sb, &lintTestParams{
Dockerfile: dockerfile,
Warnings: []expectedLintWarning{
{
RuleName: "UndefinedVar",
Description: "Variables should be defined before their use",
Detail: "Usage of undefined variable '$PAHT' (did you mean $PATH?)",
Level: 1,
Line: 3,
},
},
})
}

func testMultipleInstructionsDisallowed(t *testing.T, sb integration.Sandbox) {
Expand Down
10 changes: 7 additions & 3 deletions frontend/dockerfile/linter/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,15 @@ var (
return fmt.Sprintf("Usage of undefined variable '$%s'", arg)
},
}
RuleUndefinedVar = LinterRule[func(string) string]{
RuleUndefinedVar = LinterRule[func(string, string) string]{
Name: "UndefinedVar",
Description: "Variables should be defined before their use",
Format: func(arg string) string {
return fmt.Sprintf("Usage of undefined variable '$%s'", arg)
Format: func(arg, suggest string) string {
out := fmt.Sprintf("Usage of undefined variable '$%s'", arg)
if suggest != "" {
out += fmt.Sprintf(" (did you mean $%s?)", suggest)
}
return out
},
}
RuleMultipleInstructionsDisallowed = LinterRule[func(instructionName string) string]{
Expand Down

0 comments on commit 94ee93a

Please sign in to comment.