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>
| ... | ... |
@@ -2,6 +2,7 @@ package dockerignore |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 | 4 |
"bufio" |
| 5 |
+ "bytes" |
|
| 5 | 6 |
"fmt" |
| 6 | 7 |
"io" |
| 7 | 8 |
"path/filepath" |
| ... | ... |
@@ -18,10 +19,18 @@ func ReadAll(reader io.ReadCloser) ([]string, error) {
|
| 18 | 18 |
defer reader.Close() |
| 19 | 19 |
scanner := bufio.NewScanner(reader) |
| 20 | 20 |
var excludes []string |
| 21 |
+ currentLine := 0 |
|
| 21 | 22 |
|
| 23 |
+ utf8bom := []byte{0xEF, 0xBB, 0xBF}
|
|
| 22 | 24 |
for scanner.Scan() {
|
| 25 |
+ scannedBytes := scanner.Bytes() |
|
| 26 |
+ // We trim UTF8 BOM |
|
| 27 |
+ if currentLine == 0 {
|
|
| 28 |
+ scannedBytes = bytes.TrimPrefix(scannedBytes, utf8bom) |
|
| 29 |
+ } |
|
| 30 |
+ pattern := string(scannedBytes) |
|
| 31 |
+ currentLine++ |
|
| 23 | 32 |
// Lines starting with # (comments) are ignored before processing |
| 24 |
- pattern := scanner.Text() |
|
| 25 | 33 |
if strings.HasPrefix(pattern, "#") {
|
| 26 | 34 |
continue |
| 27 | 35 |
} |
| ... | ... |
@@ -6805,3 +6805,27 @@ func (s *DockerSuite) TestBuildWithUTF8BOM(c *check.C) {
|
| 6805 | 6805 |
_, err = buildImageFromContext(name, ctx, true) |
| 6806 | 6806 |
c.Assert(err, check.IsNil) |
| 6807 | 6807 |
} |
| 6808 |
+ |
|
| 6809 |
+// Test case for UTF-8 BOM in .dockerignore, related to #23221 |
|
| 6810 |
+func (s *DockerSuite) TestBuildWithUTF8BOMDockerignore(c *check.C) {
|
|
| 6811 |
+ name := "test-with-utf8-bom-dockerignore" |
|
| 6812 |
+ dockerfile := ` |
|
| 6813 |
+ FROM busybox |
|
| 6814 |
+ ADD . /tmp/ |
|
| 6815 |
+ RUN ls -la /tmp |
|
| 6816 |
+ RUN sh -c "! ls /tmp/Dockerfile" |
|
| 6817 |
+ RUN ls /tmp/.dockerignore` |
|
| 6818 |
+ dockerignore := []byte("./Dockerfile\n")
|
|
| 6819 |
+ bomDockerignore := append([]byte{0xEF, 0xBB, 0xBF}, dockerignore...)
|
|
| 6820 |
+ ctx, err := fakeContext(dockerfile, map[string]string{
|
|
| 6821 |
+ "Dockerfile": dockerfile, |
|
| 6822 |
+ }) |
|
| 6823 |
+ c.Assert(err, check.IsNil) |
|
| 6824 |
+ defer ctx.Close() |
|
| 6825 |
+ err = ctx.addFile(".dockerignore", bomDockerignore)
|
|
| 6826 |
+ c.Assert(err, check.IsNil) |
|
| 6827 |
+ _, err = buildImageFromContext(name, ctx, true) |
|
| 6828 |
+ if err != nil {
|
|
| 6829 |
+ c.Fatal(err) |
|
| 6830 |
+ } |
|
| 6831 |
+} |