diff --git a/frontend/dockerfile/dockerfile2llb/convert_runmount.go b/frontend/dockerfile/dockerfile2llb/convert_runmount.go index 095be0a0..3d9a83c3 100644 --- a/frontend/dockerfile/dockerfile2llb/convert_runmount.go +++ b/frontend/dockerfile/dockerfile2llb/convert_runmount.go @@ -124,6 +124,9 @@ func dispatchRunMounts(d *dispatchState, c *instructions.RunCommand, sources []* if mount.CacheSharing == instructions.MountSharingLocked { sharing = llb.CacheMountLocked } + if mount.CacheID == "" { + mount.CacheID = path.Clean(mount.Target) + } mountOpts = append(mountOpts, llb.AsPersistentCacheDir(opt.cacheIDNamespace+"/"+mount.CacheID, sharing)) } target := mount.Target diff --git a/frontend/dockerfile/dockerfile_mount_test.go b/frontend/dockerfile/dockerfile_mount_test.go index ead09bce..bbec4328 100644 --- a/frontend/dockerfile/dockerfile_mount_test.go +++ b/frontend/dockerfile/dockerfile_mount_test.go @@ -21,6 +21,7 @@ var mountTests = []integration.Test{ testMountContext, testMountTmpfs, testMountRWCache, + testCacheMountDefaultID, } func init() { @@ -193,3 +194,32 @@ RUN --mount=type=cache,target=/mycache,uid=1001,gid=1002,mode=0751 [ "$(stat -c }, nil) require.NoError(t, err) } + +func testCacheMountDefaultID(t *testing.T, sb integration.Sandbox) { + f := getFrontend(t, sb) + + dockerfile := []byte(` +FROM busybox +RUN --mount=type=cache,target=/mycache touch /mycache/foo +RUN --mount=type=cache,target=/mycache2 [ ! -f /mycache2/foo ] +RUN --mount=type=cache,target=/mycache [ -f /mycache/foo ] +`) + + dir, err := tmpdir( + fstest.CreateFile("Dockerfile", dockerfile, 0600), + ) + require.NoError(t, err) + defer os.RemoveAll(dir) + + c, err := client.New(context.TODO(), sb.Address()) + require.NoError(t, err) + defer c.Close() + + _, err = f.Solve(context.TODO(), c, client.SolveOpt{ + LocalDirs: map[string]string{ + builder.DefaultLocalNameDockerfile: dir, + builder.DefaultLocalNameContext: dir, + }, + }, nil) + require.NoError(t, err) +}