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
Copy Markdown
Contributor

@TanmayArya-1p TanmayArya-1p commented Mar 5, 2026

View all comments

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

This comment has been minimized.

Copy link
Copy Markdown
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 2 times, most recently from f72fbd1 to 23c225b Compare March 7, 2026 20:33
@TanmayArya-1p
Copy link
Copy Markdown
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
Copy Markdown
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.

@weihanglo
Copy link
Copy Markdown
Member

weihanglo commented Mar 11, 2026

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 See cargo clean: Add target directory validation #16712 (comment)

This also adds an extra validation that if target-dir points to a file, errors out. We decided to split this out to its own PR #16765 as it is less controversial.

@rust-rfcbot
Copy link
Copy Markdown
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:

Concerns:

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
Copy Markdown
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
Copy Markdown
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.

@weihanglo
Copy link
Copy Markdown
Member

@TanmayArya-1p While this is still in FCP, we were discussing whether the target-dir-is-file check can be a separate commit, or even a separate PR. That one turns out to be less controversial within the Cargo team.

@TanmayArya-1p
Copy link
Copy Markdown
Contributor Author

cool, i'll open another pr for the file validation.

github-merge-queue bot pushed a commit that referenced this pull request Mar 20, 2026
As discussed in #16712 , this is a separate PR that adds validation to
`cargo clean` to make sure target-dir is not a file

#### Tests:

- Added unit tests for when target-dir is found to be a file.
- I also noticed that there was no unit test asserting the expected
behaviour discussed in
<#7510 (comment)>
when the target-dir is a symlink. I added a unit test that asserts that
when the target-dir is a symlink, cargo should just delete the symlink
and should not delete the content it is pointing to.
@rustbot

This comment has been minimized.

@TanmayArya-1p TanmayArya-1p force-pushed the clean-target-validation branch from 23c225b to 53f4b78 Compare March 21, 2026 00:45
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 21, 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.

@epage
Copy link
Copy Markdown
Contributor

epage commented Mar 24, 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.

The choice when we last discussed this wasn't between future-incompat OR error but do-nothing OR future-incompat.

@weihanglo
Copy link
Copy Markdown
Member

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.

The choice when we last discussed this wasn't between future-incompat OR error but do-nothing OR future-incompat.

I was re-reading #9192 (comment) and the meeting note. My understanding is that, for the env/config cases, the conclusion was not to hard-error right away. Instead the direction was to use a future-incompat warning first and then revisit based on user feedback.

I wasn't in the meeting, though I feel like without a warning there is no action we can take in the future. The drawback of the warning is no way to suppress it though.

Copy link
Copy Markdown
Member

@0xPoe 0xPoe left a comment

Choose a reason for hiding this comment

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

Do we need to consider the new build directory layout here? I am not sure if this is out of scope. However, for the new build dir layout, I believe cargo clean also deletes the build directory. Should we perform the same check for the build directory?

Even if it is out of scope, it may be better to create an issue to track it if necessary.

cc: @ranger-ross @weihanglo

View changes since this review

@ranger-ross
Copy link
Copy Markdown
Member

ranger-ross commented Mar 25, 2026

Should we perform the same check for the build directory?

I think that seems reasonable to also check the build-dir assuming target-dir != build-dir.
Note that for build-dir there is no --build-dir CLI flag, so if we matched the target-dir behavior in this PR Cargo would only warn.
Perhaps this could be added in a separate PR?

Do we need to consider the new build directory layout here?

The validation performed by validate_target_dir_tag() should be compatible for both the v1 and v2 build-dir layouts as it does not change anything outside <build-dir>/{platform-profile}/. The layout changes do not touch CACHEDIR.TAG so I don't think there should be any issues here.

@weihanglo
Copy link
Copy Markdown
Member

Note that for build-dir there is no --build-dir CLI flag, so if we matched the target-dir behavior in this PR Cargo would only warn.
Perhaps this could be added in a separate PR?

Either in this or a separate PR is fine for me, though I would suggest doing it after the FCP concludes

@rust-rfcbot rust-rfcbot added final-comment-period FCP — a period for last comments before action is taken and removed proposed-final-comment-period An FCP proposal has started, but not yet signed off. labels Mar 26, 2026
@rust-rfcbot
Copy link
Copy Markdown
Collaborator

🔔 This is now entering its final comment period, as per the review above. 🔔

@epage
Copy link
Copy Markdown
Contributor

epage commented Mar 26, 2026

@rfcbot fcp concern future-incompat

See thread starting at #16712 (comment)

@rust-rfcbot rust-rfcbot added proposed-final-comment-period An FCP proposal has started, but not yet signed off. and removed final-comment-period FCP — a period for last comments before action is taken labels Mar 26, 2026
@epage
Copy link
Copy Markdown
Contributor

epage commented Mar 26, 2026

My understanding is that, for the env/config cases, the conclusion was not to hard-error right away. Instead the direction was to use a future-incompat warning first and then revisit based on user feedback.

While there, I'm mostly going off the notes at this point and that isn't what I got from them.

From https://github.com/rust-lang/cargo-team/blob/main/meetings/sync-meeting/2025-11-11.md

ehuss: Sounds to me like detecting the use of target-dir as the CLI flag and detecting the cache dir is not there, then error out. And we can add the rest later.

From #9192 (comment)

There was some uncertainty if that should also include setting target dir via environment variables and config files

The reason for the discrepancy with how things are called from https://github.com/rust-lang/cargo-team/blob/main/meetings/sync-meeting/2025-11-11.md

epage: Would we only do this if you specify --target-dir or for all target dirs?
Josh: I'd only do it if you specify it. Because at that point it's equivalent to rm -rf

epage: There's a lot of people who go the configuration route for caching purposes, changing drives etc. I think if they'er manually configuring something they're more likely to create the directory manually.

epage: The highest risk one is the --target-dir flag -- there we can error out. With the other ones, there's still chances where people could be creating the directory manually. And our tools haven't been good about creating target dir.

@epage
Copy link
Copy Markdown
Contributor

epage commented Mar 26, 2026

Should we perform the same check for the build directory?

build-dir doesn't have a CLI flag. Anything we do for target-dir config, we should probably do for build-dir config.

@weihanglo
Copy link
Copy Markdown
Member

Yeah, as the warning is non-suppressible, we would like to have further discussion and plan around it in order not to annoy people.

@TanmayArya-1p would you mind take the future incompatible warning out to help resolve the concern?

@TanmayArya-1p
Copy link
Copy Markdown
Contributor Author

Yeah, as the warning is non-suppressible, we would like to have further discussion and plan around it in order not to annoy people.

@TanmayArya-1p would you mind take the future incompatible warning out to help resolve the concern?

Alright makes sense, sorry for misinterpreting the comment there. Since we should not do anything different in the config-target-dir case, should I leave in the tests for config-target-dir or do i remove them completely since there is no behaviour change in that case?

@weihanglo
Copy link
Copy Markdown
Member

Can add those also for capturing the current behavior.

@TanmayArya-1p TanmayArya-1p force-pushed the clean-target-validation branch 2 times, most recently from 02b0d4e to cfeeb26 Compare March 26, 2026 21:14
@TanmayArya-1p TanmayArya-1p requested a review from weihanglo March 26, 2026 22:44
Copy link
Copy Markdown
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.

Thanks for the update!


@epage the warning is removed. We defer it to future discussion. I think the concern is addressed :)

View changes since this review


"#]]).run();
"#]])
.run();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: This formatting change should be in the first commit

@TanmayArya-1p TanmayArya-1p force-pushed the clean-target-validation branch from cfeeb26 to ac8856a Compare March 27, 2026 20:34
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

Status: FCP merge

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

8 participants