diff options
author | Bjørn Erik Pedersen <[email protected]> | 2023-07-27 20:59:47 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2023-07-27 20:59:47 +0200 |
commit | b3f10556f0ab8a92bdd92e25c67685947317d188 (patch) | |
tree | 61faa97a403aec86207bd50d8aa9b815dc3aa579 /helpers | |
parent | 4d7af757c99a561744c87c477d10b89e5c9d7e35 (diff) | |
download | hugo-b3f10556f0ab8a92bdd92e25c67685947317d188.tar.gz hugo-b3f10556f0ab8a92bdd92e25c67685947317d188.zip |
Use os.UserCacheDir as first fallback if cacheDir is not set
We will now try
1. cacheDir (or, commonly set in environment as `HUGO_CACHEDIR`)
2. if on Netlify we use `/opt/build/cache/hugo_cache/`
3. os.UserCacheDir
4. A temp dir
Storing the cache, especially the module cache, in a temporary idea has had lots of hard to debug issues, especially on MacOS,
which this commit tries to fix.
This should also make it easier to locate the Hugo cache:
>UserCacheDir returns the default root directory to use for user-specific cached data. Users should create their own
application-specific subdirectory within this one and use that.
>
>On Unix systems, it returns $XDG_CACHE_HOME as specified by
https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html if non-empty, else $HOME/.cache. On Darwin, it
returns $HOME/Library/Caches. On Windows, it returns %LocalAppData%. On Plan 9, it returns $home/lib/cache.
>
>If the location cannot be determined (for example, $HOME is not defined), then it will return an error.
Fixes #11286
Fixes #11291
Diffstat (limited to 'helpers')
-rw-r--r-- | helpers/path.go | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/helpers/path.go b/helpers/path.go index 4c2799ac2..57877bfed 100644 --- a/helpers/path.go +++ b/helpers/path.go @@ -394,6 +394,7 @@ func OpenFileForWriting(fs afero.Fs, filename string) (afero.File, error) { // The dir will be created if it does not exist. func GetCacheDir(fs afero.Fs, cacheDir string) (string, error) { cacheDir = cacheDirDefault(cacheDir) + if cacheDir != "" { exists, err := DirExists(cacheDir, fs) if err != nil { @@ -408,12 +409,22 @@ func GetCacheDir(fs afero.Fs, cacheDir string) (string, error) { return cacheDir, nil } + const hugoCacheBase = "hugo_cache" + + userCacheDir, err := os.UserCacheDir() + if err == nil { + cacheDir := filepath.Join(userCacheDir, hugoCacheBase) + if err := fs.Mkdir(cacheDir, 0777); err == nil || os.IsExist(err) { + return cacheDir, nil + } + } + // Fall back to a cache in /tmp. userName := os.Getenv("USER") if userName != "" { - return GetTempDir("hugo_cache_"+userName, fs), nil + return GetTempDir(hugoCacheBase+"_"+userName, fs), nil } else { - return GetTempDir("hugo_cache", fs), nil + return GetTempDir(hugoCacheBase, fs), nil } } @@ -433,7 +444,7 @@ func cacheDirDefault(cacheDir string) string { return "/opt/build/cache/hugo_cache/" } - // This will fall back to an hugo_cache folder in the tmp dir, which should work fine for most CI + // This will fall back to an hugo_cache folder in either os.UserCacheDir or the tmp dir, which should work fine for most CI // providers. See this for a working CircleCI setup: // https://github.com/bep/hugo-sass-test/blob/6c3960a8f4b90e8938228688bc49bdcdd6b2d99e/.circleci/config.yml // If not, they can set the HUGO_CACHEDIR environment variable or cacheDir config key. |