Skip to content

Commit 8104aa8

Browse files
committed
Try a temporary directory if the user cache fails
Since commit 58c17f6 ("addr.Suggest should lock a file instead of memory"), pkg/internal/testing/addr/manager.go’s init() function tries to create a directory using either os.UserCacheDir() or os.TempDir(), whichever succeeds first. In many build environments, $HOME is non-empty but points to an unusable directory; in such cases, os.UserCacheDir() returns an unusable directory, which causes init() to panic. This changes init() to first try os.UserCacheDir(), including creating the desired directory; if that fails, the whole operation is tried again with os.TempDir(). Fixes: #1799 Signed-off-by: Stephen Kitt <skitt@redhat.com>
1 parent 273e608 commit 8104aa8

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

pkg/internal/testing/addr/manager.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,30 @@ var (
4242
)
4343

4444
func init() {
45-
baseDir, err := os.UserCacheDir()
45+
var err error
46+
cacheDir, err = tryUserCacheDir()
4647
if err != nil {
47-
baseDir = os.TempDir()
48+
// Either we didn't get a cache directory, or we can't use it
49+
cacheDir, err = tryCacheInDir(os.TempDir())
4850
}
49-
cacheDir = filepath.Join(baseDir, "kubebuilder-envtest")
50-
if err := os.MkdirAll(cacheDir, 0750); err != nil {
51+
if err != nil {
5152
panic(err)
5253
}
5354
}
5455

56+
func tryUserCacheDir() (string, error) {
57+
baseDir, err := os.UserCacheDir()
58+
if err != nil {
59+
return "", err
60+
}
61+
return tryCacheInDir(baseDir)
62+
}
63+
64+
func tryCacheInDir(dir string) (string, error) {
65+
dir = filepath.Join(dir, "kubebuilder-envtest")
66+
return dir, os.MkdirAll(dir, 0o750)
67+
}
68+
5569
type portCache struct{}
5670

5771
func (c *portCache) add(port int) (bool, error) {

0 commit comments

Comments
 (0)