From 84cc48afd6dec3e88b1d0464fb12d599db1aa60f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= <mdelapenya@gmail.com>
Date: Tue, 9 Apr 2024 17:56:17 +0200
Subject: [PATCH] chore: only include the dockerignore file if it exists

---
 container.go             | 18 ++++++++++++------
 container_ignore_test.go | 11 ++++++++++-
 2 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/container.go b/container.go
index a7ae03dd56..00da75783d 100644
--- a/container.go
+++ b/container.go
@@ -221,13 +221,13 @@ func (c *ContainerRequest) GetContext() (io.Reader, error) {
 	}
 	c.Context = abs
 
-	excluded, err := parseDockerIgnore(abs)
+	dockerIgnoreExists, excluded, err := parseDockerIgnore(abs)
 	if err != nil {
 		return nil, err
 	}
 
-	if len(excluded) > 0 {
-		// only add .dockerignore if it exists including files to ignore
+	if dockerIgnoreExists {
+		// only add .dockerignore if it exists
 		includes = append(includes, filepath.Join(".dockerignore"))
 	}
 
@@ -244,18 +244,24 @@ func (c *ContainerRequest) GetContext() (io.Reader, error) {
 	return buildContext, nil
 }
 
-func parseDockerIgnore(targetDir string) ([]string, error) {
+// parseDockerIgnore returns if the file exists, the excluded files and an error if any
+func parseDockerIgnore(targetDir string) (bool, []string, error) {
 	// based on https://github.com/docker/cli/blob/master/cli/command/image/build/dockerignore.go#L14
 	fileLocation := filepath.Join(targetDir, ".dockerignore")
 	var excluded []string
+	exists := false
 	if f, openErr := os.Open(fileLocation); openErr == nil {
+		defer f.Close()
+
+		exists = true
+
 		var err error
 		excluded, err = ignorefile.ReadAll(f)
 		if err != nil {
-			return excluded, fmt.Errorf("error reading .dockerignore: %w", err)
+			return true, excluded, fmt.Errorf("error reading .dockerignore: %w", err)
 		}
 	}
-	return excluded, nil
+	return exists, excluded, nil
 }
 
 // GetBuildArgs returns the env args to be used when creating from Dockerfile
diff --git a/container_ignore_test.go b/container_ignore_test.go
index 566697c980..ca89db4d89 100644
--- a/container_ignore_test.go
+++ b/container_ignore_test.go
@@ -11,23 +11,32 @@ import (
 func TestParseDockerIgnore(t *testing.T) {
 	testCases := []struct {
 		filePath         string
+		exists           bool
 		expectedErr      error
 		expectedExcluded []string
 	}{
 		{
 			filePath:         "./testdata/dockerignore",
 			expectedErr:      nil,
+			exists:           true,
 			expectedExcluded: []string{"vendor", "foo", "bar"},
 		},
 		{
 			filePath:         "./testdata",
 			expectedErr:      nil,
+			exists:           true,
 			expectedExcluded: []string{"Dockerfile", "echo.Dockerfile"},
 		},
+		{
+			filePath:         "./testdata/data",
+			expectedErr:      nil,
+			expectedExcluded: nil, // it's nil because the parseDockerIgnore function uses the zero value of a slice
+		},
 	}
 
 	for _, testCase := range testCases {
-		excluded, err := parseDockerIgnore(testCase.filePath)
+		exists, excluded, err := parseDockerIgnore(testCase.filePath)
+		assert.Equal(t, testCase.exists, exists)
 		assert.Equal(t, testCase.expectedErr, err)
 		assert.Equal(t, testCase.expectedExcluded, excluded)
 	}