Skip to content
Merged
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
2 changes: 2 additions & 0 deletions tests/testsuite/build_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,13 +525,15 @@ fn cargo_package_should_build_in_build_dir_and_output_to_target_dir() {
[ROOT]/foo/build-dir/package/foo-0.0.1/Cargo.toml.orig
[ROOT]/foo/build-dir/package/foo-0.0.1/src/main.rs
[ROOT]/foo/build-dir/package/foo-0.0.1.crate
[ROOT]/foo/build-dir/CACHEDIR.TAG

"#]]);

p.root()
.join("target-dir")
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/package/foo-0.0.1.crate
[ROOT]/foo/target-dir/CACHEDIR.TAG

"#]]);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/testsuite/build_dir_legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,13 +494,15 @@ fn cargo_package_should_build_in_build_dir_and_output_to_target_dir() {
[ROOT]/foo/build-dir/package/foo-0.0.1/Cargo.toml.orig
[ROOT]/foo/build-dir/package/foo-0.0.1/src/main.rs
[ROOT]/foo/build-dir/package/foo-0.0.1.crate
[ROOT]/foo/build-dir/CACHEDIR.TAG

"#]]);

p.root()
.join("target-dir")
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/package/foo-0.0.1.crate
[ROOT]/foo/target-dir/CACHEDIR.TAG

"#]]);
}
Expand Down
28 changes: 28 additions & 0 deletions tests/testsuite/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7804,3 +7804,31 @@ fn publish_to_alt_registry_warns() {
"#]])
.run();
}

#[cargo_test]
fn package_dir_not_excluded_from_backups() {
// This test documents the current behavior where target directory is NOT excluded from backups.
// After the fix, this test will be updated to verify that CACHEDIR.TAG exists.
let p = project().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"
);
}