Skip to content

Conversation

@SepehrGanji
Copy link

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.

Add a test that documents the current behavior where `cargo package`
creates the target/package directory without excluding it from backups.
The CACHEDIR.TAG file does not currently exist in the target directory,
causing backup tools to include these temporary build artifacts.

This test currently passes by asserting the buggy behavior (CACHEDIR.TAG
does not exist). The next commit will fix the issue and update the test
to verify correct behavior.
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 rust-lang#16238
@SepehrGanji
Copy link
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", "")

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-review Status: Awaiting review from the assignee but also interested parties.

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