Skip to content

Commit 161fc50

Browse files
committed
Refactor various rulecheck related code to properly handle env vars.
after EnvGetter refactoring Signed-off-by: Talon Bowler <[email protected]>
1 parent a38e4cc commit 161fc50

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
@@ -265,7 +265,7 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
265265

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

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

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

@@ -2332,9 +2334,27 @@ func toPBLocation(sourceIndex int, location []parser.Range) pb.Location {
23322334
}
23332335
}
23342336

2335-
func reportUnusedFromArgs(args shell.EnvGetter, unmatched map[string]struct{}, location []parser.Range, lint *linter.Linter) {
2337+
func unusedFromArgsCheckKeys(env shell.EnvGetter, args map[string]argInfo) map[string]struct{} {
2338+
matched := make(map[string]struct{})
2339+
for _, arg := range args {
2340+
matched[arg.definition.Key] = struct{}{}
2341+
}
2342+
for _, k := range env.Keys() {
2343+
matched[k] = struct{}{}
2344+
}
2345+
return matched
2346+
}
2347+
2348+
func reportUnusedFromArgs(testArgKeys map[string]struct{}, unmatched map[string]struct{}, location []parser.Range, lint *linter.Linter) {
2349+
var argKeys []string
2350+
for arg := range testArgKeys {
2351+
argKeys = append(argKeys, arg)
2352+
}
23362353
for arg := range unmatched {
2337-
suggest, _ := suggest.Search(arg, args.Keys(), true)
2354+
if _, ok := testArgKeys[arg]; ok {
2355+
continue
2356+
}
2357+
suggest, _ := suggest.Search(arg, argKeys, true)
23382358
msg := linter.RuleUndefinedArgInFrom.Format(arg, suggest)
23392359
lint.Run(&linter.RuleUndefinedArgInFrom, location, msg)
23402360
}
@@ -2456,10 +2476,10 @@ func validateNoSecretKey(instruction, key string, location []parser.Range, lint
24562476
}
24572477
}
24582478

2459-
func validateBaseImagesWithDefaultArgs(stages []instructions.Stage, shlex *shell.Lex, argsCmds []instructions.ArgCommand, args *llb.EnvList, lint *linter.Linter) {
2479+
func validateBaseImagesWithDefaultArgs(stages []instructions.Stage, shlex *shell.Lex, env *llb.EnvList, argCmds []instructions.ArgCommand, lint *linter.Linter) {
24602480
// Build the arguments as if no build options were given
24612481
// and using only defaults.
2462-
args, _, err := buildMetaArgs(args, shlex, argsCmds, nil)
2482+
args, _, err := buildMetaArgs(env, shlex, argCmds, nil)
24632483
if err != nil {
24642484
// Abandon running the linter. We'll likely fail after this point
24652485
// 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
@@ -805,6 +805,27 @@ COPY Dockerfile .
805805
`)
806806
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})
807807

808+
dockerfile = []byte(`
809+
ARG DEBUG
810+
FROM scratch${DEBUG}
811+
COPY Dockerfile .
812+
`)
813+
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})
814+
815+
dockerfile = []byte(`
816+
ARG DEBUG
817+
FROM scra${DEBUG:-tch}
818+
COPY Dockerfile .
819+
`)
820+
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})
821+
822+
dockerfile = []byte(`
823+
ARG DEBUG=""
824+
FROM scratch${DEBUG-@bogus}
825+
COPY Dockerfile .
826+
`)
827+
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})
828+
808829
dockerfile = []byte(`
809830
FROM --platform=$BULIDPLATFORM scratch
810831
COPY Dockerfile .
@@ -1456,6 +1477,9 @@ func checkProgressStream(t *testing.T, sb integration.Sandbox, lintTest *lintTes
14561477
},
14571478
}, status)
14581479
if lintTest.StreamBuildErr == "" && lintTest.StreamBuildErrRegexp == nil {
1480+
if err != nil {
1481+
t.Logf("expected no error, received: %v", err)
1482+
}
14591483
require.NoError(t, err)
14601484
} else {
14611485
if lintTest.StreamBuildErr != "" {
@@ -1471,6 +1495,18 @@ func checkProgressStream(t *testing.T, sb integration.Sandbox, lintTest *lintTes
14711495
t.Fatalf("timed out waiting for statusDone")
14721496
}
14731497

1498+
if len(lintTest.Warnings) != len(warnings) {
1499+
t.Logf("expected %d warnings, received:", len(lintTest.Warnings))
1500+
t.Logf("\texpected:")
1501+
for i, w := range lintTest.Warnings {
1502+
t.Logf("\t\t%d: %s", i, w.Detail)
1503+
}
1504+
1505+
t.Logf("\treceived:")
1506+
for i, w := range warnings {
1507+
t.Logf("\t%d: %s", i, w.Short)
1508+
}
1509+
}
14741510
require.Equal(t, len(lintTest.Warnings), len(warnings))
14751511
sort.Slice(warnings, func(i, j int) bool {
14761512
w1 := warnings[i]

0 commit comments

Comments
 (0)