cargo-shear 1.13.4

Detect and fix unused/misplaced dependencies from Cargo.toml
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! # cargo-shear
//!
//! A tool for detecting and removing unused dependencies from Rust projects.
//!
//! ## Overview
//!
//! `cargo-shear` analyzes your Rust codebase to identify dependencies that are declared
//! in `Cargo.toml` but never actually used in the code. It can automatically remove
//! these unused dependencies with the `--fix` flag.
//!
//! ## Architecture
//!
//! The codebase is organized into several focused modules:
//!
//! - `cargo_toml_editor` - Handles modifications to Cargo.toml files
//! - `package_analyzer` - Analyzes packages to find issues
//! - `package_processor` - Processes packages and detects unused dependencies
//! - `source_parser` - Parses Rust source to extract data
//!
//! ## Usage
//!
//! ```no_run
//! use cargo_shear::{CargoShear, CargoShearOptions};
//!
//! let options = CargoShearOptions::new(std::path::PathBuf::from("."));
//! let exit_code = CargoShear::new(std::io::stdout(), options).run();
//! ```

mod cargo_toml_editor;
mod context;
mod diagnostics;
mod manifest;
mod output;
mod package_analyzer;
mod package_processor;
mod source_parser;
#[cfg(test)]
mod tests;
pub mod util;

use std::{
    env, fs,
    io::Write,
    path::{Path, PathBuf},
    process::ExitCode,
    str::FromStr,
};

use anyhow::Result;
use bpaf::Bpaf;
use cargo_metadata::{CargoOpt, Metadata, MetadataCommand, Package};
use owo_colors::OwoColorize;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use rustc_hash::FxHashSet;
use toml_edit::DocumentMut;

pub use crate::output::{ColorMode, OutputFormat};
use crate::{
    cargo_toml_editor::CargoTomlEditor,
    context::{PackageContext, WorkspaceContext},
    diagnostics::ShearAnalysis,
    output::Renderer,
    package_processor::{PackageAnalysis, PackageProcessor, WorkspaceAnalysis},
    util::read_to_string,
};

const VERSION: &str = env!("CARGO_PKG_VERSION");

/// Command-line options for cargo-shear.
///
/// Parsed from `argv` by `bpaf`. The doc comments on individual fields below
/// become the `--help` text Cargo's users see, so keep them user-facing.
///
/// The "batteries" `cargo_helper` strips the leading `shear` argument when
/// invoked as `cargo shear`, so this struct sees the same shape either way.
/// See <https://docs.rs/bpaf/latest/bpaf/batteries/fn.cargo_helper.html>.
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options("shear"), version(VERSION))]
pub struct CargoShearOptions {
    /// Remove unused dependencies.
    ///
    /// When set, cargo-shear will automatically remove detected unused
    /// dependencies from Cargo.toml files.
    #[bpaf(long)]
    fix: bool,

    /// Uses `cargo expand` to expand macros, which requires nightly and is significantly slower.
    ///
    /// This option provides more accurate detection by expanding proc macros
    /// and attribute macros, but requires a nightly Rust toolchain.
    #[bpaf(long)]
    expand: bool,

    /// Detect mismatches between `test`/`doctest` target settings and source content.
    ///
    /// When set, cargo-shear warns when `[lib] test = false` is paired with source
    /// that contains tests (or `doctest = false` with source that contains doc tests),
    /// and — within a workspace — when `test`/`doctest` are left at their default of
    /// `true` for lib targets that contain none.
    #[bpaf(long("check-test-targets"))]
    check_test_targets: bool,

    /// Treat warnings as errors.
    ///
    /// When set, warnings will cause cargo-shear to exit with a failure code.
    #[bpaf(long("deny-warnings"))]
    deny_warnings: bool,

    /// Assert that `Cargo.lock` will remain unchanged.
    locked: bool,

    /// Run without accessing the network
    offline: bool,

    /// Equivalent to specifying both --locked and --offline
    frozen: bool,

    /// Package(s) to check.
    ///
    /// If not specified, all packages in the workspace are checked.
    /// Can be specified multiple times to check specific packages.
    #[bpaf(long, short, argument("SPEC"))]
    package: Vec<String>,

    /// Exclude packages from the check.
    ///
    /// Can be specified multiple times to exclude multiple packages.
    exclude: Vec<String>,

    /// Output format: auto, json, github
    #[bpaf(long, fallback(OutputFormat::Auto))]
    format: OutputFormat,

    /// Color usage for output: auto, always, never
    #[bpaf(long, fallback(ColorMode::Auto))]
    color: ColorMode,

    /// Path to the project directory.
    ///
    /// Defaults to the current directory if not specified.
    #[bpaf(positional("PATH"), fallback_with(default_path))]
    path: PathBuf,
}

impl CargoShearOptions {
    /// Construct an options value with every flag at its default and the project
    /// rooted at `path`. Programmatic callers usually layer `with_*` builders on top.
    #[must_use]
    pub fn new(path: PathBuf) -> Self {
        Self {
            path,
            fix: false,
            expand: false,
            check_test_targets: false,
            deny_warnings: false,
            locked: false,
            offline: false,
            frozen: false,
            package: vec![],
            exclude: vec![],
            format: OutputFormat::default(),
            color: ColorMode::default(),
        }
    }

    /// Enable `--fix`: rewrite manifests to apply fixable diagnostics.
    #[must_use]
    pub const fn with_fix(mut self) -> Self {
        self.fix = true;
        self
    }

    /// Enable `--expand`: run `cargo expand` first to surface macro-generated imports.
    #[must_use]
    pub const fn with_expand(mut self) -> Self {
        self.expand = true;
        self
    }

    /// Enable `--check-test-targets`: emit `test`/`doctest` flag mismatch diagnostics.
    #[must_use]
    pub const fn with_check_test_targets(mut self) -> Self {
        self.check_test_targets = true;
        self
    }

    /// Enable `--deny-warnings`: turn any warning into a non-zero exit.
    #[must_use]
    pub const fn with_deny_warnings(mut self) -> Self {
        self.deny_warnings = true;
        self
    }

    /// Enable `--locked`: forwarded to `cargo metadata`.
    #[must_use]
    pub const fn with_locked(mut self) -> Self {
        self.locked = true;
        self
    }

    /// Enable `--offline`: forwarded to `cargo metadata`.
    #[must_use]
    pub const fn with_offline(mut self) -> Self {
        self.offline = true;
        self
    }

    /// Enable `--frozen`: forwarded to `cargo metadata` (implies `--locked` and `--offline`).
    #[must_use]
    pub const fn with_frozen(mut self) -> Self {
        self.frozen = true;
        self
    }

    /// Restrict the run to these workspace members. Empty means "all".
    #[must_use]
    pub fn with_packages(mut self, packages: Vec<String>) -> Self {
        self.package = packages;
        self
    }

    /// Exclude these workspace members from the run.
    #[must_use]
    pub fn with_excludes(mut self, excludes: Vec<String>) -> Self {
        self.exclude = excludes;
        self
    }

    /// Pick the renderer used for diagnostics.
    #[must_use]
    pub const fn with_format(mut self, format: OutputFormat) -> Self {
        self.format = format;
        self
    }

    /// Override automatic color detection.
    #[must_use]
    pub const fn with_color(mut self, color: ColorMode) -> Self {
        self.color = color;
        self
    }

    /// Apply environment-based resolution to defaults — currently only
    /// substitutes the GitHub renderer for `format = Auto` under CI.
    /// Call this after CLI parsing but before passing options to `CargoShear`.
    #[must_use]
    pub fn resolve(mut self) -> Self {
        self.format = self.format.resolve();
        self
    }
}

pub(crate) fn default_path() -> Result<PathBuf> {
    Ok(env::current_dir()?)
}

/// Top-level entry point: orchestrates `cargo metadata`, runs each package
/// through [`PackageProcessor`], optionally rewrites manifests, and renders
/// the aggregated [`ShearAnalysis`] to `writer`.
pub struct CargoShear<W> {
    /// Sink for rendered diagnostics (typically `std::io::stdout()`).
    writer: W,

    /// Caller-supplied configuration; immutable once `run` starts.
    options: CargoShearOptions,

    /// Diagnostics accumulated as each package is processed.
    analysis: ShearAnalysis,
}

impl<W: Write> CargoShear<W> {
    /// Build a runner that will write diagnostics to `writer` using the
    /// settings in `options`.
    ///
    /// ```
    /// use cargo_shear::{CargoShear, CargoShearOptions};
    /// use std::path::PathBuf;
    ///
    /// let options = CargoShearOptions::new(PathBuf::from("."));
    /// let shear = CargoShear::new(std::io::stdout(), options);
    /// ```
    #[must_use]
    pub fn new(writer: W, options: CargoShearOptions) -> Self {
        let analysis = ShearAnalysis::new(options.clone());
        Self { writer, options, analysis }
    }

    /// Execute the full pipeline: analyze every selected package, optionally
    /// apply `--fix` rewrites, render the report, and translate the result
    /// into a process exit code.
    ///
    /// Returns:
    /// - `0` if no issues were found, or every found issue was fixed by `--fix`.
    /// - `1` if issues were found and not all of them were fixed.
    /// - `2` if a fatal error occurred (cargo failure, IO error, ...).
    #[must_use]
    pub fn run(mut self) -> ExitCode {
        match self.shear() {
            Ok(()) => {
                let color = self.options.color.enabled();
                let mut renderer = Renderer::new(&mut self.writer, self.options.format, color);

                if let Err(err) = renderer.render(&self.analysis) {
                    let _ = writeln!(self.writer, "error rendering report: {err:?}");
                    return ExitCode::from(2);
                }

                self.determine_exit_code()
            }
            Err(err) => {
                let _ = writeln!(self.writer, "error: {err:?}");
                ExitCode::from(2)
            }
        }
    }

    /// Translate accumulated counts into the documented `0`/`1`/`2` exit code.
    /// `2` is reserved for fatal errors and is set in [`run`](Self::run).
    const fn determine_exit_code(&self) -> ExitCode {
        // `--fix` succeeded if at least one repair landed and nothing else errored.
        if self.options.fix && self.analysis.fixed > 0 && self.analysis.errors == 0 {
            return ExitCode::SUCCESS;
        }

        // Errors always fail; warnings only fail when `--deny-warnings` was passed.
        let has_errors = self.analysis.errors > 0;
        let has_warnings = self.options.deny_warnings && self.analysis.warnings > 0;

        if has_errors || has_warnings { ExitCode::FAILURE } else { ExitCode::SUCCESS }
    }

    fn shear(&mut self) -> Result<()> {
        let mut extra_opts = Vec::new();
        if self.options.locked {
            extra_opts.push("--locked".to_owned());
        }
        if self.options.offline {
            extra_opts.push("--offline".to_owned());
        }
        if self.options.frozen {
            extra_opts.push("--frozen".to_owned());
        }

        let metadata = MetadataCommand::new()
            .features(CargoOpt::AllFeatures)
            .current_dir(&self.options.path)
            .other_options(extra_opts)
            .verbose(true)
            .exec()
            .map_err(|e| anyhow::anyhow!("Metadata error: {e}"))?;

        let processor = PackageProcessor::new(self.options.expand, self.options.check_test_targets);
        let workspace_ctx = WorkspaceContext::new(&metadata)?;

        let packages = metadata.workspace_packages();
        let packages: Vec<_> = packages
            .into_iter()
            .filter(|package| {
                // `--exclude` always wins over `--package` (matches Cargo's behaviour).
                if self.options.exclude.iter().any(|name| name == package.name.as_str()) {
                    return false;
                }

                // When `--package` is given, restrict the run to its allowlist.
                if !self.options.package.is_empty()
                    && !self.options.package.iter().any(|name| name == package.name.as_str())
                {
                    return false;
                }

                true
            })
            .collect();

        let total = packages.len();
        let results: Vec<_> = if self.options.expand {
            // `cargo expand` shells out to `cargo build` per package, which holds
            // a global build lock — running these in parallel would just serialize
            // on the lock and lose the progress output. Stay sequential.
            packages
                .iter()
                .enumerate()
                .map(|(index, package)| {
                    eprintln!(
                        "{:>12} {} [{}/{}]",
                        "Expanding".bright_cyan().bold(),
                        package.name,
                        index + 1,
                        total
                    );

                    Self::process_package(&processor, &workspace_ctx, package, &metadata)
                })
                .collect::<Result<Vec<_>>>()?
        } else {
            // Process packages in parallel
            packages
                .par_iter()
                .map(|package| {
                    Self::process_package(&processor, &workspace_ctx, package, &metadata)
                })
                .collect::<Result<Vec<_>>>()?
        };

        let mut used_workspace_ignore_paths: FxHashSet<String> = FxHashSet::default();
        let mut used_ignores: FxHashSet<String> = FxHashSet::default();
        for (ctx, result) in results {
            let fixed = self.fix_package_issues(&ctx.manifest_path, &result)?;
            used_workspace_ignore_paths.extend(result.used_workspace_ignore_paths.iter().cloned());
            used_ignores.extend(result.used_ignores.iter().cloned());
            self.analysis.add_package_result(&ctx, &result, fixed);
        }

        // Workspace-level diagnostics need a complete view of which workspace
        // dependencies are actually used; skip them when the user has narrowed
        // the run with `--package`/`--exclude` and the picture is incomplete.
        if self.options.package.is_empty() && self.options.exclude.is_empty() {
            let workspace_result = PackageProcessor::process_workspace(
                &workspace_ctx,
                &self.analysis.packages,
                &used_workspace_ignore_paths,
                used_ignores,
            );

            let fixed =
                self.fix_workspace_issues(&workspace_ctx.manifest_path, &workspace_result)?;
            self.analysis.add_workspace_result(&workspace_ctx, &workspace_result, fixed);
        }

        Ok(())
    }

    fn process_package<'a>(
        processor: &PackageProcessor,
        workspace_ctx: &'a WorkspaceContext,
        package: &Package,
        metadata: &'a Metadata,
    ) -> Result<(PackageContext<'a>, PackageAnalysis)> {
        let ctx = PackageContext::new(workspace_ctx, package, metadata)?;
        let result = processor.process_package(&ctx)?;
        Ok((ctx, result))
    }

    fn fix_package_issues(&self, manifest_path: &Path, result: &PackageAnalysis) -> Result<usize> {
        if !self.options.fix {
            return Ok(0);
        }

        if !result.has_fixable_issues() {
            return Ok(0);
        }

        let content = read_to_string(manifest_path)?;
        let mut manifest = DocumentMut::from_str(&content)?;

        let fixed_unused =
            CargoTomlEditor::remove_dependencies(&mut manifest, &result.unused_dependencies);
        let fixed_misplaced = CargoTomlEditor::move_to_dev_dependencies(
            &mut manifest,
            &result.misplaced_dependencies,
        );
        let mut flag_fixes = 0usize;
        if !result.test_disabled_with_tests.is_empty() {
            flag_fixes += usize::from(CargoTomlEditor::remove_lib_flag(&mut manifest, "test"));
        }
        if !result.test_enabled_without_tests.is_empty() {
            CargoTomlEditor::set_lib_flag_false(&mut manifest, "test");
            flag_fixes += 1;
        }
        if !result.doctest_disabled_with_doctests.is_empty() {
            flag_fixes += usize::from(CargoTomlEditor::remove_lib_flag(&mut manifest, "doctest"));
        }
        if !result.doctest_enabled_without_doctests.is_empty() {
            CargoTomlEditor::set_lib_flag_false(&mut manifest, "doctest");
            flag_fixes += 1;
        }

        fs::write(manifest_path, manifest.to_string())?;
        Ok(fixed_unused + fixed_misplaced + flag_fixes)
    }

    fn fix_workspace_issues(
        &self,
        manifest_path: &Path,
        result: &WorkspaceAnalysis,
    ) -> Result<usize> {
        if !self.options.fix {
            return Ok(0);
        }

        if result.unused_dependencies.is_empty() {
            return Ok(0);
        }

        let content = read_to_string(manifest_path)?;
        let mut manifest = DocumentMut::from_str(&content)?;

        let fixed =
            CargoTomlEditor::remove_workspace_deps(&mut manifest, &result.unused_dependencies);

        fs::write(manifest_path, manifest.to_string())?;
        Ok(fixed)
    }
}