Skip to content

Commit 87e84c5

Browse files
authored
fix: treat ignored packages as used on the workspace level (#295)
If a workspace member marks an unused dependency as ignored, cargo-shear will still try to remove it on the workspace level. Any subsequent build will fail as the workspace member now tries to inherit a dependency that no longer exists. A failing test case was added in this commit: 0e3a656
1 parent bff600d commit 87e84c5

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

src/package_processor.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,12 @@ impl PackageProcessor {
104104
let module_names_from_package_deps: FxHashSet<String> =
105105
package_dependency_names_map.keys().cloned().collect();
106106

107-
let package_dependency_names: FxHashSet<String> =
108-
package_dependency_names_map.values().cloned().collect();
107+
// Add ignored names to the set of package dependency names to prevent them from being marked unused in the workspace analysis.
108+
let package_dependency_names: FxHashSet<String> = package_dependency_names_map
109+
.values()
110+
.cloned()
111+
.chain(package_ignored_names.into_iter().map(str::to_string))
112+
.collect();
109113

110114
let module_names_from_rust_files = self.analyzer.analyze_package(package)?;
111115

tests/fixtures/workspace_unused_deps/Cargo.lock

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/fixtures/workspace_unused_deps/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ serde = "1.0"
88
unused-crate = "0.1"
99
# Used by app
1010
clap = "4.0"
11+
# Ignored by app - should still be considered used
12+
humantime-serde = "0.1"

tests/fixtures/workspace_unused_deps/app/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ name = "app"
33
version = "0.1.0"
44
edition = "2021"
55

6+
[package.metadata.cargo-shear]
7+
ignored = ["humantime-serde"]
8+
69
[dependencies]
710
clap = { workspace = true }
811
lib = { path = "../lib" }
12+
humantime-serde = { workspace = true }

tests/integration_tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ fn test_workspace_unused_deps_fix() {
9393
// serde and clap should still be there (they're used)
9494
assert!(cargo_toml.contains("serde"), "Used workspace dependency should remain");
9595
assert!(cargo_toml.contains("clap"), "Used workspace dependency should remain");
96+
// humantime-serde should still be there (it's ignored in app)
97+
assert!(cargo_toml.contains("humantime-serde"), "Used workspace dependency should remain");
9698

9799
// unused-crate should be removed (it's not used by anyone)
98100
assert!(!cargo_toml.contains("unused-crate"), "Unused workspace dependency should be removed");

0 commit comments

Comments
 (0)