Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: util/path: CheckSystemDriveAndRemoveDriveLetter to preserve / #5317

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions frontend/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ var allTests = integration.TestFuncs(
testNamedMultiplatformInputContext,
testNamedFilteredContext,
testEmptyDestDir,
testPreserveDestDirSlash,
testCopyLinkDotDestDir,
testCopyLinkEmptyDestDir,
testCopyChownCreateDest,
Expand Down Expand Up @@ -547,6 +548,42 @@ RUN cmd /V:on /C "set /p tfcontent=<testfile \
require.NoError(t, err)
}

func testPreserveDestDirSlash(t *testing.T, sb integration.Sandbox) {
f := getFrontend(t, sb)

dockerfile := []byte(integration.UnixOrWindows(
`
FROM busybox
COPY testfile /sample/
RUN [ "$(cat /sample/testfile)" == "contents0" ]
`,
`
FROM nanoserver
COPY testfile /sample/
RUN cmd /V:on /C "set /p tfcontent=<\sample\testfile \
& if !tfcontent! NEQ contents0 (exit 1)"
`,
))

dir := integration.Tmpdir(
t,
fstest.CreateFile("Dockerfile", dockerfile, 0600),
fstest.CreateFile("testfile", []byte("contents0"), 0600),
)

c, err := client.New(sb.Context(), sb.Address())
require.NoError(t, err)
defer c.Close()

_, err = f.Solve(sb.Context(), c, client.SolveOpt{
LocalMounts: map[string]fsutil.FS{
dockerui.DefaultLocalNameDockerfile: dir,
dockerui.DefaultLocalNameContext: dir,
},
}, nil)
require.NoError(t, err)
}

func testCopyLinkDotDestDir(t *testing.T, sb integration.Sandbox) {
integration.SkipOnPlatform(t, "windows")
f := getFrontend(t, sb)
Expand Down
7 changes: 5 additions & 2 deletions util/system/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,11 @@ func CheckSystemDriveAndRemoveDriveLetter(path string, inputOS string) (string,
}

parts := strings.SplitN(path, ":", 2)

// Path does not have a drive letter. Just return it.
if len(parts) < 2 {
return ToSlash(filepath.Clean(path), inputOS), nil
cleanedPath := filepath.Clean(path)
return ToSlash(keepTrailingSlash(cleanedPath, path, inputOS), inputOS), nil
}

// We expect all paths to be in C:
Expand All @@ -220,5 +222,6 @@ 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
cleanedPath := filepath.Clean(parts[1])
return ToSlash(keepTrailingSlash(cleanedPath, parts[1], inputOS), inputOS), nil
}
10 changes: 10 additions & 0 deletions util/system/path_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build !windows
// +build !windows

package system

// this was addressing a Windows-specific issue #5249,
// does nothing on unix.
func keepTrailingSlash(cleanedPath, _, _ string) string {
return cleanedPath
}
17 changes: 17 additions & 0 deletions util/system/path_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package system

import "strings"

// Checks if a path other than root has a trailing slash
// and retains it. See https://github.com/moby/buildkit/issues/5249
// Expects cleanedPath to have gone through filepath.Clean
func keepTrailingSlash(cleanedPath, origPath, inputOS string) string {
hasTrailingSlash := strings.HasSuffix(origPath, "\\") ||
strings.HasSuffix(origPath, "/")

if len(cleanedPath) > 1 && hasTrailingSlash && inputOS == "windows" {
// for cleaned paths, / will turn to \\
return cleanedPath + "\\"
}
return cleanedPath
}
Loading