Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/cargo/ops/cargo_package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ fn create_package(
}

let filename = pkg.package_id().tarball_name();
let dir = ws.build_dir().join("package");
let build_dir = ws.build_dir();
paths::create_dir_all_excluded_from_backups_atomic(build_dir.as_path_unlocked())?;
let dir = build_dir.join("package");
let mut dst = {
let tmp = format!(".{}", filename);
dir.open_rw_exclusive_create(&tmp, gctx, "package scratch space")?
Expand Down Expand Up @@ -225,6 +227,7 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Vec<Fi
result.extend(packaged.into_iter().map(|(_, _, src)| src));
} else {
// Uplifting artifacts
paths::create_dir_all_excluded_from_backups_atomic(target_dir.as_path_unlocked())?;
let artifact_dir = target_dir.join("package");
for (pkg, _, src) in packaged {
let filename = pkg.package_id().tarball_name();
Expand Down
29 changes: 29 additions & 0 deletions tests/testsuite/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7807,3 +7807,32 @@ fn publish_to_alt_registry_warns() {
"#]])
.run();
}

#[cargo_test]
fn package_dir_not_excluded_from_backups() {
// Verify that target directory is excluded from backups by checking for CACHEDIR.TAG
let p = project()
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: for faster test execution (avoid to binary linking)

Suggested change
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
.file("src/lib.rs", "")

.build();

p.cargo("package --allow-dirty")
.with_stderr_data(str![[r#"
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository
|
= [NOTE] see https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
[VERIFYING] foo v0.0.1 ([ROOT]/foo)
[COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();

// Verify CACHEDIR.TAG exists in target (which excludes target/ and all subdirectories)
let cachedir_tag = p.root().join("target/CACHEDIR.TAG");
assert!(
cachedir_tag.exists(),
"CACHEDIR.TAG should exist in target directory to exclude it from backups"
);
}
Loading