Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/ops/cargo_clean.rs

@weihanglo weihanglo Aug 6, 2026 •

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.

Please follow the commit structure here in

where you should have two commits: test commit goes first as a repro, then fix commit.

View changes since the review

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.

I don't want to spend my time on dealing with fully LLM automated PR because there is zero chance the agent behind it will become a longer term contributor. So, if you next push do the fixup wrong, I'll just close it.

(Note that while there is not yet a formal LLM policy in rust-lang/cargo, you should https://blog.rust-lang.org/inside-rust/2026/08/05/rust-langrust-is-adopting-an-llm-policy/ which is a pretty good one.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reworked this into two passing commits: cd7600d52 captures the existing explicit/configured target behavior, and 5443eb202 applies the fix and updates the expectations. I verified each commit with cargo test --test testsuite -- clean_doc_target (2 passed). The final head also passes cargo fmt --all --check, cargo clippy --workspace --all-targets --no-deps --keep-going, cargo test -p cargo --test testsuite -- clean:: --skip symlink (32 passed), and cargo test -p cargo --test testsuite -- clean_legacy_layout:: --skip symlink (38 passed).

Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,17 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
bail!("--doc cannot be used with -p");
}
// If the doc option is set, we just want to delete the doc directory.
target_dir = target_dir.join("doc");
clean_ctx.remove_paths(&[target_dir.into_path_unlocked()])?;
let doc_dirs = CompileKind::from_requested_targets(gctx, &opts.targets)?
.into_iter()
.map(|kind| {
let target_dir = match kind {
CompileKind::Host => target_dir.clone(),
CompileKind::Target(target) => target_dir.join(target.short_name()),
};
target_dir.join("doc").into_path_unlocked()
})
.collect::<Vec<_>>();
clean_ctx.remove_paths(&doc_dirs)?;
} else {
let profiles = Profiles::new(&ws, opts.requested_profile)?;

Expand Down
48 changes: 48 additions & 0 deletions tests/testsuite/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,54 @@ fn clean_doc() {
assert!(p.build_dir().is_dir());
}

#[cargo_test]
fn clean_doc_target() {
let target = rustc_host();
let p = project()
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
.file("src/lib.rs", "")
.build();
let doc_path = p.build_dir().join(target).join("doc");

p.cargo("doc --target")
.arg(target)
.arg("-Zbuild-dir-new-layout")
.masquerade_as_nightly_cargo(&["new build-dir layout"])
.run();
assert!(doc_path.is_dir());

p.cargo("clean --doc --target")
.arg(target)
.with_stderr_data(str![[r#"
[REMOVED] [FILE_NUM] files, [FILE_SIZE]B total

"#]])
.arg("-Zbuild-dir-new-layout")
.masquerade_as_nightly_cargo(&["new build-dir layout"])
.run();
assert!(!doc_path.is_dir());

p.change_file(
".cargo/config.toml",
&format!("[build]\ntarget = \"{target}\""),
);
p.cargo("doc")
.arg("-Zbuild-dir-new-layout")
.masquerade_as_nightly_cargo(&["new build-dir layout"])
.run();
assert!(doc_path.is_dir());

p.cargo("clean --doc")
.with_stderr_data(str![[r#"
[REMOVED] [FILE_NUM] files, [FILE_SIZE]B total

"#]])
.arg("-Zbuild-dir-new-layout")
.masquerade_as_nightly_cargo(&["new build-dir layout"])
.run();
assert!(!doc_path.is_dir());
}

#[cargo_test]
fn build_script() {
let p = project()
Expand Down
44 changes: 44 additions & 0 deletions tests/testsuite/clean_legacy_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,50 @@ fn clean_doc() {
assert!(p.build_dir().is_dir());
}

#[cargo_test]
fn clean_doc_target() {
let target = rustc_host();
let p = project()
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
.file("src/lib.rs", "")
.build();
let doc_path = p.build_dir().join(target).join("doc");

p.cargo("doc --target")
.arg(target)
.env("__CARGO_TEMPORARY_BUILD_DIR_NEW_LAYOUT_OPT_OUT", "1")
.run();
assert!(doc_path.is_dir());

p.cargo("clean --doc --target")
.arg(target)
.with_stderr_data(str![[r#"
[REMOVED] [FILE_NUM] files, [FILE_SIZE]B total

"#]])
.env("__CARGO_TEMPORARY_BUILD_DIR_NEW_LAYOUT_OPT_OUT", "1")
.run();
assert!(!doc_path.is_dir());

p.change_file(
".cargo/config.toml",
&format!("[build]\ntarget = \"{target}\""),
);
p.cargo("doc")
.env("__CARGO_TEMPORARY_BUILD_DIR_NEW_LAYOUT_OPT_OUT", "1")
.run();
assert!(doc_path.is_dir());

p.cargo("clean --doc")
.with_stderr_data(str![[r#"
[REMOVED] [FILE_NUM] files, [FILE_SIZE]B total

"#]])
.env("__CARGO_TEMPORARY_BUILD_DIR_NEW_LAYOUT_OPT_OUT", "1")
.run();
assert!(!doc_path.is_dir());
}

#[cargo_test]
fn build_script() {
let p = project()
Expand Down