Skip to content

Commit 4510bf6

Browse files
committed
dockerfile: ignore empty BUILDKIT_SYNTAX overrides
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent eaa4de0 commit 4510bf6

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

frontend/dockerfile/builder/build.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ func Build(ctx context.Context, c client.Client) (_ *client.Result, err error) {
5252
}
5353

5454
if _, ok := opts["cmdline"]; !ok {
55-
if cmdline, ok := opts[keySyntaxArg]; ok {
56-
p := strings.SplitN(strings.TrimSpace(cmdline), " ", 2)
57-
res, err := forwardGateway(ctx, c, p[0], cmdline)
58-
if err != nil && len(errdefs.Sources(err)) == 0 {
59-
return nil, errors.Wrapf(err, "failed with %s = %s", keySyntaxArg, cmdline)
55+
if v, ok := opts[keySyntaxArg]; ok {
56+
if cmdline := strings.TrimSpace(v); cmdline != "" {
57+
p := strings.SplitN(cmdline, " ", 2)
58+
res, err := forwardGateway(ctx, c, p[0], cmdline)
59+
if err != nil && len(errdefs.Sources(err)) == 0 {
60+
return nil, errors.Wrapf(err, "failed with %s = %s", keySyntaxArg, cmdline)
61+
}
62+
return res, err
6063
}
61-
return res, err
6264
} else if ref, cmdline, loc, ok := parser.DetectSyntax(src.Data); ok {
6365
res, err := forwardGateway(ctx, c, ref, cmdline)
6466
if err != nil && len(errdefs.Sources(err)) == 0 {

frontend/dockerfile/dockerfile_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ var allTests = integration.TestFuncs(
108108
testCacheMountModeNoCache,
109109
testDockerfileFromHTTP,
110110
testBuiltinArgs,
111+
testBuiltinSyntaxOverrideEmpty,
111112
testPullScratch,
112113
testSymlinkDestination,
113114
testHTTPDockerfile,
@@ -6886,6 +6887,61 @@ COPY --from=build out /
68866887
require.Equal(t, expectedStr, string(dt))
68876888
}
68886889

6890+
func testBuiltinSyntaxOverrideEmpty(t *testing.T, sb integration.Sandbox) {
6891+
f := getFrontend(t, sb)
6892+
6893+
dockerfile := []byte(integration.UnixOrWindows(
6894+
`
6895+
# syntax=docker/dockerfile-upstream:master
6896+
FROM busybox AS build
6897+
RUN echo -n ok > /out
6898+
FROM scratch
6899+
COPY --from=build /out /out
6900+
`,
6901+
`
6902+
# syntax=docker/dockerfile-upstream:master
6903+
FROM nanoserver:latest AS build
6904+
USER ContainerAdministrator
6905+
RUN echo ok>C:\out
6906+
FROM nanoserver:latest
6907+
USER ContainerAdministrator
6908+
COPY --from=build /out /out
6909+
`,
6910+
))
6911+
6912+
dir := integration.Tmpdir(
6913+
t,
6914+
fstest.CreateFile("Dockerfile", dockerfile, 0600),
6915+
)
6916+
6917+
c, err := client.New(sb.Context(), sb.Address())
6918+
require.NoError(t, err)
6919+
defer c.Close()
6920+
6921+
destDir := t.TempDir()
6922+
6923+
_, err = f.Solve(sb.Context(), c, client.SolveOpt{
6924+
FrontendAttrs: map[string]string{
6925+
"build-arg:BUILDKIT_SYNTAX": "",
6926+
},
6927+
Exports: []client.ExportEntry{
6928+
{
6929+
Type: client.ExporterLocal,
6930+
OutputDir: destDir,
6931+
},
6932+
},
6933+
LocalMounts: map[string]fsutil.FS{
6934+
dockerui.DefaultLocalNameDockerfile: dir,
6935+
dockerui.DefaultLocalNameContext: dir,
6936+
},
6937+
}, nil)
6938+
require.NoError(t, err)
6939+
6940+
dt, err := os.ReadFile(filepath.Join(destDir, "out"))
6941+
require.NoError(t, err)
6942+
require.Equal(t, "ok", strings.TrimSpace(string(dt)))
6943+
}
6944+
68896945
func testTarContext(t *testing.T, sb integration.Sandbox) {
68906946
f := getFrontend(t, sb)
68916947

frontend/dockerfile/docs/reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2723,7 +2723,7 @@ RUN echo "I'm building for $TARGETPLATFORM"
27232723
| `BUILDKIT_INLINE_CACHE`[^2] | Bool | Inline cache metadata to image config or not. |
27242724
| `BUILDKIT_MULTI_PLATFORM` | Bool | Opt into deterministic output regardless of multi-platform output or not. |
27252725
| `BUILDKIT_SANDBOX_HOSTNAME` | String | Set the hostname (default `buildkitsandbox`) |
2726-
| `BUILDKIT_SYNTAX` | String | Set frontend image |
2726+
| `BUILDKIT_SYNTAX` | String | Set frontend image. Set to an empty value to ignore the Dockerfile `# syntax=` directive and use the built-in frontend instead. |
27272727
| `SOURCE_DATE_EPOCH` | Int | Set the Unix timestamp for created image and layers. More info from [reproducible builds](https://reproducible-builds.org/docs/source-date-epoch/). Supported since Dockerfile 1.5, BuildKit 0.11 |
27282728

27292729
#### Example: keep `.git` dir

0 commit comments

Comments
 (0)