Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use os.UserCacheDir instead of /tmp is HUGO_CACHEDIR if not set #11286

Closed
bep opened this issue Jul 26, 2023 · 8 comments · Fixed by #11292
Closed

Use os.UserCacheDir instead of /tmp is HUGO_CACHEDIR if not set #11286

bep opened this issue Jul 26, 2023 · 8 comments · Fixed by #11292
Assignees
Milestone

Comments

@bep
Copy link
Member

bep commented Jul 26, 2023

No description provided.

@bep bep added this to the v0.116.0 milestone Jul 26, 2023
@bep
Copy link
Member Author

bep commented Jul 26, 2023

@jmooring do you agree with this change? I suspect most of the Hugo Modules related issues are related to the OS messing with the /tmp dir.

@jmooring
Copy link
Member

I'm not sure it buys us anything on Linux or Windows. For Linux, os.TempDir looks to $TMPDIR (which is usually not set), falling back to /tmp. On Windows, we're already using the personal temp directory.

If it helps on macOS we should definitely do it. Most of the cache volatility problems that I have seen are on macOS.

Finally, we need to make sure we don't inadvertently revert #11082.

@bep
Copy link
Member Author

bep commented Jul 26, 2023

Not os.TempDir ... os.UserCacheDir:

https://pkg.go.dev/os#UserCacheDir

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.

But we will fall back to the current logic if the above fails.

And yes, this is probably a MacOS issue, but that's not an uncommon Hugo workstation.

@jmooring
Copy link
Member

Not os.TempDir ... os.UserCacheDir

Well, if you used ALL CAPS, highlighted in yellow, and added some emoji, this issue's title would have been a bit clearer.

Sounds good to me.

@jmooring
Copy link
Member

OK, there's a Linux issue with the Snap package:

$ /snap/bin/hugo
Error: failed to load config: failed to create cache dir: mkdir /home/jmooring/.cache/hugo/: permission denied

Strictly confined Snaps, by default, do not have access to "personal files" ($HOME/.something). Let me know when you plan to do this, and I will submit a request to access $HOME/.cache/hugo/* (not $HOME/.cache/hugo_$USER/*). It takes about a week to get approval.

@bep
Copy link
Member Author

bep commented Jul 26, 2023

Strictly confined Snaps, by default, do not have access to "personal files" ($HOME/.something). L

So, I will try to do a stat/mkdir or something -- and if that fails, fall back to the "old logic", so what you describe should at least not fail on Snap.

@jmooring
Copy link
Member

I'm going to request Snap access for $HOME/.cache/hugo/ anyway. This will make the cache directory visible and easy to manually clear. Currently you have to do something like:

sudo rm -rf /tmp/snap-private-tmp/snap.hugo/tmp/hugo_cache_jmooring

jmooring added a commit to jmooring/hugo that referenced this issue Jul 27, 2023
@bep bep self-assigned this Jul 27, 2023
bep added a commit to bep/hugo that referenced this issue Jul 27, 2023
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 gohugoio#11286
Fixes gohugoio#11291
bep added a commit to bep/hugo that referenced this issue Jul 27, 2023
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 gohugoio#11286
Fixes gohugoio#11291
bep added a commit to bep/hugo that referenced this issue Jul 27, 2023
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 gohugoio#11286
Fixes gohugoio#11291
bep added a commit to bep/hugo that referenced this issue Jul 27, 2023
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 gohugoio#11286
Fixes gohugoio#11291
bep added a commit that referenced this issue Jul 27, 2023
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
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants