]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
cache: Set default cache path based on $USER
authorErica Mays <erica@emays.dev>
Sat, 10 Jun 2023 17:25:07 +0000 (13:25 -0400)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 12 Jun 2023 19:33:35 +0000 (21:33 +0200)
Change the default cache directory to `$TMPDIR/hugo_cache_$USER`, so
that multi-user systems do not have caches that interfere with each
other.  The other cache-choosing logic (e.g. Netlify exceptions,
configuration options) are not affected.

Fixes #7391

16 files changed:
commands/commandeer.go
docs/content/en/commands/hugo.md
docs/content/en/commands/hugo_config.md
docs/content/en/commands/hugo_config_mounts.md
docs/content/en/commands/hugo_mod_clean.md
docs/content/en/commands/hugo_mod_graph.md
docs/content/en/commands/hugo_mod_init.md
docs/content/en/commands/hugo_mod_npm_pack.md
docs/content/en/commands/hugo_mod_tidy.md
docs/content/en/commands/hugo_mod_vendor.md
docs/content/en/commands/hugo_mod_verify.md
docs/content/en/commands/hugo_new_content.md
docs/content/en/commands/hugo_server.md
docs/content/en/getting-started/configuration.md
docs/content/en/templates/data-templates.md
helpers/path.go

index d43d06b8a757f9136bc761b34dada4b440cbee24..da395baa58f8e8ccdccb611c48986428913ca324 100644 (file)
@@ -521,7 +521,7 @@ Complete documentation is available at https://gohugo.io/.`
 func applyLocalFlagsBuildConfig(cmd *cobra.Command, r *rootCommand) {
        cmd.Flags().StringSliceP("theme", "t", []string{}, "themes to use (located in /themes/THEMENAME/)")
        cmd.Flags().StringVarP(&r.baseURL, "baseURL", "b", "", "hostname (and path) to the root, e.g. https://spf13.com/")
-       cmd.Flags().StringP("cacheDir", "", "", "filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/")
+       cmd.Flags().StringP("cacheDir", "", "", "filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache_$USER/")
        _ = cmd.Flags().SetAnnotation("cacheDir", cobra.BashCompSubdirsInDir, []string{})
        cmd.Flags().StringP("contentDir", "c", "", "filesystem path to content directory")
        _ = cmd.Flags().SetAnnotation("theme", cobra.BashCompSubdirsInDir, []string{"themes"})
index 73d443169e6fc58c738d1901cbeeaa01f4828770..8f3569abd31a03d477271f66ded78ef26ffdb7a0 100644 (file)
@@ -27,7 +27,7 @@ hugo [flags]
   -D, --buildDrafts                include content marked as draft
   -E, --buildExpired               include expired content
   -F, --buildFuture                include content with publishdate in the future
-      --cacheDir string            filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/
+      --cacheDir string            filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache_$USER/
       --cleanDestinationDir        remove files from destination not found in static directories
       --clock string               set the clock used by Hugo, e.g. --clock 2021-11-06T22:30:00.00+09:00
       --config string              config file (default is hugo.yaml|json|toml)
index 42cd188b7188fbe643394a81a009f1eaf1ae5521..e3204ba1f427129f56ccfd7beee808cf7aaa3519 100644 (file)
@@ -19,7 +19,7 @@ hugo config [command] [flags]
 
 ```
   -b, --baseURL string      hostname (and path) to the root, e.g. https://spf13.com/
-      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/
+      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache_$USER/
   -c, --contentDir string   filesystem path to content directory
       --format string       preferred file format (toml, yaml or json) (default "toml")
   -h, --help                help for config
index e4c4cf8c561632b9e6d0b265ddf40dda04c3035a..70f9751424caf8fc15fd3bc101c953d44b7aba06 100644 (file)
@@ -15,7 +15,7 @@ hugo config mounts [flags] [args]
 
 ```
   -b, --baseURL string      hostname (and path) to the root, e.g. https://spf13.com/
-      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/
+      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache_$USER/
   -c, --contentDir string   filesystem path to content directory
   -h, --help                help for mounts
   -t, --theme strings       themes to use (located in /themes/THEMENAME/)
index 7878fe178f2f6389e9c538f92ab2df8ca0fed24a..bb6b735b376f787be6823fbf5798b94f7604c3f0 100644 (file)
@@ -20,7 +20,7 @@ hugo mod clean [flags] [args]
 ```
       --all                 clean entire module cache
   -b, --baseURL string      hostname (and path) to the root, e.g. https://spf13.com/
-      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/
+      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache_$USER/
   -c, --contentDir string   filesystem path to content directory
   -h, --help                help for clean
       --pattern string      pattern matching module paths to clean (all if not set), e.g. "**hugo*"
index fa1d66602b9536666dcd04edcca88013203016b0..26c89371eda7781275f8c114a8974aa9d78c9265 100644 (file)
@@ -21,7 +21,7 @@ hugo mod graph [flags] [args]
 
 ```
   -b, --baseURL string      hostname (and path) to the root, e.g. https://spf13.com/
-      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/
+      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache_$USER/
       --clean               delete module cache for dependencies that fail verification
   -c, --contentDir string   filesystem path to content directory
   -h, --help                help for graph
index af1575b734f6f96489916ad5adefe8c7ae833bf6..0def6b91c56df9a371bc812757ed505305835c76 100644 (file)
@@ -26,7 +26,7 @@ hugo mod init [flags] [args]
 
 ```
   -b, --baseURL string      hostname (and path) to the root, e.g. https://spf13.com/
-      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/
+      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache_$USER/
   -c, --contentDir string   filesystem path to content directory
   -h, --help                help for init
   -t, --theme strings       themes to use (located in /themes/THEMENAME/)
index 6236499099dc1c946e8badb7478d010a50adb5cc..65404e009e4fea6caed3d369a4ba4bfd28e4d674 100644 (file)
@@ -29,7 +29,7 @@ hugo mod npm pack [flags] [args]
 
 ```
   -b, --baseURL string      hostname (and path) to the root, e.g. https://spf13.com/
-      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/
+      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache_$USER/
   -c, --contentDir string   filesystem path to content directory
   -h, --help                help for pack
   -t, --theme strings       themes to use (located in /themes/THEMENAME/)
index 31558a55bf2f3d303fdb05a0e481f50656e4e1b1..f58204a2b4de6405ea8e26244899b51306d8cf1e 100644 (file)
@@ -15,7 +15,7 @@ hugo mod tidy [flags] [args]
 
 ```
   -b, --baseURL string      hostname (and path) to the root, e.g. https://spf13.com/
-      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/
+      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache_$USER/
   -c, --contentDir string   filesystem path to content directory
   -h, --help                help for tidy
   -t, --theme strings       themes to use (located in /themes/THEMENAME/)
index 42d19b36e5192173982149da21d58ce47a409c7c..d6e8ca0a190ef20f5958bb22a974177b594bd24b 100644 (file)
@@ -21,7 +21,7 @@ hugo mod vendor [flags] [args]
 
 ```
   -b, --baseURL string      hostname (and path) to the root, e.g. https://spf13.com/
-      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/
+      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache_$USER/
   -c, --contentDir string   filesystem path to content directory
   -h, --help                help for vendor
   -t, --theme strings       themes to use (located in /themes/THEMENAME/)
index 861ef575265beb5e8e7c128a37338318b2bec7b0..b712432468aa67fb58ad17d0e4a5665f7c7e2aac 100644 (file)
@@ -19,7 +19,7 @@ hugo mod verify [flags] [args]
 
 ```
   -b, --baseURL string      hostname (and path) to the root, e.g. https://spf13.com/
-      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/
+      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache_$USER/
       --clean               delete module cache for dependencies that fail verification
   -c, --contentDir string   filesystem path to content directory
   -h, --help                help for verify
index fdacc65963be31f8529e774187688069cfbcd6a9..c566b1e556e900ad4a055f030a34b11fcf421167 100644 (file)
@@ -26,7 +26,7 @@ hugo new content [path] [flags]
 
 ```
   -b, --baseURL string      hostname (and path) to the root, e.g. https://spf13.com/
-      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/
+      --cacheDir string     filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache_$USER/
   -c, --contentDir string   filesystem path to content directory
       --editor string       edit new content with this editor, if provided
   -f, --force               overwrite file if it already exists
index d328ee68551cfdaaaaad2c7eabb9601f9ab7f279..19055825a96c4a1c486e573b85905ac4281f76de 100644 (file)
@@ -33,7 +33,7 @@ hugo server [command] [flags]
   -D, --buildDrafts            include content marked as draft
   -E, --buildExpired           include expired content
   -F, --buildFuture            include content with publishdate in the future
-      --cacheDir string        filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/
+      --cacheDir string        filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache_$USER/
       --cleanDestinationDir    remove files from destination not found in static directories
   -c, --contentDir string      filesystem path to content directory
       --disableBrowserError    do not show build errors in the browser
index 2ab9e6a33ba35f39abed993742350537c8d9f299..05465967ae62971044b3c6d0aa825ba5b8e979d2 100644 (file)
@@ -807,7 +807,7 @@ You can override any of these cache settings in your own `hugo.toml`.
 ### The keywords explained
 
 `:cacheDir`
-: This is the value of the `cacheDir` config option if set (can also be set via OS env variable `HUGO_CACHEDIR`). It will fall back to `/opt/build/cache/hugo_cache/` on Netlify, or a `hugo_cache` directory below the OS temp dir for the others. This means that if you run your builds on Netlify, all caches configured with `:cacheDir` will be saved and restored on the next build. For other CI vendors, please read their documentation. For an CircleCI example, see [this configuration](https://github.com/bep/hugo-sass-test/blob/6c3960a8f4b90e8938228688bc49bdcdd6b2d99e/.circleci/config.yml).
+: This is the value of the `cacheDir` config option if set (can also be set via OS env variable `HUGO_CACHEDIR`). It will fall back to `/opt/build/cache/hugo_cache/` on Netlify, or a `hugo_cache_$USER` directory below the OS temp dir for the others. This means that if you run your builds on Netlify, all caches configured with `:cacheDir` will be saved and restored on the next build. For other CI vendors, please read their documentation. For an CircleCI example, see [this configuration](https://github.com/bep/hugo-sass-test/blob/6c3960a8f4b90e8938228688bc49bdcdd6b2d99e/.circleci/config.yml).
 
 `:project`
 : The base directory name of the current Hugo project. This means that, in its default setting, every project will have separated file caches, which means that when you do `hugo --gc` you will not touch files related to other Hugo projects running on the same PC.
index ee44fb60ebe19879d1e2b1db3c01930545a8b943..c9b676a7da9a541efcd064707aaa6be07d538d89 100644 (file)
@@ -209,7 +209,7 @@ The expression `{{ index $r number }}` must be used to output the nth-column fro
 
 ### Cache URLs
 
-Each downloaded URL will be cached in the default folder `$TMPDIR/hugo_cache/`. The variable `$TMPDIR` will be resolved to your system-dependent temporary directory.
+Each downloaded URL will be cached in the default folder `$TMPDIR/hugo_cache_$USER/`. The variable `$TMPDIR` will be resolved to your system-dependent temporary directory.
 
 With the command-line flag `--cacheDir`, you can specify any folder on your system as a caching directory.
 
index 00c541babe8a27435e91527d23bd7262cc229723..4c2799ac26507421f80dd8b6645e9cd688b036ae 100644 (file)
@@ -409,7 +409,12 @@ func GetCacheDir(fs afero.Fs, cacheDir string) (string, error) {
        }
 
        // Fall back to a cache in /tmp.
-       return GetTempDir("hugo_cache", fs), nil
+       userName := os.Getenv("USER")
+       if userName != "" {
+               return GetTempDir("hugo_cache_"+userName, fs), nil
+       } else {
+               return GetTempDir("hugo_cache", fs), nil
+       }
 }
 
 func cacheDirDefault(cacheDir string) string {