From f49b038a3f3d26d0d8cbf2b0ed274de15a8113a3 Mon Sep 17 00:00:00 2001
From: Eric Huss <eric@huss.org>
Date: Wed, 6 Sep 2023 20:15:24 -0700
Subject: [PATCH] Error out if `cargo clean --doc` is mixed with `-p`.

Currently the `-p` is ignored. This should help with any confusion
about the interaction of different flags.
https://github.com/rust-lang/cargo/issues/8790 is tracking to fix this.
---
 src/cargo/ops/cargo_clean.rs | 11 ++++++++++-
 tests/testsuite/clean.rs     | 10 ++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/src/cargo/ops/cargo_clean.rs b/src/cargo/ops/cargo_clean.rs
index b9b33690e45..d28f759d707 100644
--- a/src/cargo/ops/cargo_clean.rs
+++ b/src/cargo/ops/cargo_clean.rs
@@ -7,7 +7,7 @@ use crate::util::errors::CargoResult;
 use crate::util::interning::InternedString;
 use crate::util::{Config, Progress, ProgressStyle};
 
-use anyhow::Context as _;
+use anyhow::{bail, Context as _};
 use cargo_util::paths;
 use std::fs;
 use std::path::Path;
@@ -33,6 +33,15 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
 
     // If the doc option is set, we just want to delete the doc directory.
     if opts.doc {
+        if !opts.spec.is_empty() {
+            // FIXME: https://github.com/rust-lang/cargo/issues/8790
+            // This should support the ability to clean specific packages
+            // within the doc directory. It's a little tricky since it
+            // needs to find all documentable targets, but also consider
+            // the fact that target names might overlap with dependency
+            // names and such.
+            bail!("--doc cannot be used with -p");
+        }
         target_dir = target_dir.join("doc");
         return clean_entire_folder(&target_dir.into_path_unlocked(), config);
     }
diff --git a/tests/testsuite/clean.rs b/tests/testsuite/clean.rs
index e0885fd269e..65f6cf47616 100644
--- a/tests/testsuite/clean.rs
+++ b/tests/testsuite/clean.rs
@@ -673,3 +673,13 @@ fn clean_spec_reserved() {
         )
         .run();
 }
+
+#[cargo_test]
+fn doc_with_package_selection() {
+    // --doc with -p
+    let p = project().file("src/lib.rs", "").build();
+    p.cargo("clean --doc -p foo")
+        .with_status(101)
+        .with_stderr("error: --doc cannot be used with -p")
+        .run();
+}