From cd7600d52d25cdc01457e10918c77cd908884334 Mon Sep 17 00:00:00 2001 From: subotac <73706465+subotac@users.noreply.github.com> Date: Thu, 6 Aug 2026 07:34:00 +0300 Subject: [PATCH 1/2] test(clean): capture --doc target behavior --- tests/testsuite/clean.rs | 48 ++++++++++++++++++++++++++ tests/testsuite/clean_legacy_layout.rs | 44 +++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/tests/testsuite/clean.rs b/tests/testsuite/clean.rs index 425d09224a8..ed4f6bf4c5f 100644 --- a/tests/testsuite/clean.rs +++ b/tests/testsuite/clean.rs @@ -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] 0 files + +"#]]) + .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] 0 files + +"#]]) + .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() diff --git a/tests/testsuite/clean_legacy_layout.rs b/tests/testsuite/clean_legacy_layout.rs index ec22805e43f..434f22a6ea8 100644 --- a/tests/testsuite/clean_legacy_layout.rs +++ b/tests/testsuite/clean_legacy_layout.rs @@ -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] 0 files + +"#]]) + .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] 0 files + +"#]]) + .env("__CARGO_TEMPORARY_BUILD_DIR_NEW_LAYOUT_OPT_OUT", "1") + .run(); + assert!(doc_path.is_dir()); +} + #[cargo_test] fn build_script() { let p = project() From 5443eb202ba471aff5619f1b918c2bed9f614c98 Mon Sep 17 00:00:00 2001 From: subotac <73706465+subotac@users.noreply.github.com> Date: Thu, 6 Aug 2026 07:36:04 +0300 Subject: [PATCH 2/2] fix(clean): respect target with --doc --- src/ops/cargo_clean.rs | 13 +++++++++++-- tests/testsuite/clean.rs | 8 ++++---- tests/testsuite/clean_legacy_layout.rs | 8 ++++---- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/ops/cargo_clean.rs b/src/ops/cargo_clean.rs index 10f5ba8541a..fa3830775db 100644 --- a/src/ops/cargo_clean.rs +++ b/src/ops/cargo_clean.rs @@ -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::>(); + clean_ctx.remove_paths(&doc_dirs)?; } else { let profiles = Profiles::new(&ws, opts.requested_profile)?; diff --git a/tests/testsuite/clean.rs b/tests/testsuite/clean.rs index ed4f6bf4c5f..44537359d23 100644 --- a/tests/testsuite/clean.rs +++ b/tests/testsuite/clean.rs @@ -373,13 +373,13 @@ fn clean_doc_target() { p.cargo("clean --doc --target") .arg(target) .with_stderr_data(str![[r#" -[REMOVED] 0 files +[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()); + assert!(!doc_path.is_dir()); p.change_file( ".cargo/config.toml", @@ -393,13 +393,13 @@ fn clean_doc_target() { p.cargo("clean --doc") .with_stderr_data(str![[r#" -[REMOVED] 0 files +[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()); + assert!(!doc_path.is_dir()); } #[cargo_test] diff --git a/tests/testsuite/clean_legacy_layout.rs b/tests/testsuite/clean_legacy_layout.rs index 434f22a6ea8..e5f37e84c7f 100644 --- a/tests/testsuite/clean_legacy_layout.rs +++ b/tests/testsuite/clean_legacy_layout.rs @@ -501,12 +501,12 @@ fn clean_doc_target() { p.cargo("clean --doc --target") .arg(target) .with_stderr_data(str![[r#" -[REMOVED] 0 files +[REMOVED] [FILE_NUM] files, [FILE_SIZE]B total "#]]) .env("__CARGO_TEMPORARY_BUILD_DIR_NEW_LAYOUT_OPT_OUT", "1") .run(); - assert!(doc_path.is_dir()); + assert!(!doc_path.is_dir()); p.change_file( ".cargo/config.toml", @@ -519,12 +519,12 @@ fn clean_doc_target() { p.cargo("clean --doc") .with_stderr_data(str![[r#" -[REMOVED] 0 files +[REMOVED] [FILE_NUM] files, [FILE_SIZE]B total "#]]) .env("__CARGO_TEMPORARY_BUILD_DIR_NEW_LAYOUT_OPT_OUT", "1") .run(); - assert!(doc_path.is_dir()); + assert!(!doc_path.is_dir()); } #[cargo_test]