]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Use os.UserCacheDir as first fallback if cacheDir is not set
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 27 Jul 2023 18:59:47 +0000 (20:59 +0200)
committerGitHub <noreply@github.com>
Thu, 27 Jul 2023 18:59:47 +0000 (20:59 +0200)
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

config/allconfig/allconfig.go
helpers/path.go
main_test.go
testscripts/commands/config__cachedir.txt [new file with mode: 0644]

index 72f2883f258db9120102fbcd5117d3be49f0f7dd..ec95f5eba885c0d23ff3dfbf024692389376f5a7 100644 (file)
@@ -729,6 +729,7 @@ func fromLoadConfigResult(fs afero.Fs, logger loggers.Logger, res config.LoadCon
        cfg := res.Cfg
 
        all := &Config{}
+
        err := decodeConfigFromParams(fs, bcfg, cfg, all, nil)
        if err != nil {
                return nil, err
@@ -871,6 +872,10 @@ func fromLoadConfigResult(fs afero.Fs, logger loggers.Logger, res config.LoadCon
 
        bcfg.PublishDir = all.PublishDir
        res.BaseConfig = bcfg
+       all.CommonDirs.CacheDir = bcfg.CacheDir
+       for _, l := range langConfigs {
+               l.CommonDirs.CacheDir = bcfg.CacheDir
+       }
 
        cm := &Configs{
                Base:                  all,
index 4c2799ac26507421f80dd8b6645e9cd688b036ae..57877bfedc5b061bef03404965ceb2b110ec790a 100644 (file)
@@ -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.
index 6274cd9bb47bdd38e178eea7ce42a3109ab19c7c..c5a59828f73f97adea447adb5ebcbd6e2cf0ff60 100644 (file)
@@ -375,8 +375,23 @@ func testSetupFunc() func(env *testscript.Env) error {
        return func(env *testscript.Env) error {
                var keyVals []string
                keyVals = append(keyVals, "HUGO_TESTRUN", "true")
-               hugoCachedDir := filepath.Join(env.WorkDir, "hugocache")
-               keyVals = append(keyVals, "HUGO_CACHEDIR", hugoCachedDir)
+               keyVals = append(keyVals, "HUGO_CACHEDIR", filepath.Join(env.WorkDir, "hugocache"))
+               xdghome := filepath.Join(env.WorkDir, "xdgcachehome")
+               keyVals = append(keyVals, "XDG_CACHE_HOME", xdghome)
+               home := filepath.Join(env.WorkDir, "home")
+               keyVals = append(keyVals, "HOME", home)
+
+               if runtime.GOOS == "darwin" {
+                       if err := os.MkdirAll(filepath.Join(home, "Library", "Caches"), 0777); err != nil {
+                               return err
+                       }
+               }
+
+               if runtime.GOOS == "linux" {
+                       if err := os.MkdirAll(xdghome, 0777); err != nil {
+                               return err
+                       }
+               }
 
                keyVals = append(keyVals, "SOURCE", sourceDir)
 
diff --git a/testscripts/commands/config__cachedir.txt b/testscripts/commands/config__cachedir.txt
new file mode 100644 (file)
index 0000000..aecb40b
--- /dev/null
@@ -0,0 +1,18 @@
+
+[windows] skip
+
+env HUGO_CACHEDIR=
+hugo config
+
+[darwin] stdout 'home/Library/Caches/hugo_cache'
+[linux] stdout 'xdgcachehome/hugo_cache'
+
+# Repeat it to make sure it handles an existing hugo_cache dir.
+hugo config
+
+[darwin] stdout 'home/Library/Caches/hugo_cache'
+[linux] stdout 'xdgcachehome/hugo_cache'
+
+-- hugo.toml --
+baseURL="https://example.com/"
+title="My New Hugo Site"