Migrate to XDG and Linux strategy for macOS directories (#5806)

This PR moves us to the Linux strategy for our global directories on
macOS. We both feel on the team _and_ have received feedback (in Issues
and Polls) that the `Application Support` directories are more intended
for GUIs, and CLI tools are correct to respect the XDG variables and use
the same directory paths on Linux and macOS.

Namely, we now use:

- `/Users/crmarsh/.local/share/uv/tools` (for tools)
- `/Users/crmarsh/.local/share/uv/python` (for Pythons)
- `/Users/crmarsh/.cache/uv` (for the cache)

The strategy is such that if the `/Users/crmarsh/Library/Application
Support/uv` already exists, we keep using it -- same goes for
`/Users/crmarsh/Library/Caches/uv`, so **it's entirely backwards
compatible**.

If you want to force a migration to the new schema, you can run:

- `uv cache clean`
- `uv tool uninstall --all`
- `uv python uninstall --all`

Which will clean up the macOS-specific directories, paving the way for
the above paths. In other words, once you run those commands, subsequent
`uv` operations will automatically use the `~/.cache` and `~/.local`
variants.

Closes https://github.com/astral-sh/uv/issues/4411.

---------

Co-authored-by: Zanie Blue <contact@zanie.dev>
This commit is contained in:
Charlie Marsh
2024-08-19 15:33:21 -04:00
committed by Zanie Blue
parent 04e3e7ce65
commit e11bbb539a
9 changed files with 136 additions and 17 deletions
+18 -7
View File
@@ -1,10 +1,10 @@
use std::io;
use std::path::PathBuf;
use crate::Cache;
use clap::Parser;
use directories::ProjectDirs;
use crate::Cache;
use etcetera::BaseStrategy;
#[derive(Parser, Debug, Clone)]
#[command(next_help_heading = "Cache options")]
@@ -40,13 +40,24 @@ impl Cache {
/// Returns an absolute cache dir.
pub fn from_settings(no_cache: bool, cache_dir: Option<PathBuf>) -> Result<Self, io::Error> {
if no_cache {
Cache::temp()
Self::temp()
} else if let Some(cache_dir) = cache_dir {
Ok(Cache::from_path(cache_dir))
} else if let Some(project_dirs) = ProjectDirs::from("", "", "uv") {
Ok(Cache::from_path(project_dirs.cache_dir()))
Ok(Self::from_path(cache_dir))
} else if let Some(cache_dir) = ProjectDirs::from("", "", "uv")
.map(|dirs| dirs.cache_dir().to_path_buf())
.filter(|dir| dir.exists())
{
// If the user has an existing directory at (e.g.) `/Users/user/Library/Caches/uv`,
// respect it for backwards compatibility. Otherwise, prefer the XDG strategy, even on
// macOS.
Ok(Self::from_path(cache_dir))
} else if let Some(cache_dir) = etcetera::base_strategy::choose_base_strategy()
.ok()
.map(|dirs| dirs.cache_dir().join("uv"))
{
Ok(Self::from_path(cache_dir))
} else {
Ok(Cache::from_path(".uv_cache"))
Ok(Self::from_path(".uv_cache"))
}
}
}