Skip to content

Conversation

@SepehrGanji
Copy link
Contributor

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.

@rustbot rustbot added A-filesystem Area: issues with filesystems Command-package S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 17, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 17, 2025

r? @epage

rustbot has assigned @epage.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@epage
Copy link
Contributor

epage commented Nov 17, 2025

Should we have automated tests for this?

Note that our contrib guide normally recommends creating a passing test in a commit before the fix, showing the old behavior. Then the commit with the fix updates the test so it still passes, showing the new behavior. Then the diff will show how the behavior changed.

@epage
Copy link
Contributor

epage commented Nov 18, 2025

Looks like this needs cargo fmt run. As a warning, force pushes don't always generate notifications, so be sure to post here when you do it so we know it is ready to merge.

@SepehrGanji
Copy link
Contributor Author

Looks like this needs cargo fmt run. As a warning, force pushes don't always generate notifications, so be sure to post here when you do it so we know it is ready to merge.

Thanks for all your help! I have did the cargo fmt, could you please take a look?

Copy link
Member

@weihanglo weihanglo left a comment

Choose a reason for hiding this comment

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

// 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/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", "")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the tip! Done.

@weihanglo
Copy link
Member

@rustbot author

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 22, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 22, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. label Nov 22, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 23, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

.file("src/lib.rs", "")
.build();

p.cargo("package --allow-dirty -v")
Copy link
Member

Choose a reason for hiding this comment

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

We probably don't need -v here, since we just want to check cachedir tag file existing

You might need to change that , regenerate snapshots, and cargo fmt

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@SepehrGanji SepehrGanji marked this pull request as draft November 23, 2025 01:41
Add a failing test that demonstrates the current buggy behavior where
`cargo package` does not create a CACHEDIR.TAG file in the target
directory. This causes backup tools to unnecessarily include temporary
packaging artifacts.

The test currently passes by asserting that CACHEDIR.TAG does NOT
exist (the buggy behavior). The next commit will implement the fix
and update this test to verify correct behavior.

Relates to rust-lang#16238
Create CACHEDIR.TAG files in build directories during `cargo package`
to exclude them from system backups. This ensures that temporary
packaging artifacts in target/package are not unnecessarily included
by backup tools like Time Machine on macOS.

The fix calls `create_dir_all_excluded_from_backups_atomic` when
creating the build directory and target directory for packaging,
which automatically creates the CACHEDIR.TAG file.

Update existing tests to verify that CACHEDIR.TAG files are now
created in the expected locations.

Fixes rust-lang#16238
@SepehrGanji SepehrGanji marked this pull request as ready for review November 23, 2025 02:02
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 23, 2025
@SepehrGanji
Copy link
Contributor Author

@rustbot ready

Copy link
Member

@weihanglo weihanglo left a comment

Choose a reason for hiding this comment

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

@weihanglo weihanglo added this pull request to the merge queue Nov 23, 2025
Merged via the queue into rust-lang:master with commit 8e43074 Nov 23, 2025
26 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-filesystem Area: issues with filesystems Command-package S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CACHEDIR.TAG not created for target directory with cargo package or cargo publish

4 participants