From 1a5289cb21f9b93cb5666bd8696692a384f08143 Mon Sep 17 00:00:00 2001 From: Anthony Nandaa Date: Tue, 10 Sep 2024 00:55:17 -0700 Subject: [PATCH] fix: util/path: CheckSystemDriveAndRemoveDriveLetter to preserve `/` The call to CheckSystemDriveAndRemoveDriveLetter() does not preserve the trailing `/` or `\\`. This happens because `filepath.Clean()` strips away any trailing slashes. For example `/sample/` will be `\\sample` on Windows. This function was mainly written for Windows scenarios, which have System Drive Letters like C:/, etc. This was causing cases like `COPY testfile /testdir/` to be intepreted as `COPY testfile /testdir`, and if `testdir` is not explictly created before the call, it ends up being treated as a destination file other than a directory. Fix this by checking that if we have a trailing `/` or `\\`, we preserve it after the call to `filepath.Clean()`. Fixes #5249 Signed-off-by: Anthony Nandaa --- frontend/dockerfile/dockerfile_test.go | 37 ++++++++++++++++++++++++++ util/system/path.go | 24 +++++++++++++++-- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/frontend/dockerfile/dockerfile_test.go b/frontend/dockerfile/dockerfile_test.go index 8d4f25401752..526d00e54cd8 100644 --- a/frontend/dockerfile/dockerfile_test.go +++ b/frontend/dockerfile/dockerfile_test.go @@ -152,6 +152,7 @@ var allTests = integration.TestFuncs( testNamedMultiplatformInputContext, testNamedFilteredContext, testEmptyDestDir, + testPreserveDestDirSlash, testCopyLinkDotDestDir, testCopyLinkEmptyDestDir, testCopyChownCreateDest, @@ -547,6 +548,42 @@ RUN cmd /V:on /C "set /p tfcontent= 1 && !hasTrailingSlash { + if strings.HasSuffix(origPath, "/") { + path += "/" + } else if strings.HasSuffix(origPath, "\\") { + path += "\\" + } + } + return path }