Skip to content

Commit

Permalink
dockerfile: avoid frontend panic when no stages defined
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Jul 10, 2024
1 parent e38c064 commit 262634c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
if err != nil {
return nil, err
}
if len(stages) == 0 {
return nil, errors.New("dockerfile contains no stages to build")
}
validateStageNames(stages, lint)

shlex := shell.NewLex(dockerfile.EscapeToken)
Expand Down
35 changes: 35 additions & 0 deletions frontend/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ var allTests = integration.TestFuncs(
testInvalidJSONCommands,
testHistoryError,
testHistoryFinalizeTrace,
testEmptyStages,
)

// Tests that depend on the `security.*` entitlements
Expand Down Expand Up @@ -5479,6 +5480,40 @@ RUN echo $(hostname) | grep foo
}
}

func testEmptyStages(t *testing.T, sb integration.Sandbox) {
integration.SkipOnPlatform(t, "windows")
f := getFrontend(t, sb)
dockerfile := []byte(`
ARG foo=bar
`)

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

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

destDir := t.TempDir()

_, err = f.Solve(sb.Context(), c, client.SolveOpt{
LocalMounts: map[string]fsutil.FS{
dockerui.DefaultLocalNameDockerfile: dir,
dockerui.DefaultLocalNameContext: dir,
},
Exports: []client.ExportEntry{
{
Type: client.ExporterLocal,
OutputDir: destDir,
},
},
}, nil)
require.Error(t, err)
require.Contains(t, err.Error(), "dockerfile contains no stages to build")
}

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

0 comments on commit 262634c

Please sign in to comment.