From 78a82a7623d891f96077b60be7b9dd238a104ec5 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Mon, 5 Dec 2016 18:55:07 -0800 Subject: [PATCH] Warn/deprecate continuing on empty lines in `Dockerfile` This fix is related to 29005 and 24693. Currently in `Dockerfile` empty lines will continue as long as there is line escape before. This may cause some issues. The issue in 24693 is an example. A non-empty line after an empty line might be considered to be a separate instruction by many users. However, it is actually part of the last instruction under the current `Dockerfile` parsing rule. This fix is an effort to reduce the confusion around the parsing of `Dockerfile`. Even though this fix does not change the behavior of the `Dockerfile` parsing, it tries to deprecate the empty line continuation and present a warning for the user. In this case, at least it prompt users to check for the Dockerfile and avoid the confusion if possible. Signed-off-by: Yong Tang Upstream-commit: 7815c8f8754d5473eda7cd80277a4ea3c59e3c29 Component: engine --- .../builder/dockerfile/parser/parser.go | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/components/engine/builder/dockerfile/parser/parser.go b/components/engine/builder/dockerfile/parser/parser.go index a1f7a74570..6f6df9893d 100644 --- a/components/engine/builder/dockerfile/parser/parser.go +++ b/components/engine/builder/dockerfile/parser/parser.go @@ -243,6 +243,7 @@ type Result struct { AST *Node EscapeToken rune Platform string + Warnings []string } // Parse reads lines from a Reader, parses the lines into an AST and returns @@ -252,6 +253,7 @@ func Parse(rwc io.Reader) (*Result, error) { currentLine := 0 root := &Node{StartLine: -1} scanner := bufio.NewScanner(rwc) + warnings := []string{} var err error for scanner.Scan() { @@ -272,6 +274,7 @@ func Parse(rwc io.Reader) (*Result, error) { continue } + var hasEmptyContinuationLine bool for !isEndOfLine && scanner.Scan() { bytesRead, err := processLine(d, scanner.Bytes(), false) if err != nil { @@ -279,8 +282,8 @@ func Parse(rwc io.Reader) (*Result, error) { } currentLine++ - // TODO: warn this is being deprecated/removed if isEmptyContinuationLine(bytesRead) { + hasEmptyContinuationLine = true continue } @@ -289,13 +292,27 @@ func Parse(rwc io.Reader) (*Result, error) { line += continuationLine } + if hasEmptyContinuationLine { + warning := "[WARNING]: Empty continuation line found in:\n " + line + warnings = append(warnings, warning) + } + child, err := newNodeFromLine(line, d) if err != nil { return nil, err } root.AddChild(child, startLine, currentLine) } - return &Result{AST: root, EscapeToken: d.escapeToken, Platform: d.platformToken}, nil + + if len(warnings) > 0 { + warnings = append(warnings, "[WARNING]: Empty continuation lines will become errors in a future release.") + } + return &Result{ + AST: root, + Warnings: warnings, + EscapeToken: d.escapeToken, + Platform: d.platformToken, + }, nil } func trimComments(src []byte) []byte { @@ -326,6 +343,5 @@ func processLine(d *Directive, token []byte, stripLeftWhitespace bool) ([]byte, if stripLeftWhitespace { token = trimWhitespace(token) } - err := d.possibleParserDirective(string(token)) - return trimComments(token), err + return trimComments(token), d.possibleParserDirective(string(token)) }