diff --git a/builder/tarsum.go b/builder/tarsum.go index 48372cb01c..35054dcba1 100644 --- a/builder/tarsum.go +++ b/builder/tarsum.go @@ -63,7 +63,7 @@ func (c *tarSumContext) Stat(path string) (string, FileInfo, error) { sum := path // Use the checksum of the followed path(not the possible symlink) because // this is the file that is actually copied. - if tsInfo := c.sums.GetFile(rel); tsInfo != nil { + if tsInfo := c.sums.GetFile(filepath.ToSlash(rel)); tsInfo != nil { sum = tsInfo.Sum() } fi := &HashedFileInfo{PathFileInfo{st, fullpath, filepath.Base(cleanpath)}, sum} diff --git a/integration-cli/docker_cli_build_test.go b/integration-cli/docker_cli_build_test.go index 81f484f3b2..08423fab4b 100644 --- a/integration-cli/docker_cli_build_test.go +++ b/integration-cli/docker_cli_build_test.go @@ -590,6 +590,39 @@ ADD %s/file /` } +// Regression for https://github.com/docker/docker/pull/27805 +// Makes sure that we don't use the cache if the contents of +// a file in a subfolder of the context is modified and we re-build. +func (s *DockerSuite) TestBuildModifyFileInFolder(c *check.C) { + name := "testbuildmodifyfileinfolder" + + ctx, err := fakeContext(`FROM busybox +RUN ["mkdir", "/test"] +ADD folder/file /test/changetarget`, + map[string]string{}) + if err != nil { + c.Fatal(err) + } + defer ctx.Close() + if err := ctx.Add("folder/file", "first"); err != nil { + c.Fatal(err) + } + id1, err := buildImageFromContext(name, ctx, true) + if err != nil { + c.Fatal(err) + } + if err := ctx.Add("folder/file", "second"); err != nil { + c.Fatal(err) + } + id2, err := buildImageFromContext(name, ctx, true) + if err != nil { + c.Fatal(err) + } + if id1 == id2 { + c.Fatal("cache was used even though file contents in folder was changed") + } +} + func (s *DockerSuite) TestBuildAddSingleFileToRoot(c *check.C) { testRequires(c, DaemonIsLinux) // Linux specific test name := "testaddimg" diff --git a/integration-cli/docker_utils.go b/integration-cli/docker_utils.go index dbe12c4299..87bbc6b8b1 100644 --- a/integration-cli/docker_utils.go +++ b/integration-cli/docker_utils.go @@ -578,21 +578,21 @@ func (f *FakeContext) Add(file, content string) error { } func (f *FakeContext) addFile(file string, content []byte) error { - filepath := path.Join(f.Dir, file) - dirpath := path.Dir(filepath) + fp := filepath.Join(f.Dir, filepath.FromSlash(file)) + dirpath := filepath.Dir(fp) if dirpath != "." { if err := os.MkdirAll(dirpath, 0755); err != nil { return err } } - return ioutil.WriteFile(filepath, content, 0644) + return ioutil.WriteFile(fp, content, 0644) } // Delete a file at a path func (f *FakeContext) Delete(file string) error { - filepath := path.Join(f.Dir, file) - return os.RemoveAll(filepath) + fp := filepath.Join(f.Dir, filepath.FromSlash(file)) + return os.RemoveAll(fp) } // Close deletes the context