Repository: gohugoio/hugo (language: go)

Issue:
user /tmp/hugo_$USER instead of /tmp/hugo_cache
Using 0.72 compiled from master from 2 days ago on Ubuntu 16.04 with go 1.14. Hugo by default uses /tmp/hugo_cache to store temp files. This is a problem on a multiuser system like ours. For example, one user runs `hugo server` on their project folder. Another user attempt to run `hugo` and fails as it can't write to `/tmp/hugo_cache` being owned by the previous user.

I know it can be overcome by `--cacheDir` but it would be better to have hugo by default use `/tmp/hugo_$USER` instead of the hardcoded `/tmp/hugo` (better never to hardcode things in general anyway). Or make it a random unique dir if it's easier for cross-OS compatibility, making sure gc is doing its job.


Discussion / hints:
You can also do something like this in `~/.bashrc`:

```
export HUGO_CACHEDIR="hugo_cache_$USER"
```
Indeed, and that's perhaps a better workaround than using --cacheDir, but I was advocating that this is best addressed in the default implementation.
> this is best addressed in the default implementation.

Agreed.
Related issue: https://github.com/gohugoio/hugo/issues/5487
If you don't mind having all of your caches under source control (unless specifically ignored), you can also override the default location in [`config.toml`](https://gohugo.io/getting-started/configuration/#configure-file-caches). If all caches are set to `:resourceDir/_gen`, a `hugo_cache` directory in created in `/tmp` but remains empty. I have no idea if there are negative side effects. I am certain there is a Very Good Reason for placing the caches for getcsv, getjson, and modules in a temporary location outside of the project directory.
That's another useful workaround, thanks. I guess avoiding the repo is just general good practice (folks accidentally adding them to source control and then committing large sets on every commit). I found many hugo themes which do not ignore /resources (but ignore /public).
If you read https://gohugo.io/getting-started/configuration/#configure-file-caches

There have been some thinking in this area, see the `:project` placeholder.

Some of the caches should be safe to share between users/projects.
Another option is to use the paths as specified by https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html first and then to default to an hard-coded value.

Quick workaround I used for reference:
```
HUGO_CACHEDIR="${PWD}/.hugo_cache/" hugo ...
```
See https://github.com/gohugoio/hugo/issues/9452 (closed as duplicate) for additional detail.

Fix patch:
diff --git a/commands/commandeer.go b/commands/commandeer.go
index d43d06b8a75..da395baa58f 100644
--- a/commands/commandeer.go
+++ b/commands/commandeer.go
@@ -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"})
diff --git a/docs/content/en/commands/hugo.md b/docs/content/en/commands/hugo.md
index 73d443169e6..8f3569abd31 100644
--- a/docs/content/en/commands/hugo.md
+++ b/docs/content/en/commands/hugo.md
@@ -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)
diff --git a/docs/content/en/commands/hugo_config.md b/docs/content/en/commands/hugo_config.md
index 42cd188b718..e3204ba1f42 100644
--- a/docs/content/en/commands/hugo_config.md
+++ b/docs/content/en/commands/hugo_config.md
@@ -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
diff --git a/docs/content/en/commands/hugo_config_mounts.md b/docs/content/en/commands/hugo_config_mounts.md
index e4c4cf8c561..70f9751424c 100644
--- a/docs/content/en/commands/hugo_config_mounts.md
+++ b/docs/content/en/commands/hugo_config_mounts.md
@@ -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/)
diff --git a/docs/content/en/commands/hugo_mod_clean.md b/docs/content/en/commands/hugo_mod_clean.md
index 7878fe178f2..bb6b735b376 100644
--- a/docs/content/en/commands/hugo_mod_clean.md
+++ b/docs/content/en/commands/hugo_mod_clean.md
@@ -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*"
diff --git a/docs/content/en/commands/hugo_mod_graph.md b/docs/content/en/commands/hugo_mod_graph.md
index fa1d66602b9..26c89371eda 100644
--- a/docs/content/en/commands/hugo_mod_graph.md
+++ b/docs/content/en/commands/hugo_mod_graph.md
@@ -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
diff --git a/docs/content/en/commands/hugo_mod_init.md b/docs/content/en/commands/hugo_mod_init.md
index af1575b734f..0def6b91c56 100644
--- a/docs/content/en/commands/hugo_mod_init.md
+++ b/docs/content/en/commands/hugo_mod_init.md
@@ -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/)
diff --git a/docs/content/en/commands/hugo_mod_npm_pack.md b/docs/content/en/commands/hugo_mod_npm_pack.md
index 6236499099d..65404e009e4 100644
--- a/docs/content/en/commands/hugo_mod_npm_pack.md
+++ b/docs/content/en/commands/hugo_mod_npm_pack.md
@@ -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/)
diff --git a/docs/content/en/commands/hugo_mod_tidy.md b/docs/content/en/commands/hugo_mod_tidy.md
index 31558a55bf2..f58204a2b4d 100644
--- a/docs/content/en/commands/hugo_mod_tidy.md
+++ b/docs/content/en/commands/hugo_mod_tidy.md
@@ -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/)
diff --git a/docs/content/en/commands/hugo_mod_vendor.md b/docs/content/en/commands/hugo_mod_vendor.md
index 42d19b36e51..d6e8ca0a190 100644
--- a/docs/content/en/commands/hugo_mod_vendor.md
+++ b/docs/content/en/commands/hugo_mod_vendor.md
@@ -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/)
diff --git a/docs/content/en/commands/hugo_mod_verify.md b/docs/content/en/commands/hugo_mod_verify.md
index 861ef575265..b712432468a 100644
--- a/docs/content/en/commands/hugo_mod_verify.md
+++ b/docs/content/en/commands/hugo_mod_verify.md
@@ -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
diff --git a/docs/content/en/commands/hugo_new_content.md b/docs/content/en/commands/hugo_new_content.md
index fdacc65963b..c566b1e556e 100644
--- a/docs/content/en/commands/hugo_new_content.md
+++ b/docs/content/en/commands/hugo_new_content.md
@@ -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
diff --git a/docs/content/en/commands/hugo_server.md b/docs/content/en/commands/hugo_server.md
index d328ee68551..19055825a96 100644
--- a/docs/content/en/commands/hugo_server.md
+++ b/docs/content/en/commands/hugo_server.md
@@ -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
diff --git a/docs/content/en/getting-started/configuration.md b/docs/content/en/getting-started/configuration.md
index 2ab9e6a33ba..05465967ae6 100644
--- a/docs/content/en/getting-started/configuration.md
+++ b/docs/content/en/getting-started/configuration.md
@@ -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.
diff --git a/docs/content/en/templates/data-templates.md b/docs/content/en/templates/data-templates.md
index ee44fb60ebe..c9b676a7da9 100644
--- a/docs/content/en/templates/data-templates.md
+++ b/docs/content/en/templates/data-templates.md
@@ -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.
 
diff --git a/helpers/path.go b/helpers/path.go
index 00c541babe8..4c2799ac265 100644
--- a/helpers/path.go
+++ b/helpers/path.go
@@ -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 {


Test patch: