Skip to content

cargo clean: Add target directory validation#16712

Open
TanmayArya-1p wants to merge 2 commits intorust-lang:masterfrom
TanmayArya-1p:clean-target-validation
Open

cargo clean: Add target directory validation#16712
TanmayArya-1p wants to merge 2 commits intorust-lang:masterfrom
TanmayArya-1p:clean-target-validation

Conversation

@TanmayArya-1p
Copy link
Contributor

What does this PR try to resolve?

Fixes #9192

Implements the checks mentioned in this comment

To summarise, when cargo clean is run with a specified target directory:

  • If a target directory is explicitly passed via --target-dir: check if a valid CACHEDIR.TAG exists in the target directory and hard error otherwise.
  • In other cases where target directory is specified(via env vars or build config): emit a future incompat warning if the target directory does not contain a valid CACHEDIR.TAG

Tests

I've added 3 sets of unit tests for:

  • When --target-dir is used explicitly
  • When target directory is specified via the build config
  • When target directory is specified via the CARGO_TARGET_DIR env variable

Let me know if there is a case I've missed or if i need to merge multiple tests into a single one.

@rustbot rustbot added A-cli Area: Command-line interface, option parsing, etc. Command-clean labels Mar 5, 2026
@TanmayArya-1p
Copy link
Contributor Author

I also noticed that if a file path is passed via --target-dir, it gets deleted without validation. I was wondering if we require checks for this as well?

@TanmayArya-1p TanmayArya-1p marked this pull request as ready for review March 5, 2026 23:27
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 5, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 5, 2026

r? @weihanglo

rustbot has assigned @weihanglo.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ehuss, @epage, @weihanglo
  • @ehuss, @epage, @weihanglo expanded to ehuss, epage, weihanglo
  • Random selection from ehuss, epage, weihanglo

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.

Some nitpicks but general good. Thank you!

View changes since this review

let mut clean_ctx = CleanContext::new(gctx);
clean_ctx.dry_run = opts.dry_run;

// TODO: validate if target_dir is not a file?
Copy link
Member

Choose a reason for hiding this comment

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

I also noticed that if a file path is passed via --target-dir, it gets deleted without validation. I was wondering if we require checks for this as well?

That would be a behavior change. Currently you can do this

$ touch myfile
$ cargo clean --target-dir myfile
     Removed 1 file

Though this is rare and personally I considered it unsupported use case. We probably want to address it as well.

// if target_dir was passed explicitly via --target-dir, then hard error if validation fails
validate_target_dir_tag(target_dir.as_path_unlocked()).map_err(|err| {
anyhow!(
"cannot clean `{}`: {err:#}\n\
Copy link
Member

Choose a reason for hiding this comment

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

Why using alternate display here?

Copy link
Member

Choose a reason for hiding this comment

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

Any specific reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no specific reason, just a habit from another project. ill remove it since it shouldn't matter here

@TanmayArya-1p TanmayArya-1p force-pushed the clean-target-validation branch from 94c1489 to 94a4d66 Compare March 6, 2026 11:15
@TanmayArya-1p
Copy link
Contributor Author

TanmayArya-1p commented Mar 6, 2026

thanks for the review :)
I made a few changes and fixed the things you pointed out, let me know if theres anything else that needs to be changed.

Also I noticed that the CI keeps failing in something totally unrelated. I see it's happening in other PRs as well. Any idea why this is happening?
Edit: Nevermind, I see #16714 addresses this

@weihanglo
Copy link
Member

weihanglo commented Mar 6, 2026

Also I noticed that the CI keeps failing in something totally unrelated. I see it's happening in other PRs as well. Any idea why this is happening?
Edit: Nevermind, I see #16714 addresses this

Feel free to rebase onto master!
Edit: actually you need to do that in order to make CI green and in a mergeable state

@TanmayArya-1p TanmayArya-1p force-pushed the clean-target-validation branch from 94a4d66 to c6cdaa1 Compare March 6, 2026 17:12
@rustbot
Copy link
Collaborator

rustbot commented Mar 6, 2026

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.

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.

While this has been discussed within the team, this is a behavior change so I may start an FCP. Just FYI

View changes since this review

const TAG_SIGNATURE: &[u8] = b"Signature: 8a477f597d28d172789f06886806bc55";

// if the path is not a dir then don't do anything
if !target_dir_path.is_dir() {
Copy link
Member

@weihanglo weihanglo Mar 7, 2026

Choose a reason for hiding this comment

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

Do we want to warn for this case? Like the "/path/to/file does not appear to be a valid Cargo target directory"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://doc.rust-lang.org/cargo/commands/cargo-clean.html#option-cargo-clean---target-dir

--target-dir directory
Directory for all generated artifacts and intermediate files. May also be specified with the CARGO_TARGET_DIR environment variable, or the build.target-dir config value. Defaults to target in the root of the workspace.

since the docs are explicit about it being a directory, maybe we should probably hard error when target_dir points to a file. I can't imagine someones script would break if we add a check here.

Copy link
Member

Choose a reason for hiding this comment

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

That sounds good to me!

// if target_dir was passed explicitly via --target-dir, then hard error if validation fails
validate_target_dir_tag(target_dir.as_path_unlocked()).map_err(|err| {
anyhow!(
"cannot clean `{}`: {err:#}\n\
Copy link
Member

Choose a reason for hiding this comment

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

Any specific reason?

@TanmayArya-1p TanmayArya-1p force-pushed the clean-target-validation branch from c6cdaa1 to f72fbd1 Compare March 7, 2026 17:55
@TanmayArya-1p TanmayArya-1p force-pushed the clean-target-validation branch from f72fbd1 to 23c225b Compare March 7, 2026 20:33
@TanmayArya-1p
Copy link
Contributor Author

TanmayArya-1p commented Mar 7, 2026

Added a check(and test) to make sure target_dir is a directory when it is specified. Also, I wasnt happy with how the code looked so i refactored it a bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-cli Area: Command-line interface, option parsing, etc. Command-clean 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.

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

3 participants