From aefc4535e1768046dec68c503ccab8d7df2ec6f7 Mon Sep 17 00:00:00 2001 From: objz Date: Tue, 28 Jul 2026 20:37:10 +0200 Subject: [PATCH] fix(content): only clean up library dependencies --- src/instance/content/dependencies.rs | 21 +++++++-- src/instance/content/manifest.rs | 7 ++- src/instance/content/reconcile.rs | 3 ++ src/instance/import/mrpack.rs | 1 + src/instance/tests/content/dependencies.rs | 53 ++++++++++++++++++++++ src/instance/tests/content/manifest.rs | 22 +++++++++ src/net/curseforge.rs | 40 ++++++++++++---- src/net/modrinth.rs | 26 +++++++++++ src/net/tests/curseforge.rs | 18 ++++++++ src/net/tests/modrinth.rs | 22 +++++++++ src/tui/input.rs | 1 + src/tui/tests/flows.rs | 2 + src/tui/tests/widgets/content/discovery.rs | 4 ++ src/tui/tests/widgets/content/list.rs | 3 ++ 14 files changed, 209 insertions(+), 14 deletions(-) diff --git a/src/instance/content/dependencies.rs b/src/instance/content/dependencies.rs index 265c4b2..b35fa33 100644 --- a/src/instance/content/dependencies.rs +++ b/src/instance/content/dependencies.rs @@ -27,6 +27,7 @@ pub struct PlannedInstall { pub provider_aliases: Vec, pub required_dependencies: Vec, pub automatic_dependency: bool, + pub cleanup_eligible: bool, pub replacement: bool, } @@ -300,6 +301,7 @@ fn build_records( } record.required_dependencies = item.required_dependencies.clone(); record.automatic_dependency = item.automatic_dependency; + record.cleanup_eligible = item.cleanup_eligible; return Ok((Some(old_relative), record)); } let path = staged @@ -326,6 +328,7 @@ fn build_records( provider_aliases: item.provider_aliases.clone(), required_dependencies: item.required_dependencies.clone(), automatic_dependency: item.automatic_dependency, + cleanup_eligible: item.cleanup_eligible, }, )) }) @@ -374,6 +377,7 @@ struct Node { version: VersionInfo, installed: Option, automatic_dependency: bool, + cleanup_eligible: bool, exact: bool, dependencies: Vec, optional_dependencies: usize, @@ -428,6 +432,7 @@ pub async fn resolve( title: root.title, version: root_version.clone(), automatic_dependency: false, + cleanup_eligible: false, exact: true, installed: root_installed, dependencies: Vec::new(), @@ -494,6 +499,7 @@ pub async fn resolve( version: choice.version.clone(), installed: choice.installed, automatic_dependency: choice.automatic_dependency, + cleanup_eligible: choice.cleanup_eligible, exact, dependencies: Vec::new(), optional_dependencies: 0, @@ -537,6 +543,7 @@ struct ResolvedChoice { version: VersionInfo, installed: Option, automatic_dependency: bool, + cleanup_eligible: bool, } async fn resolve_requirement( @@ -586,13 +593,16 @@ async fn resolve_requirement( )) })? }; - let title = provider.project(&project_id).await?.title; + let project = provider.project(&project_id).await?; + let automatic_dependency = installed + .as_ref() + .is_none_or(|installed| installed.automatic_dependency); + let cleanup_eligible = automatic_dependency && project.is_library_only(); Ok(ResolvedChoice { - title, + title: project.title, version, - automatic_dependency: installed - .as_ref() - .is_none_or(|installed| installed.automatic_dependency), + automatic_dependency, + cleanup_eligible, installed, }) } @@ -884,6 +894,7 @@ fn planned_install( }) .collect(), automatic_dependency: node.automatic_dependency, + cleanup_eligible: node.cleanup_eligible, replacement, } } diff --git a/src/instance/content/manifest.rs b/src/instance/content/manifest.rs index 70e14de..1d929f2 100644 --- a/src/instance/content/manifest.rs +++ b/src/instance/content/manifest.rs @@ -91,6 +91,8 @@ pub struct ContentFileRecord { pub required_dependencies: Vec, #[serde(default)] pub automatic_dependency: bool, + #[serde(default)] + pub cleanup_eligible: bool, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -250,7 +252,10 @@ impl ContentManifest { loop { let mut found = false; for candidate in &self.files { - if !candidate.automatic_dependency || removed.contains(&candidate.relative_path) { + if !candidate.automatic_dependency + || !candidate.cleanup_eligible + || removed.contains(&candidate.relative_path) + { continue; } let still_required = self diff --git a/src/instance/content/reconcile.rs b/src/instance/content/reconcile.rs index 9739aa2..864e8cc 100644 --- a/src/instance/content/reconcile.rs +++ b/src/instance/content/reconcile.rs @@ -332,6 +332,7 @@ fn reconcile_inventory( provider_aliases: Vec::new(), required_dependencies: Vec::new(), automatic_dependency: false, + cleanup_eligible: false, } } else if oversized { tracing::debug!( @@ -352,6 +353,7 @@ fn reconcile_inventory( provider_aliases: Vec::new(), required_dependencies: Vec::new(), automatic_dependency: false, + cleanup_eligible: false, } } else { ContentFileRecord { @@ -363,6 +365,7 @@ fn reconcile_inventory( provider_aliases: Vec::new(), required_dependencies: Vec::new(), automatic_dependency: false, + cleanup_eligible: false, } }; record.kind = kind; diff --git a/src/instance/import/mrpack.rs b/src/instance/import/mrpack.rs index baaed75..cead526 100644 --- a/src/instance/import/mrpack.rs +++ b/src/instance/import/mrpack.rs @@ -350,6 +350,7 @@ fn seed_content_manifest( provider_aliases: Vec::new(), required_dependencies: Vec::new(), automatic_dependency: false, + cleanup_eligible: false, }); } let seeded = manifest.files.len(); diff --git a/src/instance/tests/content/dependencies.rs b/src/instance/tests/content/dependencies.rs index 91e4712..c93921e 100644 --- a/src/instance/tests/content/dependencies.rs +++ b/src/instance/tests/content/dependencies.rs @@ -15,6 +15,7 @@ struct FakeProvider { versions: HashMap, compatible: HashMap>, projects: HashMap, + categories: HashMap>, resolved: HashMap, fail_download: Option, } @@ -72,6 +73,8 @@ impl ContentProvider for FakeProvider { description: String::new(), body: String::new(), icon_url: None, + categories: self.categories.get(project_id).cloned().unwrap_or_default(), + additional_categories: Vec::new(), }) } @@ -192,6 +195,10 @@ fn provider(versions: Vec) -> FakeProvider { .iter() .map(|version| (version.project_id.clone(), version.project_id.clone())) .collect(); + let categories = versions + .iter() + .map(|version| (version.project_id.clone(), vec!["library".to_owned()])) + .collect(); FakeProvider { versions: versions .into_iter() @@ -199,6 +206,7 @@ fn provider(versions: Vec) -> FakeProvider { .collect(), compatible, projects, + categories, resolved: HashMap::new(), fail_download: None, } @@ -255,6 +263,43 @@ async fn required_dependencies_prefer_the_newest_stable_release() { assert_eq!(plan.optional_dependencies, 1); } +#[tokio::test] +async fn functional_mod_dependencies_are_not_cleanup_eligible() { + let root_version = version( + "root", + "root", + VersionType::Release, + "2026-01-01", + vec![dependency("sodium", DependencyType::Required)], + ); + let sodium = version( + "sodium", + "sodium", + VersionType::Release, + "2026-01-01", + Vec::new(), + ); + let mut fake = provider(vec![root_version.clone(), sodium]); + fake.categories.insert( + "sodium".to_owned(), + vec!["library".to_owned(), "optimization".to_owned()], + ); + let registry = fake.registry(); + + let plan = resolve( + ®istry, + &ContentManifest::default(), + Path::new("/minecraft"), + &instance(), + root(root_version), + ) + .await + .unwrap(); + + assert!(plan.items[1].automatic_dependency); + assert!(!plan.items[1].cleanup_eligible); +} + #[tokio::test] async fn exact_cross_provider_match_replaces_only_the_wrong_version() { let root_version = version( @@ -307,6 +352,7 @@ async fn exact_cross_provider_match_replaces_only_the_wrong_version() { provider_aliases: Vec::new(), required_dependencies: Vec::new(), automatic_dependency: false, + cleanup_eligible: false, }], }; @@ -429,6 +475,7 @@ async fn installed_incompatible_projects_block_the_plan() { provider_aliases: Vec::new(), required_dependencies: Vec::new(), automatic_dependency: false, + cleanup_eligible: false, }], }; @@ -490,6 +537,12 @@ async fn dependency_install_commits_files_and_manifest_together() { .unwrap() .automatic_dependency ); + assert!( + manifest + .record(Path::new("mods/library.jar")) + .unwrap() + .cleanup_eligible + ); assert_eq!( manifest .record(Path::new("mods/root.jar")) diff --git a/src/instance/tests/content/manifest.rs b/src/instance/tests/content/manifest.rs index ee268b3..ad64fa8 100644 --- a/src/instance/tests/content/manifest.rs +++ b/src/instance/tests/content/manifest.rs @@ -38,6 +38,7 @@ fn manifest_round_trip_and_lookup_are_exact() { provider_aliases: Vec::new(), required_dependencies: Vec::new(), automatic_dependency: false, + cleanup_eligible: false, }; let mut manifest = ContentManifest::default(); manifest.upsert(record); @@ -90,6 +91,7 @@ fn renaming_a_record_preserves_its_resolution() { provider_aliases: Vec::new(), required_dependencies: Vec::new(), automatic_dependency: false, + cleanup_eligible: false, }); assert!(manifest.rename_record( @@ -129,6 +131,7 @@ fn managed_record( provider_aliases: Vec::new(), required_dependencies, automatic_dependency, + cleanup_eligible: automatic_dependency, } } @@ -211,3 +214,22 @@ fn orphan_cleanup_follows_automatic_dependency_chains() { ] ); } + +#[test] +fn orphan_cleanup_keeps_automatic_non_library_dependencies() { + let mut dependency_record = managed_record("mods/sodium.jar", "sodium", true, Vec::new()); + dependency_record.cleanup_eligible = false; + let manifest = ContentManifest { + version: 1, + files: vec![ + managed_record("mods/root.jar", "root", false, vec![dependency("sodium")]), + dependency_record, + ], + }; + + assert!( + manifest + .orphaned_dependencies_after_removing(Path::new("mods/root.jar")) + .is_empty() + ); +} diff --git a/src/net/curseforge.rs b/src/net/curseforge.rs index fd40ac3..f12b84b 100644 --- a/src/net/curseforge.rs +++ b/src/net/curseforge.rs @@ -41,6 +41,14 @@ struct Mod { #[serde(default)] download_count: u64, logo: Option, + #[serde(default)] + categories: Vec, +} + +#[derive(Debug, Deserialize)] +struct Category { + name: String, + slug: String, } #[derive(Debug, Deserialize)] @@ -283,14 +291,30 @@ pub async fn fetch_project( ), ) .await?; - Ok(ProjectInfo { - id: project.data.id.to_string(), - slug: project.data.slug, - title: project.data.name, - description: project.data.summary, - body: description.data, - icon_url: project.data.logo.map(|logo| logo.thumbnail_url), - }) + Ok(project_info(project.data, description.data)) +} + +fn project_info(project: Mod, body: String) -> ProjectInfo { + ProjectInfo { + id: project.id.to_string(), + slug: project.slug, + title: project.name, + description: project.summary, + body, + icon_url: project.logo.map(|logo| logo.thumbnail_url), + categories: project + .categories + .into_iter() + .map(|category| { + if category.slug.is_empty() { + category.name + } else { + category.slug + } + }) + .collect(), + additional_categories: Vec::new(), + } } pub async fn fetch_versions( diff --git a/src/net/modrinth.rs b/src/net/modrinth.rs index 056d4cc..dd105d8 100644 --- a/src/net/modrinth.rs +++ b/src/net/modrinth.rs @@ -14,6 +14,32 @@ pub struct ProjectInfo { pub body: String, #[serde(default)] pub icon_url: Option, + #[serde(default)] + pub categories: Vec, + #[serde(default)] + pub additional_categories: Vec, +} + +impl ProjectInfo { + pub fn is_library_only(&self) -> bool { + let mut categories = self + .categories + .iter() + .chain(self.additional_categories.iter()) + .peekable(); + categories.peek().is_some() + && categories.all(|category| { + matches!( + category + .chars() + .filter(|character| character.is_alphanumeric()) + .flat_map(char::to_lowercase) + .collect::() + .as_str(), + "library" | "libraries" | "apiandlibrary" | "libraryapi" + ) + }) + } } #[derive(Debug, Clone, Deserialize, serde::Serialize)] diff --git a/src/net/tests/curseforge.rs b/src/net/tests/curseforge.rs index a7a79d3..f06b3c1 100644 --- a/src/net/tests/curseforge.rs +++ b/src/net/tests/curseforge.rs @@ -40,6 +40,24 @@ fn curseforge_file_maps_to_shared_version() { assert_eq!(version.files[0].hashes["sha1"], "abc"); } +#[test] +fn curseforge_library_category_maps_to_cleanup_metadata() { + let project: Mod = serde_json::from_str( + r#"{ + "id": 7, + "name": "Library", + "slug": "library", + "categories": [{ + "name": "API and Library", + "slug": "library-api" + }] + }"#, + ) + .unwrap(); + + assert!(project_info(project, String::new()).is_library_only()); +} + #[tokio::test] async fn curseforge_versions_follow_pagination() { use wiremock::matchers::{method, path, query_param}; diff --git a/src/net/tests/modrinth.rs b/src/net/tests/modrinth.rs index 4042e10..0bfd9ac 100644 --- a/src/net/tests/modrinth.rs +++ b/src/net/tests/modrinth.rs @@ -47,6 +47,28 @@ fn version_dependencies_and_release_type_are_deserialized() { ); } +#[test] +fn only_exclusively_library_categorized_projects_are_cleanup_eligible() { + let project = |categories: &[&str]| ProjectInfo { + id: "project".to_owned(), + slug: "project".to_owned(), + title: "Project".to_owned(), + description: String::new(), + body: String::new(), + icon_url: None, + categories: categories + .iter() + .map(|category| (*category).to_owned()) + .collect(), + additional_categories: Vec::new(), + }; + + assert!(project(&["library"]).is_library_only()); + assert!(project(&["api-and-library"]).is_library_only()); + assert!(!project(&[]).is_library_only()); + assert!(!project(&["library", "utility"]).is_library_only()); +} + #[test] fn discovery_mod_facets_include_instance_compatibility() { let facets = discovery_facets(ContentKind::Mod, "1.21.1", ModLoader::Fabric); diff --git a/src/tui/input.rs b/src/tui/input.rs index 4fc098c..3d56d52 100644 --- a/src/tui/input.rs +++ b/src/tui/input.rs @@ -1068,6 +1068,7 @@ impl App { provider_aliases: Vec::new(), required_dependencies: Vec::new(), automatic_dependency: false, + cleanup_eligible: false, }; crate::instance::ContentManifest::update(&manifest_path, |manifest| { if let Some(old_path) = request.installed_path.as_ref() diff --git a/src/tui/tests/flows.rs b/src/tui/tests/flows.rs index 499e631..76038ae 100644 --- a/src/tui/tests/flows.rs +++ b/src/tui/tests/flows.rs @@ -189,6 +189,7 @@ fn managed_mod_record( provider_aliases: Vec::new(), required_dependencies, automatic_dependency, + cleanup_eligible: automatic_dependency, } } @@ -430,6 +431,7 @@ fn provider_conflict_selection_is_persisted() { provider_aliases: Vec::new(), required_dependencies: Vec::new(), automatic_dependency: false, + cleanup_eligible: false, }], }; let manifest_path = diff --git a/src/tui/tests/widgets/content/discovery.rs b/src/tui/tests/widgets/content/discovery.rs index a9b4f9c..9c731d8 100644 --- a/src/tui/tests/widgets/content/discovery.rs +++ b/src/tui/tests/widgets/content/discovery.rs @@ -352,6 +352,8 @@ fn project_page_loads_for_the_selected_discovery_entry() { description: "Short description".to_owned(), body: "Long **Markdown** description.".to_owned(), icon_url: None, + categories: Vec::new(), + additional_categories: Vec::new(), }), }, ); @@ -522,6 +524,7 @@ fn dependency_resolution_opens_the_existing_confirmation() { provider_aliases: Vec::new(), required_dependencies: Vec::new(), automatic_dependency: false, + cleanup_eligible: false, replacement: false, }], optional_dependencies: 0, @@ -671,6 +674,7 @@ fn installed_labels_follow_exact_manifest_projects() { provider_aliases: Vec::new(), required_dependencies: Vec::new(), automatic_dependency: false, + cleanup_eligible: false, }); state.refresh_installed_manifest(&manifest, std::path::Path::new("first")); assert_eq!( diff --git a/src/tui/tests/widgets/content/list.rs b/src/tui/tests/widgets/content/list.rs index bba3389..616968c 100644 --- a/src/tui/tests/widgets/content/list.rs +++ b/src/tui/tests/widgets/content/list.rs @@ -524,6 +524,7 @@ fn manifest_metadata_keeps_an_embedded_icon_renderer() { provider_aliases: Vec::new(), required_dependencies: Vec::new(), automatic_dependency: false, + cleanup_eligible: false, }); state.apply_manifest(&manifest, &minecraft_dir, crate::instance::ContentKind::Mod); @@ -583,6 +584,8 @@ async fn provider_metadata_loads_from_cache_without_network() { description: "Cached description".to_owned(), body: String::new(), icon_url: None, + categories: Vec::new(), + additional_categories: Vec::new(), }) .unwrap(), )