cmd/go/internal/clean: add logging to help debug openbsd flakes

This change adds extra logging in the case where there's an error
removing all the files in the gomodcache using modfetch.RemoveAll.
It logs the names of the files found in GOMODCACHE as well as their
modes. The modes are included because they should all be writable by the
time we call robustio.RemoveAll.

For #68087

Change-Id: Id9ae68bf6a3392baf88ec002d08fed1faf525927
Reviewed-on: https://go-review.googlesource.com/c/go/+/658816
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Michael Matloob <matloob@golang.org>
This commit is contained in:
Michael Matloob 2025-03-18 13:00:42 -04:00 committed by Gopher Robot
parent a17c092c2c
commit 6b18311bbc

View file

@ -7,8 +7,10 @@ package clean
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"runtime"
@ -216,6 +218,15 @@ func runClean(ctx context.Context, cmd *base.Command, args []string) {
if !cfg.BuildN {
if err := modfetch.RemoveAll(cfg.GOMODCACHE); err != nil {
base.Error(err)
// Add extra logging for the purposes of debugging #68087.
// We're getting ENOTEMPTY errors on openbsd from RemoveAll.
// Check for os.ErrExist, which can match syscall.ENOTEMPTY
// and syscall.EEXIST, because syscall.ENOTEMPTY is not defined
// on all platforms.
if runtime.GOOS == "openbsd" && errors.Is(err, fs.ErrExist) {
logFilesInGOMODCACHE()
}
}
}
}
@ -228,6 +239,29 @@ func runClean(ctx context.Context, cmd *base.Command, args []string) {
}
}
// logFilesInGOMODCACHE reports the file names and modes for the files in GOMODCACHE using base.Error.
func logFilesInGOMODCACHE() {
var found []string
werr := filepath.WalkDir(cfg.GOMODCACHE, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
var mode string
info, err := d.Info()
if err == nil {
mode = info.Mode().String()
} else {
mode = fmt.Sprintf("<err: %s>", info.Mode())
}
found = append(found, fmt.Sprintf("%s (mode: %s)", path, mode))
return nil
})
if werr != nil {
base.Errorf("walking files in GOMODCACHE (for debugging go.dev/issue/68087): %v", werr)
}
base.Errorf("files in GOMODCACHE (for debugging go.dev/issue/68087):\n%s", strings.Join(found, "\n"))
}
var cleaned = map[*load.Package]bool{}
// TODO: These are dregs left by Makefile-based builds.