Skip to content

Commit a5708a1

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(). Signed-off-by: Stephen Kitt <skitt@redhat.com>
1 parent 05aa087 commit a5708a1

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

pkg/internal/testing/addr/manager.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,17 @@ var (
4343

4444
func init() {
4545
baseDir, err := os.UserCacheDir()
46+
if err == nil {
47+
cacheDir = filepath.Join(baseDir, "kubebuilder-envtest")
48+
err = os.MkdirAll(cacheDir, 0o750)
49+
}
4650
if err != nil {
51+
// Either we didn't get a cache directory, or we can't use it
4752
baseDir = os.TempDir()
53+
cacheDir = filepath.Join(baseDir, "kubebuilder-envtest")
54+
err = os.MkdirAll(cacheDir, 0o750)
4855
}
49-
cacheDir = filepath.Join(baseDir, "kubebuilder-envtest")
50-
if err := os.MkdirAll(cacheDir, 0750); err != nil {
56+
if err != nil {
5157
panic(err)
5258
}
5359
}

0 commit comments

Comments
 (0)