Skip to content

Commit 4d928f4

Browse files
committed
fix(package): exclude target/package from backups
### What does this PR try to resolve? The target directory is not excluded from backups when created with `cargo package` or `cargo publish`. This causes the `target/package` directory to be included in Time Machine backups on macOS and other backup systems that respect CACHEDIR.TAG files. The issue occurs because when `Filesystem::open()` creates parent directories in `src/cargo/util/flock.rs:336`, it uses `paths::create_dir_all()` instead of the backup-excluding version, bypassing the normal target directory creation logic. This commit adds a new `create_dir_with_backup_exclusion()` method to `Filesystem` and uses it to ensure the `target/package` directory is properly excluded from backups before creating files in it. Fixes #16238 ### How to test and review this PR? Run the following: cargo new foo cd foo cargo package --allow-dirty ls -al target/package/ Verify that `CACHEDIR.TAG` now exists in `target/package/`. All existing package tests pass.
1 parent b5354b5 commit 4d928f4

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/cargo/ops/cargo_package/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ fn create_package(
162162

163163
let filename = pkg.package_id().tarball_name();
164164
let dir = ws.build_dir().join("package");
165+
dir.create_dir_with_backup_exclusion()?;
165166
let mut dst = {
166167
let tmp = format!(".{}", filename);
167168
dir.open_rw_exclusive_create(&tmp, gctx, "package scratch space")?
@@ -226,6 +227,7 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Vec<Fi
226227
} else {
227228
// Uplifting artifacts
228229
let artifact_dir = target_dir.join("package");
230+
artifact_dir.create_dir_with_backup_exclusion()?;
229231
for (pkg, _, src) in packaged {
230232
let filename = pkg.package_id().tarball_name();
231233
let dst =
@@ -1128,7 +1130,7 @@ struct TmpRegistry<'a> {
11281130

11291131
impl<'a> TmpRegistry<'a> {
11301132
fn new(gctx: &'a GlobalContext, root: Filesystem, upstream: SourceId) -> CargoResult<Self> {
1131-
root.create_dir()?;
1133+
root.create_dir_with_backup_exclusion()?;
11321134
let _lock = root.open_rw_exclusive_create(".cargo-lock", gctx, "temporary registry")?;
11331135
let slf = Self {
11341136
gctx,

src/cargo/util/flock.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,17 @@ impl Filesystem {
211211
paths::create_dir_all(&self.root)
212212
}
213213

214+
/// Creates the directory pointed to by this filesystem, excluding it from backups.
215+
///
216+
/// This should be used for target directories and other build artifacts that
217+
/// should not be included in backups or Time Machine on macOS.
218+
///
219+
/// Handles errors where other Cargo processes are also attempting to
220+
/// concurrently create this directory.
221+
pub fn create_dir_with_backup_exclusion(&self) -> CargoResult<()> {
222+
paths::create_dir_all_excluded_from_backups_atomic(&self.root)
223+
}
224+
214225
/// Returns an adaptor that can be used to print the path of this
215226
/// filesystem.
216227
pub fn display(&self) -> Display<'_> {

0 commit comments

Comments
 (0)