Skip to content

Commit b8616a8

Browse files
authored
Fix dockerfile resolution (#4260)
1 parent f5c3a37 commit b8616a8

File tree

2 files changed

+75
-8
lines changed

2 files changed

+75
-8
lines changed

pkg/skaffold/docker/dependencies.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"os"
2323
"path/filepath"
2424
"sort"
25-
"strings"
2625

2726
"github.com/docker/docker/builder/dockerignore"
2827

@@ -31,14 +30,16 @@ import (
3130

3231
// NormalizeDockerfilePath returns the absolute path to the dockerfile.
3332
func NormalizeDockerfilePath(context, dockerfile string) (string, error) {
34-
if filepath.IsAbs(dockerfile) {
35-
return dockerfile, nil
36-
}
37-
38-
if !strings.HasPrefix(dockerfile, context) {
39-
dockerfile = filepath.Join(context, dockerfile)
33+
// Expected case: should be found relative to the context directory.
34+
// If it does not exist, check if it's found relative to the current directory in case it's shared.
35+
// Otherwise return the path relative to the context directory, where it should have been.
36+
rel := filepath.Join(context, dockerfile)
37+
if _, err := os.Stat(rel); os.IsNotExist(err) {
38+
if _, err := os.Stat(dockerfile); err == nil || !os.IsNotExist(err) {
39+
return filepath.Abs(dockerfile)
40+
}
4041
}
41-
return filepath.Abs(dockerfile)
42+
return filepath.Abs(rel)
4243
}
4344

4445
// GetDependencies finds the sources dependencies for the given docker artifact.

pkg/skaffold/docker/dependencies_test.go

+66
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package docker
1919
import (
2020
"context"
2121
"fmt"
22+
"os"
2223
"path/filepath"
2324
"testing"
2425

@@ -559,3 +560,68 @@ func TestGetDependencies(t *testing.T) {
559560
})
560561
}
561562
}
563+
564+
func TestNormalizeDockerfilePath(t *testing.T) {
565+
tests := []struct {
566+
description string
567+
files []string
568+
dockerfile string
569+
570+
expected string // relative path
571+
}{
572+
{
573+
description: "dockerfile found in context",
574+
files: []string{"Dockerfile", "context/Dockerfile"},
575+
dockerfile: "Dockerfile",
576+
expected: "context/Dockerfile",
577+
},
578+
{
579+
description: "path to dockerfile resolved in context first",
580+
files: []string{"context/context/Dockerfile", "context/Dockerfile"},
581+
dockerfile: "context/Dockerfile",
582+
expected: "context/context/Dockerfile",
583+
},
584+
{
585+
description: "path to dockerfile in working directory",
586+
files: []string{"context/Dockerfile"},
587+
dockerfile: "context/Dockerfile",
588+
expected: "context/Dockerfile",
589+
},
590+
{
591+
description: "workspace dockerfile when missing in context",
592+
files: []string{"Dockerfile", "context/randomfile.txt"},
593+
dockerfile: "Dockerfile",
594+
expected: "Dockerfile",
595+
},
596+
{
597+
description: "explicit dockerfile path",
598+
files: []string{"context/Dockerfile", "elsewhere/Dockerfile"},
599+
dockerfile: "elsewhere/Dockerfile",
600+
expected: "elsewhere/Dockerfile",
601+
},
602+
}
603+
for _, test := range tests {
604+
testutil.Run(t, test.description, func(t *testutil.T) {
605+
d := t.NewTempDir()
606+
t.Chdir(d.Root())
607+
608+
d.Mkdir("context")
609+
d.Touch(test.files...)
610+
611+
f, err := NormalizeDockerfilePath(d.Path("context"), test.dockerfile)
612+
t.CheckError(false, err)
613+
checkSameFile(t, d.Path(test.expected), f)
614+
})
615+
}
616+
}
617+
618+
func checkSameFile(t *testutil.T, expected, result string) {
619+
t.Helper()
620+
i1, err := os.Stat(expected)
621+
t.CheckError(false, err)
622+
i2, err := os.Stat(result)
623+
t.CheckError(false, err)
624+
if !os.SameFile(i1, i2) {
625+
t.Errorf("returned wrong file\n got: %s\nwanted: %s", result, expected)
626+
}
627+
}

0 commit comments

Comments
 (0)