Skip to content

cargo clean --target-dir should check if the directory looks like a Cargo target directory before deleting it #9192

@joshtriplett

Description

@joshtriplett

If you're in a Rust project directory (somewhere with a valid Cargo.toml), calling cargo clean --target-dir /some/path will delete everything in /some/path, whether it's a Cargo target directory or not.

Reports like #8866 and https://www.reddit.com/r/rust/comments/lo413x/novice_mistake_deleted_all_my_hobby_projectstests/ suggest that this can lead to problems and data loss.

I think it would make sense to add some checks to cargo clean --target-dir, to make sure it doesn't unintentionally remove more than the user would like it to.

A few ideas:

  • We could look for some distinctive flag files in a target directory, and refuse to remove it if we don't find them.
  • We could create a flag file and look for that. (This may cause transition issues, though, if people upgrade and then clean before building with the new cargo.)
  • We could only remove subdirectories we know about, and if that doesn't leave the target directory empty, avoid removing everything else.

Metadata

Metadata

Assignees

Labels

C-bugCategory: bugCommand-cleanS-acceptedStatus: Issue or feature is accepted, and has a team member available to help mentor or review

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions