Skip to content

Commit

Permalink
dockerfile: avoid ignoring errors global arg expansion
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
  • Loading branch information
tonistiigi committed Apr 17, 2024
1 parent 71f99c5 commit f1e683b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
5 changes: 4 additions & 1 deletion frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,10 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
info := argInfo{definition: metaArg, location: cmd.Location()}
if v, ok := opt.BuildArgs[metaArg.Key]; !ok {
if metaArg.Value != nil {
*metaArg.Value, info.deps, _ = shlex.ProcessWordWithMatches(*metaArg.Value, metaArgsToMap(optMetaArgs))
*metaArg.Value, info.deps, err = shlex.ProcessWordWithMatches(*metaArg.Value, metaArgsToMap(optMetaArgs))
if err != nil {
return nil, parser.WithLocation(err, cmd.Location())
}
}
} else {
metaArg.Value = &v
Expand Down
41 changes: 41 additions & 0 deletions frontend/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ var allTests = integration.TestFuncs(
testPlatformArgsExplicit,
testExportMultiPlatform,
testQuotedMetaArgs,
testGlobalArgErrors,
testIgnoreEntrypoint,
testSymlinkedDockerfile,
testEmptyWildcard,
Expand Down Expand Up @@ -1291,6 +1292,46 @@ COPY --from=build /out .
require.Equal(t, "bar-box-foo", string(dt))
}

func testGlobalArgErrors(t *testing.T, sb integration.Sandbox) {
integration.SkipOnPlatform(t, "windows")
f := getFrontend(t, sb)

dockerfile := []byte(`
ARG FOO=${FOO:?"custom error"}
FROM busybox
`)

dir := integration.Tmpdir(
t,
fstest.CreateFile("Dockerfile", dockerfile, 0600),
)

c, err := client.New(sb.Context(), sb.Address())
require.NoError(t, err)
defer c.Close()

_, err = f.Solve(sb.Context(), c, client.SolveOpt{
LocalMounts: map[string]fsutil.FS{
dockerui.DefaultLocalNameDockerfile: dir,
dockerui.DefaultLocalNameContext: dir,
},
}, nil)
require.Error(t, err)

require.Contains(t, err.Error(), "FOO: custom error")

_, err = f.Solve(sb.Context(), c, client.SolveOpt{
FrontendAttrs: map[string]string{
"build-arg:FOO": "bar",
},
LocalMounts: map[string]fsutil.FS{
dockerui.DefaultLocalNameDockerfile: dir,
dockerui.DefaultLocalNameContext: dir,
},
}, nil)
require.NoError(t, err)
}

func testMultiArgs(t *testing.T, sb integration.Sandbox) {
integration.SkipOnPlatform(t, "windows")
f := getFrontend(t, sb)
Expand Down

0 comments on commit f1e683b

Please sign in to comment.