diff --git a/frontend/dockerfile/dockerfile_test.go b/frontend/dockerfile/dockerfile_test.go index e12c0dd88286..5274b7a87703 100644 --- a/frontend/dockerfile/dockerfile_test.go +++ b/frontend/dockerfile/dockerfile_test.go @@ -150,6 +150,7 @@ var allTests = integration.TestFuncs( testNamedMultiplatformInputContext, testNamedFilteredContext, testEmptyDestDir, + testPreserveDestDirSlash, testCopyLinkDotDestDir, testCopyLinkEmptyDestDir, testCopyChownCreateDest, @@ -545,6 +546,42 @@ RUN cmd /V:on /C "set /p tfcontent= Fail func CheckSystemDriveAndRemoveDriveLetter(path string, inputOS string) (string, error) { + if inputOS == "" { inputOS = "linux" } @@ -193,9 +194,11 @@ func CheckSystemDriveAndRemoveDriveLetter(path string, inputOS string) (string, } parts := strings.SplitN(path, ":", 2) + + // we need to preserve the `/` or `\\` at the end because #5249 // Path does not have a drive letter. Just return it. if len(parts) < 2 { - return ToSlash(filepath.Clean(path), inputOS), nil + return ToSlash(filepath.Clean(path), inputOS) + pathHasTrailingSlash(parts[0]), nil } // We expect all paths to be in C: @@ -220,5 +223,16 @@ func CheckSystemDriveAndRemoveDriveLetter(path string, inputOS string) (string, // // We must return the second element of the split path, as is, without attempting to convert // it to an absolute path. We have no knowledge of the CWD; that is treated elsewhere. - return ToSlash(filepath.Clean(parts[1]), inputOS), nil + return ToSlash(filepath.Clean(parts[1]), inputOS) + pathHasTrailingSlash(parts[1]), nil +} + +// Checks if a path other than / or \\ has a trailing slash +// at the end, and returns it to be retained after running +// through the ToSlash(filepath.Clean()) functions which strips them off. +// eg. "/sample/" -> "/sample". +func pathHasTrailingSlash(path string) string { + if len(path) > 1 && (strings.HasSuffix(path, "/") || strings.HasSuffix(path, "\\")) { + return "/" + } + return "" }