diff --git a/client/client_test.go b/client/client_test.go index fd562494d9a8..089a62883958 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -185,6 +185,7 @@ func TestIntegration(t *testing.T) { testSBOMScan, testSBOMScanSingleRef, testMultipleCacheExports, + testMountStubsDirectory, testMountStubsTimestamp, ) } @@ -8106,6 +8107,52 @@ func testMultipleCacheExports(t *testing.T, sb integration.Sandbox) { ensureFileContents(t, filepath.Join(destDir, "unique"), string(uniqueFile)) } +func testMountStubsDirectory(t *testing.T, sb integration.Sandbox) { + c, err := New(sb.Context(), sb.Address()) + require.NoError(t, err) + defer c.Close() + + st := llb.Image("busybox:latest").Run( + llb.Args([]string{"/bin/echo", "dummy"}), + llb.AddMount("/foo/bar", llb.Scratch(), llb.Tmpfs()), + ) + def, err := st.Marshal(sb.Context()) + require.NoError(t, err) + + tmpDir := t.TempDir() + tarFile := filepath.Join(tmpDir, "out.tar") + tarFileW, err := os.Create(tarFile) + require.NoError(t, err) + defer tarFileW.Close() + + _, err = c.Solve(sb.Context(), def, SolveOpt{ + Exports: []ExportEntry{ + { + Type: ExporterTar, + Output: fixedWriteCloser(tarFileW), + }, + }, + }, nil) + require.NoError(t, err) + tarFileW.Close() + + tarFileR, err := os.Open(tarFile) + require.NoError(t, err) + defer tarFileR.Close() + tarR := tar.NewReader(tarFileR) + + for { + hd, err := tarR.Next() + if errors.Is(err, io.EOF) { + break + } + require.NoError(t, err) + if hd.Name == "foo/bar/" { + require.Fail(t, "foo/bar/ should not be in the tar") + } + } +} + // /~https://github.com/moby/buildkit/issues/3148 func testMountStubsTimestamp(t *testing.T, sb integration.Sandbox) { c, err := New(sb.Context(), sb.Address()) diff --git a/executor/stubs.go b/executor/stubs.go index 167db203f657..f4b424d6e889 100644 --- a/executor/stubs.go +++ b/executor/stubs.go @@ -42,9 +42,18 @@ func MountStubsCleaner(dir string, mounts []Mount) func() { if err != nil { continue } - if st.Size() != 0 { + if st.IsDir() { + entries, err := os.ReadDir(p) + if err != nil { + continue + } + if len(entries) != 0 { + continue + } + } else if st.Size() != 0 { continue } + // Back up the timestamps of the dir for reproducible builds // /~https://github.com/moby/buildkit/issues/3148 dir := filepath.Dir(p)