mirror of https://github.com/docker/docs.git
Skip UTF-8 BOM bytes from Dockerfile if exists
This fix tries to address issues in #23221 where Dockerfile may consists of UTF-8 BOM. This likely happens when Notepad tries to save a file as UTF-8 in Windows. This fix skips the UTF-8 BOM bytes from the beginning of the Dockerfile if exists. Additional tests has been added to cover the changes in this fix. This fix fixes #23221. Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
parent
020a86b3d9
commit
678c80f925
|
@ -3,6 +3,7 @@ package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
@ -152,8 +153,14 @@ func Parse(rwc io.Reader) (*Node, error) {
|
||||||
root.StartLine = -1
|
root.StartLine = -1
|
||||||
scanner := bufio.NewScanner(rwc)
|
scanner := bufio.NewScanner(rwc)
|
||||||
|
|
||||||
|
utf8bom := []byte{0xEF, 0xBB, 0xBF}
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
scannedLine := strings.TrimLeftFunc(scanner.Text(), unicode.IsSpace)
|
scannedBytes := scanner.Bytes()
|
||||||
|
// We trim UTF8 BOM
|
||||||
|
if currentLine == 0 {
|
||||||
|
scannedBytes = bytes.TrimPrefix(scannedBytes, utf8bom)
|
||||||
|
}
|
||||||
|
scannedLine := strings.TrimLeftFunc(string(scannedBytes), unicode.IsSpace)
|
||||||
currentLine++
|
currentLine++
|
||||||
line, child, err := ParseLine(scannedLine)
|
line, child, err := ParseLine(scannedLine)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -6791,3 +6791,17 @@ foo2
|
||||||
c.Fatal(err)
|
c.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test case for #23221
|
||||||
|
func (s *DockerSuite) TestBuildWithUTF8BOM(c *check.C) {
|
||||||
|
name := "test-with-utf8-bom"
|
||||||
|
dockerfile := []byte(`FROM busybox`)
|
||||||
|
bomDockerfile := append([]byte{0xEF, 0xBB, 0xBF}, dockerfile...)
|
||||||
|
ctx, err := fakeContextFromNewTempDir()
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
defer ctx.Close()
|
||||||
|
err = ctx.addFile("Dockerfile", bomDockerfile)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
_, err = buildImageFromContext(name, ctx, true)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue