Separate cache construction from initialization (#3607)

## Summary

Ensures that we only initialize the cache for commands that require it.

Closes https://github.com/astral-sh/uv/issues/3539.
This commit is contained in:
Charlie Marsh 2024-05-15 12:29:39 -04:00 committed by GitHub
parent 647f38be31
commit 55aedda379
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 78 additions and 49 deletions

View file

@ -51,13 +51,6 @@ impl Cache {
impl TryFrom<CacheArgs> for Cache {
type Error = io::Error;
/// Prefer, in order:
/// 1. A temporary cache directory, if the user requested `--no-cache`.
/// 2. The specific cache directory specified by the user via `--cache-dir` or `UV_CACHE_DIR`.
/// 3. The system-appropriate cache directory.
/// 4. A `.uv_cache` directory in the current working directory.
///
/// Returns an absolute cache dir.
fn try_from(value: CacheArgs) -> Result<Self, Self::Error> {
Cache::from_settings(value.no_cache, value.cache_dir)
}

View file

@ -128,7 +128,7 @@ impl Cache {
/// A persistent cache directory at `root`.
pub fn from_path(root: impl Into<PathBuf>) -> Result<Self, io::Error> {
Ok(Self {
root: Self::init(root)?,
root: root.into(),
refresh: Refresh::None,
_temp_dir_drop: None,
})
@ -138,7 +138,7 @@ impl Cache {
pub fn temp() -> Result<Self, io::Error> {
let temp_dir = tempdir()?;
Ok(Self {
root: Self::init(temp_dir.path())?,
root: temp_dir.path().to_path_buf(),
refresh: Refresh::None,
_temp_dir_drop: Some(Arc::new(temp_dir)),
})
@ -243,15 +243,15 @@ impl Cache {
Ok(id)
}
/// Initialize a directory for use as a cache.
fn init(root: impl Into<PathBuf>) -> Result<PathBuf, io::Error> {
let root = root.into();
/// Initialize the cache.
pub fn init(self) -> Result<Self, io::Error> {
let root = &self.root;
// Create the cache directory, if it doesn't exist.
fs::create_dir_all(&root)?;
fs::create_dir_all(root)?;
// Add the CACHEDIR.TAG.
cachedir::ensure_tag(&root)?;
cachedir::ensure_tag(root)?;
// Add the .gitignore.
match fs::OpenOptions::new()
@ -289,7 +289,10 @@ impl Cache {
.write(true)
.open(root.join(CacheBucket::BuiltWheels.to_str()).join(".git"))?;
fs::canonicalize(root)
Ok(Self {
root: fs::canonicalize(root)?,
..self
})
}
/// Clear the cache, removing all entries.