Browse Source

Warn on empty continuation lines only, not for comments

Commit 8d1ae76dcbbb73d8e20c6a14a7d3fe2410b95f55 added
deprecation warnings for empty continuation lines,
but also treated comment-only lines as empty.

This patch distinguishes empty continuation lines
from comment-only lines, and only outputs warnings
for the former.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 7 years ago
parent
commit
2fd736ac10
2 changed files with 16 additions and 1 deletions
  1. 9 1
      builder/dockerfile/parser/parser.go
  2. 7 0
      builder/dockerfile/parser/parser_test.go

+ 9 - 1
builder/dockerfile/parser/parser.go

@@ -290,6 +290,10 @@ func Parse(rwc io.Reader) (*Result, error) {
 			}
 			}
 			currentLine++
 			currentLine++
 
 
+			if isComment(scanner.Bytes()) {
+				// original line was a comment (processLine strips comments)
+				continue
+			}
 			if isEmptyContinuationLine(bytesRead) {
 			if isEmptyContinuationLine(bytesRead) {
 				hasEmptyContinuationLine = true
 				hasEmptyContinuationLine = true
 				continue
 				continue
@@ -331,8 +335,12 @@ func trimWhitespace(src []byte) []byte {
 	return bytes.TrimLeftFunc(src, unicode.IsSpace)
 	return bytes.TrimLeftFunc(src, unicode.IsSpace)
 }
 }
 
 
+func isComment(line []byte) bool {
+	return tokenComment.Match(trimWhitespace(line))
+}
+
 func isEmptyContinuationLine(line []byte) bool {
 func isEmptyContinuationLine(line []byte) bool {
-	return len(trimComments(trimWhitespace(line))) == 0
+	return len(trimWhitespace(line)) == 0
 }
 }
 
 
 var utf8bom = []byte{0xEF, 0xBB, 0xBF}
 var utf8bom = []byte{0xEF, 0xBB, 0xBF}

+ 7 - 0
builder/dockerfile/parser/parser_test.go

@@ -141,6 +141,13 @@ RUN something \
 RUN another \
 RUN another \
 
 
     thing
     thing
+RUN non-indented \
+# this is a comment
+   after-comment
+
+RUN indented \
+    # this is an indented comment
+    comment
 	`)
 	`)
 
 
 	result, err := Parse(dockerfile)
 	result, err := Parse(dockerfile)