Skip to content

Commit c5adc27

Browse files
authored
Merge pull request moby#24725 from yongtang/24693-dockerfile-empty-line-after-escape
Fix dockerfile parser with empty line after escape
2 parents c73dd3f + 3e1b539 commit c5adc27

File tree

11 files changed

+70
-24
lines changed

11 files changed

+70
-24
lines changed

builder/dockerfile/parser/parser.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,17 @@ func Parse(rwc io.Reader, d *Directive) (*Node, error) {
176176
newline := scanner.Text()
177177
currentLine++
178178

179-
if stripComments(strings.TrimSpace(newline)) == "" {
180-
continue
179+
// If escape followed by a comment line then stop
180+
// Note here that comment line starts with `#` at
181+
// the first pos of the line
182+
if stripComments(newline) == "" {
183+
break
181184
}
182185

186+
// If escape followed by an empty line then stop
187+
if strings.TrimSpace(newline) == "" {
188+
break
189+
}
183190
line, child, err = ParseLine(line+newline, d)
184191
if err != nil {
185192
return nil, err

builder/dockerfile/parser/parser_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ func TestLineInformation(t *testing.T) {
150150
t.Fatalf("Error parsing dockerfile %s: %v", testFileLineInfo, err)
151151
}
152152

153-
if ast.StartLine != 5 || ast.EndLine != 31 {
154-
fmt.Fprintf(os.Stderr, "Wrong root line information: expected(%d-%d), actual(%d-%d)\n", 5, 31, ast.StartLine, ast.EndLine)
153+
if ast.StartLine != 5 || ast.EndLine != 27 {
154+
fmt.Fprintf(os.Stderr, "Wrong root line information: expected(%d-%d), actual(%d-%d)\n", 5, 27, ast.StartLine, ast.EndLine)
155155
t.Fatalf("Root line information doesn't match result.")
156156
}
157157
if len(ast.Children) != 3 {
@@ -161,7 +161,7 @@ func TestLineInformation(t *testing.T) {
161161
expected := [][]int{
162162
{5, 5},
163163
{11, 12},
164-
{17, 31},
164+
{17, 27},
165165
}
166166
for i, child := range ast.Children {
167167
if child.StartLine != expected[i][0] || child.EndLine != expected[i][1] {

builder/dockerfile/parser/testfile-line/Dockerfile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,11 @@ ENV GOPATH \
1616
# Install the packages we need, clean up after them and us
1717
RUN apt-get update \
1818
&& dpkg -l | awk '/^ii/ {print $2}' > /tmp/dpkg.clean \
19-
20-
2119
&& apt-get install -y --no-install-recommends git golang ca-certificates \
2220
&& apt-get clean \
2321
&& rm -rf /var/lib/apt/lists \
24-
2522
&& go get -v github.com/brimstone/consuldock \
2623
&& mv $GOPATH/bin/consuldock /usr/local/bin/consuldock \
27-
2824
&& dpkg -l | awk '/^ii/ {print $2}' > /tmp/dpkg.dirty \
2925
&& apt-get remove --purge -y $(diff /tmp/dpkg.clean /tmp/dpkg.dirty | awk '/^>/ {print $2}') \
3026
&& rm /tmp/dpkg.* \

builder/dockerfile/parser/testfiles/brimstone-consuldock/Dockerfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ RUN apt-get update \
1616
&& apt-get install -y --no-install-recommends git golang ca-certificates \
1717
&& apt-get clean \
1818
&& rm -rf /var/lib/apt/lists \
19-
2019
&& go get -v github.com/brimstone/consuldock \
2120
&& mv $GOPATH/bin/consuldock /usr/local/bin/consuldock \
22-
2321
&& dpkg -l | awk '/^ii/ {print $2}' > /tmp/dpkg.dirty \
2422
&& apt-get remove --purge -y $(diff /tmp/dpkg.clean /tmp/dpkg.dirty | awk '/^>/ {print $2}') \
2523
&& rm /tmp/dpkg.* \

builder/dockerfile/parser/testfiles/brimstone-docker-consul/Dockerfile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@ RUN apt-get update \
2323
&& apt-get install -y --no-install-recommends unzip wget \
2424
&& apt-get clean \
2525
&& rm -rf /var/lib/apt/lists \
26-
2726
&& cd /tmp \
2827
&& wget https://dl.bintray.com/mitchellh/consul/0.3.1_web_ui.zip \
2928
-O web_ui.zip \
3029
&& unzip web_ui.zip \
3130
&& mv dist /webui \
3231
&& rm web_ui.zip \
33-
3432
&& dpkg -l | awk '/^ii/ {print $2}' > /tmp/dpkg.dirty \
3533
&& apt-get remove --purge -y $(diff /tmp/dpkg.clean /tmp/dpkg.dirty | awk '/^>/ {print $2}') \
3634
&& rm /tmp/dpkg.*
@@ -42,10 +40,8 @@ RUN apt-get update \
4240
&& apt-get install -y --no-install-recommends git golang ca-certificates build-essential \
4341
&& apt-get clean \
4442
&& rm -rf /var/lib/apt/lists \
45-
4643
&& go get -v github.com/hashicorp/consul \
4744
&& mv $GOPATH/bin/consul /usr/bin/consul \
48-
4945
&& dpkg -l | awk '/^ii/ {print $2}' > /tmp/dpkg.dirty \
5046
&& apt-get remove --purge -y $(diff /tmp/dpkg.clean /tmp/dpkg.dirty | awk '/^>/ {print $2}') \
5147
&& rm /tmp/dpkg.* \

builder/dockerfile/parser/testfiles/continueIndent/Dockerfile

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,5 @@ bye\
2727
frog
2828

2929
RUN echo hello \
30-
# this is a comment
31-
32-
# this is a comment with a blank line surrounding it
33-
34-
this is some more useful stuff
30+
# this is a comment that breaks escape continuation
31+
RUN echo this is some more useful stuff

builder/dockerfile/parser/testfiles/continueIndent/result

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
(run "echo hi world goodnight")
77
(run "echo goodbyefrog")
88
(run "echo goodbyefrog")
9-
(run "echo hello this is some more useful stuff")
9+
(run "echo hello")
10+
(run "echo this is some more useful stuff")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM busybox
2+
3+
# The following will create two instructions
4+
# `Run foo`
5+
# `bar`
6+
# because empty line will break the escape.
7+
# The parser will generate the following:
8+
# (from "busybox")
9+
# (run "foo")
10+
# (bar "")
11+
# And `bar` will return an error instruction later
12+
# Note: Parse() will not immediately error out.
13+
RUN foo \
14+
15+
bar
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(from "busybox")
2+
(run "foo")
3+
(bar "")

builder/dockerfile/parser/testfiles/escapes/Dockerfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ RUN apt-get \update && \
66
ADD \conf\\" /.znc
77
88
RUN foo \
9-
109
bar \
11-
1210
baz
1311
1412
CMD [ "\/usr\\\"/bin/znc", "-f", "-r" ]

0 commit comments

Comments
 (0)