cargo clean: Add target directory validation#16712
cargo clean: Add target directory validation#16712TanmayArya-1p wants to merge 2 commits intorust-lang:masterfrom
Conversation
|
I also noticed that if a file path is passed via |
|
r? @weihanglo rustbot has assigned @weihanglo. Use Why was this reviewer chosen?The reviewer was selected based on:
|
src/cargo/ops/cargo_clean.rs
Outdated
| let mut clean_ctx = CleanContext::new(gctx); | ||
| clean_ctx.dry_run = opts.dry_run; | ||
|
|
||
| // TODO: validate if target_dir is not a file? |
There was a problem hiding this comment.
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.
src/cargo/ops/cargo_clean.rs
Outdated
| // 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\ |
There was a problem hiding this comment.
Why using alternate display here?
There was a problem hiding this comment.
no specific reason, just a habit from another project. ill remove it since it shouldn't matter here
94c1489 to
94a4d66
Compare
|
thanks for the review :) 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? |
Feel free to rebase onto master! |
94a4d66 to
c6cdaa1
Compare
|
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. |
src/cargo/ops/cargo_clean.rs
Outdated
| 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() { |
There was a problem hiding this comment.
Do we want to warn for this case? Like the "/path/to/file does not appear to be a valid Cargo target directory"?
There was a problem hiding this comment.
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.
src/cargo/ops/cargo_clean.rs
Outdated
| // 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\ |
c6cdaa1 to
f72fbd1
Compare
f72fbd1 to
23c225b
Compare
|
Added a check(and test) to make sure |
What does this PR try to resolve?
Fixes #9192
Implements the checks mentioned in this comment
To summarise, when
cargo cleanis run with a specified target directory:--target-dir: check if a validCACHEDIR.TAGexists in the target directory and hard error otherwise.CACHEDIR.TAGTests
I've added 3 sets of unit tests for:
--target-diris used explicitlyCARGO_TARGET_DIRenv variableLet me know if there is a case I've missed or if i need to merge multiple tests into a single one.