Work around --gc failure on Windows <= 10

This applies two related fixes/improvements:

* The --gc now keeps empty `_resources/_gen/images` etc folders, even if empty. This should have been the behaviour
from the start.
* Also, if removal of an empty dir on Windows fails with the "used by another process" error, just ignore it for now.

Fixes #10781
This commit is contained in:
Bjørn Erik Pedersen
2023-03-04 11:09:03 +01:00
parent f10009e7f1
commit ec1c97e7e9
3 changed files with 131 additions and 1 deletions

View File

@@ -17,6 +17,8 @@ import (
"fmt"
"io"
"os"
"runtime"
"strings"
"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/hugofs"
@@ -66,6 +68,7 @@ func (c *Cache) Prune(force bool) (int, error) {
if info.IsDir() {
f, err := c.Fs.Open(name)
if err != nil {
// This cache dir may not exist.
return nil
@@ -74,7 +77,24 @@ func (c *Cache) Prune(force bool) (int, error) {
_, err = f.Readdirnames(1)
if err == io.EOF {
// Empty dir.
err = c.Fs.Remove(name)
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
}
}
}
}
}
if err != nil && !herrors.IsNotExist(err) {