diff --git a/crates/uv-cache/src/lib.rs b/crates/uv-cache/src/lib.rs index da3ffecbcce4..0725c674d54e 100644 --- a/crates/uv-cache/src/lib.rs +++ b/crates/uv-cache/src/lib.rs @@ -236,11 +236,26 @@ impl Cache { // 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(gitignore_path) + { + 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.) + let git_path = root.join(".git"); + let _ = fs::OpenOptions::new() + .create(true) + .write(true) + .open(git_path); + fs::canonicalize(root) }