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

@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

@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.

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 the T-cargo Team: Cargo label Mar 11, 2026
@weihanglo

This comment was marked as duplicate.

1 similar comment
@weihanglo
Copy link
Member

I knew we discussed this during a meeting last year, but for a behavior change it is still better to have a formal record and give members time to think about it offline. Hence

@rfcbot fcp merge cargo

This implements the target-dir validation described in #9192 (comment).

  • If --target-dir is passed, cargo clean now hard-errors unless the directory contains a valid CACHEDIR.TAG.
  • Otherwise, if the explicit target-dir comes from config, cargo clean emits a future-incompat warning if it doesn't contain a valid CACHEDIR.TAG

This also adds an extra validation that if target-dir points to a file, errors out.

@rust-rfcbot
Copy link
Collaborator

rust-rfcbot commented Mar 11, 2026

Team member @weihanglo has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period An FCP proposal has started, but not yet signed off. disposition-merge FCP with intent to merge labels Mar 11, 2026
@epage
Copy link
Contributor

epage commented Mar 11, 2026

Otherwise, if the explicit target-dir comes from config, cargo clean emits a future-incompat warning if it doesn't contain a valid CACHEDIR.TAG

From #9192 (comment)

There was some uncertainty if that should also include setting target dir via environment variables and config files. One idea is to issue a future-incompatible warning that those situations may not be allowed in the future too, and then collect any feedback if people have issues with that.

What led to deciding in the direction of the future-incompat warning?

@weihanglo
Copy link
Member

weihanglo commented Mar 11, 2026

What led to deciding in the direction of the future-incompat warning?

For myself, to be conservative. I remembered rust-analyzer may create target-dir before running any cargo command, and that there will be no CACHEDIR.TAG. But probably this doesn't matter for this case.

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 disposition-merge FCP with intent to merge proposed-final-comment-period An FCP proposal has started, but not yet signed off. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-cargo Team: Cargo

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

5 participants