Skip to content

Commit

Permalink
Add nil args to env list as empty strings when calling buildMetaArgs
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 Oct 3, 2024
1 parent a38e4cc commit 1db7b44
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
15 changes: 11 additions & 4 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
// set base state for every image
for i, st := range stages {
nameMatch, err := shlex.ProcessWordWithMatches(st.BaseName, globalArgs)
reportUnusedFromArgs(globalArgs, nameMatch.Unmatched, st.Location, lint)
reportUnusedFromArgs(outline.allArgs, nameMatch.Unmatched, st.Location, lint)
used := nameMatch.Matched
if used == nil {
used = map[string]struct{}{}
Expand All @@ -311,7 +311,7 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS

if v := st.Platform; v != "" {
platMatch, err := shlex.ProcessWordWithMatches(v, globalArgs)
reportUnusedFromArgs(globalArgs, platMatch.Unmatched, st.Location, lint)
reportUnusedFromArgs(outline.allArgs, platMatch.Unmatched, st.Location, lint)
reportRedundantTargetPlatform(st.Platform, platMatch, st.Location, globalArgs, lint)
reportConstPlatformDisallowed(st.Name, platMatch, st.Location, lint)

Expand Down Expand Up @@ -2332,9 +2332,16 @@ func toPBLocation(sourceIndex int, location []parser.Range) pb.Location {
}
}

func reportUnusedFromArgs(args shell.EnvGetter, unmatched map[string]struct{}, location []parser.Range, lint *linter.Linter) {
func reportUnusedFromArgs(args map[string]argInfo, unmatched map[string]struct{}, location []parser.Range, lint *linter.Linter) {
argKeys := make([]string, 0, len(args))
for k := range args {
argKeys = append(argKeys, k)
}
for arg := range unmatched {
suggest, _ := suggest.Search(arg, args.Keys(), true)
if _, ok := args[arg]; ok {
continue
}
suggest, _ := suggest.Search(arg, argKeys, true)
msg := linter.RuleUndefinedArgInFrom.Format(arg, suggest)
lint.Run(&linter.RuleUndefinedArgInFrom, location, msg)
}
Expand Down
37 changes: 33 additions & 4 deletions frontend/dockerfile/dockerfile_lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,29 @@ COPY Dockerfile .
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})

dockerfile = []byte(`
FROM --platform=$BULIDPLATFORM scratch
ARG DEBUG
FROM scratch${DEBUG}
COPY Dockerfile .
`)
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})

dockerfile = []byte(`
ARG DEBUG
FROM scra${DEBUG:-tch}
COPY Dockerfile .
`)
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})

dockerfile = []byte(`
ARG DEBUG=""
FROM scratch${DEBUG:-@bogus}
COPY Dockerfile .
`)
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})


dockerfile = []byte(`
FROM --platform=$PLATFORM scratch
COPY Dockerfile .
`)
checkLinterWarnings(t, sb, &lintTestParams{
Expand All @@ -816,13 +838,13 @@ COPY Dockerfile .
RuleName: "UndefinedArgInFrom",
Description: "FROM command must use declared ARGs",
URL: "https://docs.docker.com/go/dockerfile/rule/undefined-arg-in-from/",
Detail: "FROM argument 'BULIDPLATFORM' is not declared (did you mean BUILDPLATFORM?)",
Detail: "FROM argument 'PLATFORM' is not declared",
Level: 1,
Line: 2,
},
},
StreamBuildErr: "failed to solve: empty platform value from expression $BULIDPLATFORM (did you mean BUILDPLATFORM?)",
UnmarshalBuildErr: "empty platform value from expression $BULIDPLATFORM (did you mean BUILDPLATFORM?)",
StreamBuildErr: "failed to solve: empty platform value from expression $PLATFORM",
UnmarshalBuildErr: "empty platform value from expression $PLATFORM",
BuildErrLocation: 2,
})

Expand Down Expand Up @@ -1471,6 +1493,13 @@ func checkProgressStream(t *testing.T, sb integration.Sandbox, lintTest *lintTes
t.Fatalf("timed out waiting for statusDone")
}

if len(lintTest.Warnings) != len(warnings) {
t.Logf("expected %d warnings, received:", len(lintTest.Warnings))
for i, w := range warnings {
t.Logf("\t%d: %s", i, w.Short)
}

}
require.Equal(t, len(lintTest.Warnings), len(warnings))
sort.Slice(warnings, func(i, j int) bool {
w1 := warnings[i]
Expand Down
2 changes: 1 addition & 1 deletion frontend/dockerfile/shell/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func (sw *shellWord) processDollar() (string, error) {

switch ch {
case '-':
if !set || (nullIsUnset && value == "") {
if !set {
return word, nil
}
return value, nil
Expand Down

0 comments on commit 1db7b44

Please sign in to comment.