cache: Fix --gc failure on Windows

Fixes "Error: failed to prune cache" on Windows and removes
work around from ec1c97e7e9.

Follows #10781.
This commit is contained in:
Oleksandr Redko 2023-03-04 16:49:25 +02:00 committed by Bjørn Erik Pedersen
parent 6c798eba60
commit 184a67ac47
2 changed files with 3 additions and 22 deletions

View file

@ -17,8 +17,6 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"runtime"
"strings"
"github.com/gohugoio/hugo/common/herrors" "github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/hugofs"
@ -73,8 +71,8 @@ func (c *Cache) Prune(force bool) (int, error) {
// This cache dir may not exist. // This cache dir may not exist.
return nil return nil
} }
defer f.Close()
_, err = f.Readdirnames(1) _, err = f.Readdirnames(1)
f.Close()
if err == io.EOF { if err == io.EOF {
// Empty dir. // Empty dir.
if name == "." { if name == "." {
@ -82,18 +80,6 @@ func (c *Cache) Prune(force bool) (int, error) {
err = nil err = nil
} else { } else {
err = c.Fs.Remove(name) 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
}
}
}
} }
} }

View file

@ -15,7 +15,6 @@ package filecache_test
import ( import (
"path/filepath" "path/filepath"
"runtime"
"testing" "testing"
"time" "time"
@ -90,12 +89,8 @@ iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAA
b.Assert(b.GCCount, qt.Equals, 1) b.Assert(b.GCCount, qt.Equals, 1)
// Build it again to GC the empty a dir. // Build it again to GC the empty a dir.
b.Build() b.Build()
if runtime.GOOS != "windows" { _, err = b.H.BaseFs.ResourcesCache.Stat(filepath.Join(imagesCacheDir, "a"))
// See issue #58860 -- this sometimes fails on Windows, b.Assert(err, qt.Not(qt.IsNil))
// 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(imagesCacheDir) _, err = b.H.BaseFs.ResourcesCache.Stat(imagesCacheDir)
b.Assert(err, qt.IsNil) b.Assert(err, qt.IsNil)