Skip to content

Commit 405f75f

Browse files
authored
Merge pull request moby#4062 from thaJeztah/cleanup_dockerignore
2 parents 054daab + 7eb2cea commit 405f75f

File tree

4 files changed

+57
-53
lines changed

4 files changed

+57
-53
lines changed

frontend/dockerfile/dockerignore/dockerignore.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,31 @@ import (
66
"io"
77
"path/filepath"
88
"strings"
9-
10-
"github.com/pkg/errors"
119
)
1210

13-
// ReadAll reads a .dockerignore file and returns the list of file patterns
14-
// to ignore. Note this will trim whitespace from each line as well
15-
// as use GO's "clean" func to get the shortest/cleanest path for each.
11+
// ReadAll reads an ignore file from a reader and returns the list of file
12+
// patterns to ignore, applying the following rules:
13+
//
14+
// - An UTF8 BOM header (if present) is stripped.
15+
// - Lines starting with "#" are considered comments and are skipped.
16+
//
17+
// For remaining lines:
18+
//
19+
// - Leading and trailing whitespace is removed from each ignore pattern.
20+
// - It uses [filepath.Clean] to get the shortest/cleanest path for
21+
// ignore patterns.
22+
// - Leading forward-slashes ("/") are removed from ignore patterns,
23+
// so "/some/path" and "some/path" are considered equivalent.
1624
func ReadAll(reader io.Reader) ([]string, error) {
1725
if reader == nil {
1826
return nil, nil
1927
}
2028

21-
scanner := bufio.NewScanner(reader)
2229
var excludes []string
2330
currentLine := 0
24-
2531
utf8bom := []byte{0xEF, 0xBB, 0xBF}
32+
33+
scanner := bufio.NewScanner(reader)
2634
for scanner.Scan() {
2735
scannedBytes := scanner.Bytes()
2836
// We trim UTF8 BOM
@@ -59,7 +67,7 @@ func ReadAll(reader io.Reader) ([]string, error) {
5967
excludes = append(excludes, pattern)
6068
}
6169
if err := scanner.Err(); err != nil {
62-
return nil, errors.Wrap(err, "error reading .dockerignore")
70+
return nil, err
6371
}
6472
return excludes, nil
6573
}
Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,54 @@
11
package dockerignore
22

33
import (
4-
"os"
5-
"path/filepath"
4+
"strings"
65
"testing"
76
)
87

98
func TestReadAll(t *testing.T) {
10-
di, err := ReadAll(nil)
9+
actual, err := ReadAll(nil)
1110
if err != nil {
12-
t.Fatalf("Expected not to have error, got %v", err)
11+
t.Errorf("Expected no error, got %v", err)
1312
}
14-
15-
if diLen := len(di); diLen != 0 {
16-
t.Fatalf("Expected to have zero dockerignore entry, got %d", diLen)
13+
if entries := len(actual); entries != 0 {
14+
t.Fatalf("Expected to have zero entries, got %d", entries)
1715
}
1816

19-
diName := filepath.Join(t.TempDir(), ".dockerignore")
20-
content := "test1\n/test2\n/a/file/here\n\nlastfile\n# this is a comment\n! /inverted/abs/path\n!\n! \n"
21-
err = os.WriteFile(diName, []byte(content), 0600)
22-
if err != nil {
23-
t.Fatal(err)
24-
}
17+
const content = `test1
18+
/test2
19+
/a/file/here
2520
26-
diFd, err := os.Open(diName)
27-
if err != nil {
28-
t.Fatal(err)
21+
lastfile
22+
# this is a comment
23+
! /inverted/abs/path
24+
!
25+
! `
26+
27+
expected := []string{
28+
"test1",
29+
"test2", // according to https://docs.docker.com/engine/reference/builder/#dockerignore-file, /foo/bar should be treated as foo/bar
30+
"a/file/here", // according to https://docs.docker.com/engine/reference/builder/#dockerignore-file, /foo/bar should be treated as foo/bar
31+
"lastfile",
32+
"!inverted/abs/path",
33+
"!",
34+
"!",
2935
}
30-
defer diFd.Close()
3136

32-
di, err = ReadAll(diFd)
37+
actual, err = ReadAll(strings.NewReader(content))
3338
if err != nil {
34-
t.Fatal(err)
39+
t.Error(err)
3540
}
3641

37-
if len(di) != 7 {
38-
t.Fatalf("Expected 7 entries, got %v", len(di))
39-
}
40-
if di[0] != "test1" {
41-
t.Fatal("First element is not test1")
42-
}
43-
if di[1] != "test2" { // according to https://docs.docker.com/engine/reference/builder/#dockerignore-file, /foo/bar should be treated as foo/bar
44-
t.Fatal("Second element is not test2")
45-
}
46-
if di[2] != "a/file/here" { // according to https://docs.docker.com/engine/reference/builder/#dockerignore-file, /foo/bar should be treated as foo/bar
47-
t.Fatal("Third element is not a/file/here")
48-
}
49-
if di[3] != "lastfile" {
50-
t.Fatal("Fourth element is not lastfile")
51-
}
52-
if di[4] != "!inverted/abs/path" {
53-
t.Fatal("Fifth element is not !inverted/abs/path")
54-
}
55-
if di[5] != "!" {
56-
t.Fatalf("Sixth element is not !, but %s", di[5])
57-
}
58-
if di[6] != "!" {
59-
t.Fatalf("Seventh element is not !, but %s", di[6])
42+
if len(actual) != len(expected) {
43+
t.Errorf("Expected %d entries, got %v", len(expected), len(actual))
44+
}
45+
for i, expectedLine := range expected {
46+
if i >= len(actual) {
47+
t.Errorf(`missing line %d: expected: "%s", got none`, i+1, expectedLine)
48+
continue
49+
}
50+
if actual[i] != expectedLine {
51+
t.Errorf(`line %d: expected: "%s", got: "%s"`, i+1, expectedLine, actual[i])
52+
}
6053
}
6154
}

frontend/dockerui/config.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ type Client struct {
8080
g flightcontrol.Group[*buildContext]
8181
bopts client.BuildOpts
8282

83-
dockerignore []byte
83+
dockerignore []byte
84+
dockerignoreName string
8485
}
8586

8687
type SBOM struct {
@@ -375,6 +376,7 @@ func (bc *Client) ReadEntrypoint(ctx context.Context, lang string, opts ...llb.L
375376
})
376377
if err == nil {
377378
bc.dockerignore = dt
379+
bc.dockerignoreName = bctx.filename + ".dockerignore"
378380
}
379381

380382
return &Source{
@@ -435,13 +437,14 @@ func (bc *Client) MainContext(ctx context.Context, opts ...llb.LocalOption) (*ll
435437
dt = []byte{}
436438
}
437439
bc.dockerignore = dt
440+
bc.dockerignoreName = DefaultDockerignoreName
438441
}
439442

440443
var excludes []string
441444
if len(bc.dockerignore) != 0 {
442445
excludes, err = dockerignore.ReadAll(bytes.NewBuffer(bc.dockerignore))
443446
if err != nil {
444-
return nil, errors.Wrap(err, "failed to parse dockerignore")
447+
return nil, errors.Wrapf(err, "failed parsing %s", bc.dockerignoreName)
445448
}
446449
}
447450

frontend/dockerui/namedcontext.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func (bc *Client) namedContextRecursive(ctx context.Context, name string, nameWi
208208
if len(dt) != 0 {
209209
excludes, err = dockerignore.ReadAll(bytes.NewBuffer(dt))
210210
if err != nil {
211-
return nil, nil, err
211+
return nil, nil, errors.Wrapf(err, "failed parsing %s", DefaultDockerignoreName)
212212
}
213213
}
214214
}

0 commit comments

Comments
 (0)