Skip to content

Commit 2eadae2

Browse files
committed
frontend/dockerfile: Chomp heredoc before parsing shebang
Fixes: #5383 Signed-off-by: Matt Coster <opensource@mtcoster.net>
1 parent 06fc06b commit 2eadae2

2 files changed

Lines changed: 29 additions & 15 deletions

File tree

frontend/dockerfile/dockerfile2llb/convert.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,12 @@ func dispatchRun(d *dispatchState, c *instructions.RunCommand, proxy *llb.ProxyE
12431243
}
12441244

12451245
if heredoc := parser.MustParseHeredoc(args[0]); heredoc != nil {
1246-
if d.image.OS != "windows" && strings.HasPrefix(c.Files[0].Data, "#!") {
1246+
data := c.Files[0].Data
1247+
if c.Files[0].Chomp {
1248+
data = parser.ChompHeredocContent(data)
1249+
}
1250+
1251+
if d.image.OS != "windows" && strings.HasPrefix(data, "#!") {
12471252
// This is a single heredoc with a shebang, so create a file
12481253
// and run it.
12491254
// NOTE: choosing to expand doesn't really make sense here, so
@@ -1252,10 +1257,6 @@ func dispatchRun(d *dispatchState, c *instructions.RunCommand, proxy *llb.ProxyE
12521257
destPath := "/dev/pipes/"
12531258

12541259
f := c.Files[0].Name
1255-
data := c.Files[0].Data
1256-
if c.Files[0].Chomp {
1257-
data = parser.ChompHeredocContent(data)
1258-
}
12591260
st := llb.Scratch().Dir(sourcePath).File(
12601261
llb.Mkfile(f, 0755, []byte(data)),
12611262
dockerui.WithInternalName("preparing inline document"),
@@ -1272,10 +1273,6 @@ func dispatchRun(d *dispatchState, c *instructions.RunCommand, proxy *llb.ProxyE
12721273
// the syntax can still be used for shells that don't support
12731274
// heredocs directly.
12741275
// NOTE: like above, we ignore the expand option.
1275-
data := c.Files[0].Data
1276-
if c.Files[0].Chomp {
1277-
data = parser.ChompHeredocContent(data)
1278-
}
12791276
args = []string{data}
12801277
}
12811278
customname += fmt.Sprintf(" (%s)", summarizeHeredoc(c.Files[0].Data))

frontend/dockerfile/dockerfile_heredoc_test.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,16 +295,26 @@ func testRunShebangHeredoc(t *testing.T, sb integration.Sandbox) {
295295
dockerfile := []byte(`
296296
FROM busybox AS build
297297
298+
WORKDIR /dest
299+
298300
RUN <<EOF
299301
#!/bin/awk -f
300302
BEGIN {
301-
print "hello" >> "/dest"
302-
print "world" >> "/dest"
303+
print "hello" >> "./out1"
304+
print "world" >> "./out1"
303305
}
304306
EOF
305307
308+
RUN <<-EOF
309+
#!/bin/awk -f
310+
BEGIN {
311+
print "hello" >> "./out2"
312+
print "world" >> "./out2"
313+
}
314+
EOF
315+
306316
FROM scratch
307-
COPY --from=build /dest /dest
317+
COPY --from=build /dest /
308318
`)
309319

310320
dir := integration.Tmpdir(
@@ -332,9 +342,16 @@ COPY --from=build /dest /dest
332342
}, nil)
333343
require.NoError(t, err)
334344

335-
dt, err := os.ReadFile(filepath.Join(destDir, "dest"))
336-
require.NoError(t, err)
337-
require.Equal(t, "hello\nworld\n", string(dt))
345+
contents := map[string]string{
346+
"out1": "hello\nworld\n",
347+
"out2": "hello\nworld\n",
348+
}
349+
350+
for name, content := range contents {
351+
dt, err := os.ReadFile(filepath.Join(destDir, name))
352+
require.NoError(t, err)
353+
require.Equal(t, content, string(dt))
354+
}
338355
}
339356

340357
func testRunComplexHeredoc(t *testing.T, sb integration.Sandbox) {

0 commit comments

Comments
 (0)