diff --git a/crates/uv-cache/src/lib.rs b/crates/uv-cache/src/lib.rs index da3ffecbc..5c8a187ea 100644 --- a/crates/uv-cache/src/lib.rs +++ b/crates/uv-cache/src/lib.rs @@ -235,12 +235,25 @@ impl Cache { cachedir::ensure_tag(&root)?; // Add the .gitignore. - let gitignore_path = root.join(".gitignore"); - if !gitignore_path.exists() { - let mut file = fs::File::create(gitignore_path)?; - file.write_all(b"*")?; + match fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(root.join(".gitignore")) + { + Ok(mut file) => file.write_all(b"*")?, + Err(err) if err.kind() == io::ErrorKind::AlreadyExists => (), + Err(err) => return Err(err), } + // Add a phony .git, if it doesn't exist, to ensure that the cache isn't considered to be + // part of a Git repository. (Some packages will include Git metadata (like a hash) in the + // built version if they're in a Git repository, but the cache should be viewed as an + // isolated store.) + fs::OpenOptions::new() + .create(true) + .write(true) + .open(root.join(".git"))?; + fs::canonicalize(root) }