Skip UTF-8 BOM bytes from Dockerignore if exist
This fix tries to address issues related to #23221 where Dockerignore 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 Dockerignore if exists. Additional tests has been added to cover the changes in this fix. This fix is related to #23221 (UTF-8 BOM in Dockerfile). Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
parent
678c80f925
commit
ea86320fcc
2 changed files with 34 additions and 1 deletions
|
@ -2,6 +2,7 @@ package dockerignore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -18,10 +19,18 @@ func ReadAll(reader io.ReadCloser) ([]string, error) {
|
||||||
defer reader.Close()
|
defer reader.Close()
|
||||||
scanner := bufio.NewScanner(reader)
|
scanner := bufio.NewScanner(reader)
|
||||||
var excludes []string
|
var excludes []string
|
||||||
|
currentLine := 0
|
||||||
|
|
||||||
|
utf8bom := []byte{0xEF, 0xBB, 0xBF}
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
|
scannedBytes := scanner.Bytes()
|
||||||
|
// We trim UTF8 BOM
|
||||||
|
if currentLine == 0 {
|
||||||
|
scannedBytes = bytes.TrimPrefix(scannedBytes, utf8bom)
|
||||||
|
}
|
||||||
|
pattern := string(scannedBytes)
|
||||||
|
currentLine++
|
||||||
// Lines starting with # (comments) are ignored before processing
|
// Lines starting with # (comments) are ignored before processing
|
||||||
pattern := scanner.Text()
|
|
||||||
if strings.HasPrefix(pattern, "#") {
|
if strings.HasPrefix(pattern, "#") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
@ -6805,3 +6805,27 @@ func (s *DockerSuite) TestBuildWithUTF8BOM(c *check.C) {
|
||||||
_, err = buildImageFromContext(name, ctx, true)
|
_, err = buildImageFromContext(name, ctx, true)
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test case for UTF-8 BOM in .dockerignore, related to #23221
|
||||||
|
func (s *DockerSuite) TestBuildWithUTF8BOMDockerignore(c *check.C) {
|
||||||
|
name := "test-with-utf8-bom-dockerignore"
|
||||||
|
dockerfile := `
|
||||||
|
FROM busybox
|
||||||
|
ADD . /tmp/
|
||||||
|
RUN ls -la /tmp
|
||||||
|
RUN sh -c "! ls /tmp/Dockerfile"
|
||||||
|
RUN ls /tmp/.dockerignore`
|
||||||
|
dockerignore := []byte("./Dockerfile\n")
|
||||||
|
bomDockerignore := append([]byte{0xEF, 0xBB, 0xBF}, dockerignore...)
|
||||||
|
ctx, err := fakeContext(dockerfile, map[string]string{
|
||||||
|
"Dockerfile": dockerfile,
|
||||||
|
})
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
defer ctx.Close()
|
||||||
|
err = ctx.addFile(".dockerignore", bomDockerignore)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
_, err = buildImageFromContext(name, ctx, true)
|
||||||
|
if err != nil {
|
||||||
|
c.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue