Skip to content

Commit d77851d

Browse files
committed
fix(package): exclude target dir from backups
Ensure that the target directory is excluded from backups when running `cargo package` by creating a CACHEDIR.TAG file. According to the CACHEDIR.TAG specification, a tag file in a parent directory excludes all subdirectories, so tagging the target directory protects the entire tree including the package/ subdirectory and all build artifacts. This prevents temporary packaging artifacts from being included in Time Machine and other backup systems that respect CACHEDIR.TAG. The fix applies to both build_dir (which typically equals target_dir) during package creation, and to target_dir when uplifting artifacts in workspaces where they differ. Updates the test from the previous commit to verify that target/CACHEDIR.TAG now exists after running `cargo package`. Fixes #16238
1 parent 10cacee commit d77851d

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/cargo/ops/cargo_package/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ fn create_package(
161161
}
162162

163163
let filename = pkg.package_id().tarball_name();
164-
let dir = ws.build_dir().join("package");
164+
let build_dir = ws.build_dir();
165+
paths::create_dir_all_excluded_from_backups_atomic(build_dir.as_path_unlocked())?;
166+
let dir = build_dir.join("package");
165167
let mut dst = {
166168
let tmp = format!(".{}", filename);
167169
dir.open_rw_exclusive_create(&tmp, gctx, "package scratch space")?
@@ -225,6 +227,7 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Vec<Fi
225227
result.extend(packaged.into_iter().map(|(_, _, src)| src));
226228
} else {
227229
// Uplifting artifacts
230+
paths::create_dir_all_excluded_from_backups_atomic(target_dir.as_path_unlocked())?;
228231
let artifact_dir = target_dir.join("package");
229232
for (pkg, _, src) in packaged {
230233
let filename = pkg.package_id().tarball_name();

tests/testsuite/package.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7810,8 +7810,7 @@ fn publish_to_alt_registry_warns() {
78107810

78117811
#[cargo_test]
78127812
fn package_dir_not_excluded_from_backups() {
7813-
// This test documents the current behavior where target directory is NOT excluded from backups.
7814-
// After the fix, this test will be updated to verify that CACHEDIR.TAG exists.
7813+
// Verify that target directory is excluded from backups by checking for CACHEDIR.TAG
78157814
let p = project()
78167815
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
78177816
.build();
@@ -7830,10 +7829,10 @@ fn package_dir_not_excluded_from_backups() {
78307829
"#]])
78317830
.run();
78327831

7833-
// Currently, CACHEDIR.TAG does NOT exist in target directory
7832+
// Verify CACHEDIR.TAG exists in target (which excludes target/ and all subdirectories)
78347833
let cachedir_tag = p.root().join("target/CACHEDIR.TAG");
78357834
assert!(
7836-
!cachedir_tag.exists(),
7837-
"CACHEDIR.TAG should not exist (documenting current buggy behavior)"
7835+
cachedir_tag.exists(),
7836+
"CACHEDIR.TAG should exist in target directory to exclude it from backups"
78387837
);
78397838
}

0 commit comments

Comments
 (0)