Skip to content

Commit

Permalink
fix: dot path normalized correctly for COPY
Browse files Browse the repository at this point in the history
In my previous fix on #4825, I had removed this line
knowing that all that had been addressed in `pahtRelativeToWorkingDir`:

```go
	if cfg.params.DestPath == "." // <-- this one
		|| cfg.params.DestPath == "" ||
		cfg.params.DestPath[len(cfg.params.DestPath)-1] == filepath.Separator {
		dest += string(filepath.Separator)
	}
```

However, I had overlooked the `"."` and `""` scenarios. `""`.
The  `"/"`case is handled correctly in `system.NormalizePath()`.

This change therefore undoes this, to make sure "." is transformed
correctly to "./" during COPY operation, same to "" -> "./". This
is important especially for WORKDIR that are not `/`, so that
`COPY --link` operations are handled properly.

fixes #5070

Signed-off-by: Anthony Nandaa <[email protected]>
  • Loading branch information
profnandaa committed Jun 26, 2024
1 parent 07e2dce commit c605289
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
9 changes: 6 additions & 3 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -1763,9 +1763,6 @@ func pathRelativeToWorkingDir(s llb.State, p string, platform ocispecs.Platform)
return "", err
}

if len(p) == 0 {
return dir, nil
}
p, err = system.CheckSystemDriveAndRemoveDriveLetter(p, platform.OS)
if err != nil {
return "", errors.Wrap(err, "removing drive letter")
Expand All @@ -1774,6 +1771,12 @@ func pathRelativeToWorkingDir(s llb.State, p string, platform ocispecs.Platform)
if system.IsAbs(p, platform.OS) {
return system.NormalizePath("/", p, platform.OS, true)
}

// add slashes for "" and "." paths
// "" is treated as current directory and not necessariy root
if p == "." || p == "" {
p = "./"
}
return system.NormalizePath(dir, p, platform.OS, true)
}

Expand Down
63 changes: 63 additions & 0 deletions frontend/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ var allTests = integration.TestFuncs(
testNamedMultiplatformInputContext,
testNamedFilteredContext,
testEmptyDestDir,
testCopyLinkDotDestDir,
testCopyLinkEmptyDestDir,
testCopyChownCreateDest,
testCopyThroughSymlinkContext,
testCopyThroughSymlinkMultiStage,
Expand Down Expand Up @@ -447,6 +449,67 @@ RUN [ "$(cat testfile)" == "contents0" ]
require.NoError(t, err)
}

func testCopyLinkDotDestDir(t *testing.T, sb integration.Sandbox) {
integration.SkipOnPlatform(t, "windows")
f := getFrontend(t, sb)

dockerfile := []byte(`
FROM busybox
WORKDIR /var/www
COPY --link testfile .
RUN [ "$(cat testfile)" == "contents0" ]
`)

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 testCopyLinkEmptyDestDir(t *testing.T, sb integration.Sandbox) {
integration.SkipOnPlatform(t, "windows")
f := getFrontend(t, sb)

dockerfile := []byte(`
FROM busybox
WORKDIR /var/www
ENV empty=""
COPY --link testfile $empty
RUN [ "$(cat testfile)" == "contents0" ]
`)

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 testExportCacheLoop(t *testing.T, sb integration.Sandbox) {
integration.SkipOnPlatform(t, "windows")
workers.CheckFeatureCompat(t, sb, workers.FeatureCacheExport, workers.FeatureCacheImport, workers.FeatureCacheBackendLocal)
Expand Down

0 comments on commit c605289

Please sign in to comment.