Skip to content

Commit 330e66b

Browse files
osiewiczdinocosta
andcommitted
clean: Add --workspace support
Co-authored-by: Dino <dino@zed.dev>
1 parent a348443 commit 330e66b

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

src/bin/cargo/commands/clean.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use cargo::ops::CleanContext;
77
use cargo::ops::{self, CleanOptions};
88
use cargo::util::print_available_packages;
99
use clap_complete::ArgValueCandidates;
10+
use indexmap::IndexSet;
1011
use std::time::Duration;
1112

1213
pub fn cli() -> Command {
@@ -18,6 +19,10 @@ pub fn cli() -> Command {
1819
"Package to clean artifacts for",
1920
ArgValueCandidates::new(get_pkg_name_candidates),
2021
)
22+
.arg(
23+
flag("workspace", "Only clean the workspace packages")
24+
.help_heading(heading::PACKAGE_SELECTION),
25+
)
2126
.arg_release("Whether or not to clean release artifacts")
2227
.arg_profile("Clean artifacts of the specified profile")
2328
.arg_target_triple("Target triple to clean output for")
@@ -146,10 +151,14 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
146151
if args.is_present_with_zero_values("package") {
147152
print_available_packages(&ws)?;
148153
}
154+
let mut spec = IndexSet::from_iter(values(args, "package"));
149155

156+
if args.flag("workspace") {
157+
spec.extend(ws.members().map(|package| package.name().to_string()))
158+
};
150159
let opts = CleanOptions {
151160
gctx,
152-
spec: values(args, "package"),
161+
spec,
153162
targets: args.targets()?,
154163
requested_profile: args.get_profile_name("dev", ProfileChecking::Custom)?,
155164
profile_specified: args.contains_id("profile") || args.flag("release"),

src/cargo/ops/cargo_clean.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::util::interning::InternedString;
99
use crate::util::{GlobalContext, Progress, ProgressStyle};
1010
use anyhow::bail;
1111
use cargo_util::paths;
12+
use indexmap::IndexSet;
1213
use std::collections::{HashMap, HashSet};
1314
use std::fs;
1415
use std::path::{Path, PathBuf};
@@ -17,7 +18,7 @@ use std::rc::Rc;
1718
pub struct CleanOptions<'gctx> {
1819
pub gctx: &'gctx GlobalContext,
1920
/// A list of packages to clean. If empty, everything is cleaned.
20-
pub spec: Vec<String>,
21+
pub spec: IndexSet<String>,
2122
/// The target arch triple to clean, or None for the host arch
2223
pub targets: Vec<String>,
2324
/// Whether to clean the release directory
@@ -108,7 +109,7 @@ fn clean_specs(
108109
ws: &Workspace<'_>,
109110
profiles: &Profiles,
110111
targets: &[String],
111-
spec: &[String],
112+
spec: &IndexSet<String>,
112113
dry_run: bool,
113114
) -> CargoResult<()> {
114115
// Clean specific packages.

tests/testsuite/cargo_clean/help/stdout.term.svg

Lines changed: 20 additions & 18 deletions
Loading

tests/testsuite/clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ fn clean_workspace_does_not_touch_non_workspace_packages() {
233233

234234
let fingerprint_path = &p.build_dir().join("debug").join(".fingerprint");
235235

236-
p.cargo("build -p foo -p foo_core -p foo-base").run();
236+
p.cargo("check -p foo -p foo_core -p foo-base").run();
237237

238238
let mut fingerprint_names = get_fingerprints_without_hashes(fingerprint_path);
239239

0 commit comments

Comments
 (0)