Skip to content

Commit

Permalink
cache: Fix --gc failure on Windows
Browse files Browse the repository at this point in the history
Fixes "Error: failed to prune cache" on Windows and removes
work around from ec1c97e.

Follows #10781.
  • Loading branch information
alexandear authored and bep committed Mar 4, 2023
1 parent 6c798eb commit 184a67a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 22 deletions.
16 changes: 1 addition & 15 deletions cache/filecache/filecache_pruner.go
Expand Up @@ -17,8 +17,6 @@ import (
"fmt"
"io"
"os"
"runtime"
"strings"

"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/hugofs"
Expand Down Expand Up @@ -73,27 +71,15 @@ func (c *Cache) Prune(force bool) (int, error) {
// This cache dir may not exist.
return nil
}
defer f.Close()
_, err = f.Readdirnames(1)
f.Close()
if err == io.EOF {
// Empty dir.
if name == "." {
// e.g. /_gen/images -- keep it even if empty.
err = nil
} else {
err = c.Fs.Remove(name)
if err != nil {
if runtime.GOOS == "windows" {
if strings.Contains(err.Error(), "used by another process") {
// See https://github.com/gohugoio/hugo/issues/10781
// This is a known issue on Windows with Go 1.20.
// There's not much we can do about it.
// So just return nil.
err = nil

}
}
}
}
}

Expand Down
9 changes: 2 additions & 7 deletions cache/filecache/integration_test.go
Expand Up @@ -15,7 +15,6 @@ package filecache_test

import (
"path/filepath"
"runtime"
"testing"
"time"

Expand Down Expand Up @@ -90,12 +89,8 @@ iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAA
b.Assert(b.GCCount, qt.Equals, 1)
// Build it again to GC the empty a dir.
b.Build()
if runtime.GOOS != "windows" {
// See issue #58860 -- this sometimes fails on Windows,
// but the empty directory will be removed on the next run.
_, err = b.H.BaseFs.ResourcesCache.Stat(filepath.Join(imagesCacheDir, "a"))
b.Assert(err, qt.Not(qt.IsNil))
}
_, err = b.H.BaseFs.ResourcesCache.Stat(filepath.Join(imagesCacheDir, "a"))
b.Assert(err, qt.Not(qt.IsNil))
_, err = b.H.BaseFs.ResourcesCache.Stat(imagesCacheDir)
b.Assert(err, qt.IsNil)

Expand Down

0 comments on commit 184a67a

Please sign in to comment.