Skip to content

Commit 58df409

Browse files
authored
Merge pull request moby#5387 from daghack/nil-args-to-empty-strings
frontend: update lint checks to correctly utilize EnvList and EnvSetter
2 parents 9b620fa + 161fc50 commit 58df409

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

frontend/dockerfile/dockerfile2llb/convert.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
266266

267267
// Validate that base images continue to be valid even
268268
// when no build arguments are used.
269-
validateBaseImagesWithDefaultArgs(stages, shlex, argCmds, globalArgs, lint)
269+
validateBaseImagesWithDefaultArgs(stages, shlex, globalArgs, argCmds, lint)
270270

271271
// Rebuild the arguments using the provided build arguments
272272
// for the remainder of the build.
@@ -285,7 +285,8 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
285285
// set base state for every image
286286
for i, st := range stages {
287287
nameMatch, err := shlex.ProcessWordWithMatches(st.BaseName, globalArgs)
288-
reportUnusedFromArgs(globalArgs, nameMatch.Unmatched, st.Location, lint)
288+
argKeys := unusedFromArgsCheckKeys(globalArgs, outline.allArgs)
289+
reportUnusedFromArgs(argKeys, nameMatch.Unmatched, st.Location, lint)
289290
used := nameMatch.Matched
290291
if used == nil {
291292
used = map[string]struct{}{}
@@ -312,7 +313,8 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
312313

313314
if v := st.Platform; v != "" {
314315
platMatch, err := shlex.ProcessWordWithMatches(v, globalArgs)
315-
reportUnusedFromArgs(globalArgs, platMatch.Unmatched, st.Location, lint)
316+
argKeys := unusedFromArgsCheckKeys(globalArgs, outline.allArgs)
317+
reportUnusedFromArgs(argKeys, platMatch.Unmatched, st.Location, lint)
316318
reportRedundantTargetPlatform(st.Platform, platMatch, st.Location, globalArgs, lint)
317319
reportConstPlatformDisallowed(st.Name, platMatch, st.Location, lint)
318320

@@ -2353,9 +2355,27 @@ func toPBLocation(sourceIndex int, location []parser.Range) pb.Location {
23532355
}
23542356
}
23552357

2356-
func reportUnusedFromArgs(args shell.EnvGetter, unmatched map[string]struct{}, location []parser.Range, lint *linter.Linter) {
2358+
func unusedFromArgsCheckKeys(env shell.EnvGetter, args map[string]argInfo) map[string]struct{} {
2359+
matched := make(map[string]struct{})
2360+
for _, arg := range args {
2361+
matched[arg.definition.Key] = struct{}{}
2362+
}
2363+
for _, k := range env.Keys() {
2364+
matched[k] = struct{}{}
2365+
}
2366+
return matched
2367+
}
2368+
2369+
func reportUnusedFromArgs(testArgKeys map[string]struct{}, unmatched map[string]struct{}, location []parser.Range, lint *linter.Linter) {
2370+
var argKeys []string
2371+
for arg := range testArgKeys {
2372+
argKeys = append(argKeys, arg)
2373+
}
23572374
for arg := range unmatched {
2358-
suggest, _ := suggest.Search(arg, args.Keys(), true)
2375+
if _, ok := testArgKeys[arg]; ok {
2376+
continue
2377+
}
2378+
suggest, _ := suggest.Search(arg, argKeys, true)
23592379
msg := linter.RuleUndefinedArgInFrom.Format(arg, suggest)
23602380
lint.Run(&linter.RuleUndefinedArgInFrom, location, msg)
23612381
}
@@ -2483,10 +2503,10 @@ func validateNoSecretKey(instruction, key string, location []parser.Range, lint
24832503
}
24842504
}
24852505

2486-
func validateBaseImagesWithDefaultArgs(stages []instructions.Stage, shlex *shell.Lex, argsCmds []instructions.ArgCommand, args *llb.EnvList, lint *linter.Linter) {
2506+
func validateBaseImagesWithDefaultArgs(stages []instructions.Stage, shlex *shell.Lex, env *llb.EnvList, argCmds []instructions.ArgCommand, lint *linter.Linter) {
24872507
// Build the arguments as if no build options were given
24882508
// and using only defaults.
2489-
args, _, err := buildMetaArgs(args, shlex, argsCmds, nil)
2509+
args, _, err := buildMetaArgs(env, shlex, argCmds, nil)
24902510
if err != nil {
24912511
// Abandon running the linter. We'll likely fail after this point
24922512
// with the same error but we shouldn't error here inside

frontend/dockerfile/dockerfile_lint_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,27 @@ COPY Dockerfile .
885885
`)
886886
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})
887887

888+
dockerfile = []byte(`
889+
ARG DEBUG
890+
FROM scratch${DEBUG}
891+
COPY Dockerfile .
892+
`)
893+
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})
894+
895+
dockerfile = []byte(`
896+
ARG DEBUG
897+
FROM scra${DEBUG:-tch}
898+
COPY Dockerfile .
899+
`)
900+
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})
901+
902+
dockerfile = []byte(`
903+
ARG DEBUG=""
904+
FROM scratch${DEBUG-@bogus}
905+
COPY Dockerfile .
906+
`)
907+
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})
908+
888909
dockerfile = []byte(`
889910
FROM --platform=$BULIDPLATFORM scratch
890911
COPY Dockerfile .
@@ -1542,6 +1563,9 @@ func checkProgressStream(t *testing.T, sb integration.Sandbox, lintTest *lintTes
15421563
},
15431564
}, status)
15441565
if lintTest.StreamBuildErr == "" && lintTest.StreamBuildErrRegexp == nil {
1566+
if err != nil {
1567+
t.Logf("expected no error, received: %v", err)
1568+
}
15451569
require.NoError(t, err)
15461570
} else {
15471571
if lintTest.StreamBuildErr != "" {
@@ -1557,6 +1581,18 @@ func checkProgressStream(t *testing.T, sb integration.Sandbox, lintTest *lintTes
15571581
t.Fatalf("timed out waiting for statusDone")
15581582
}
15591583

1584+
if len(lintTest.Warnings) != len(warnings) {
1585+
t.Logf("expected %d warnings, received:", len(lintTest.Warnings))
1586+
t.Logf("\texpected:")
1587+
for i, w := range lintTest.Warnings {
1588+
t.Logf("\t\t%d: %s", i, w.Detail)
1589+
}
1590+
1591+
t.Logf("\treceived:")
1592+
for i, w := range warnings {
1593+
t.Logf("\t%d: %s", i, w.Short)
1594+
}
1595+
}
15601596
require.Equal(t, len(lintTest.Warnings), len(warnings))
15611597
sort.Slice(warnings, func(i, j int) bool {
15621598
w1 := warnings[i]

0 commit comments

Comments
 (0)