Skip to content

Commit

Permalink
cache: Set default cache path based on $USER
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Sircular authored and bep committed Jun 12, 2023
1 parent 254c2b3 commit 258884f
Show file tree
Hide file tree
Showing 16 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion commands/commandeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/commands/hugo.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/commands/hugo_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/commands/hugo_config_mounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/commands/hugo_mod_clean.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*"
Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/commands/hugo_mod_graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/commands/hugo_mod_init.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/commands/hugo_mod_npm_pack.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/commands/hugo_mod_tidy.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/commands/hugo_mod_vendor.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/commands/hugo_mod_verify.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/commands/hugo_new_content.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/commands/hugo_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/getting-started/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/templates/data-templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
7 changes: 6 additions & 1 deletion helpers/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 258884f

Please sign in to comment.