go-gh icon indicating copy to clipboard operation
go-gh copied to clipboard

Use UserCacheDir and UserConfigDir instead of custom implementation

Open el7cosmos opened this issue 6 months ago • 4 comments

Go have os.UserCacheDir() which added in 1.11

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) or the path in $XDG_CACHE_HOME is relative, then it will return an error.

Go also have os.UserConfigDir() which added in 1.13

UserConfigDir returns the default root directory to use for user-specific configuration data. Users should create their own application-specific subdirectory within this one and use that.

On Unix systems, it returns $XDG_CONFIG_HOME as specified by https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html if non-empty, else $HOME/.config. On Darwin, it returns $HOME/Library/Application Support. On Windows, it returns %AppData%. On Plan 9, it returns $home/lib.

If the location cannot be determined (for example, $HOME is not defined) or the path in $XDG_CONFIG_HOME is relative, then it will return an error.

It will be good to utilize this for config.CacheDir() and config.ConfigDir() instead of custom implementations.

el7cosmos avatar Jun 22 '25 04:06 el7cosmos

Will this have some functional change that is useful to you, or is it a matter of technical debt you're thinking about?

williammartin avatar Jun 22 '25 04:06 williammartin

Yes, this will mostly affects Mac users as this will have a different directory for cache and config.

  • ~/.cache will be ~/Library/Caches
  • ~/.config will be ~/Library/Application Support

Aside from storing cache and config in a more proper place, from my personal point of view, tools on Mac are split between the home directory and Library directory to store them, it will be good to have more consistency between tools.

el7cosmos avatar Jun 22 '25 05:06 el7cosmos

I think cache would be a pretty simple lift and shift since it would effectively act as a cache expiration. Config might require a little more work to move the underlying config.yml and hosts.yml or to prioritise ~/.config if the files are already there. Also, though it really is an implementation detail, a lot of people have come to expect finding the files under ~/.config, I suspect there might be quite some scripts out there expecting to find files there. Will think about it!

williammartin avatar Jun 22 '25 05:06 williammartin

The Apple Developer documentation on Library directory and app-specific files seems to coincide with Go functions behaviors, so I don't foresee problems with the new directories per se but I also don't know what Mac OS does with those directories behind the scenes.

Lastly, I think we want to continue allowing users to customize where this is if they want them.

@el7cosmos : have you customized your local workstation to use custom XDG_CONFIG_HOME and XDG_CACHE_HOME to the relevant ~/Library directories? if so, what have you experienced?

andyfeller avatar Jun 23 '25 15:06 andyfeller