From b5d6d91054b9ceef95aef0819bb5c4ef7db1abdd Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 2 Aug 2026 08:42:55 +0900 Subject: fix(repository): run lazy initialization in count/has_package PHP's ArrayRepository::count()/hasPackage() call $this->initialize(), which late-binds to the concrete repository class and lazily loads its packages. The Rust pass-throughs skipped that: they ran ArrayRepository's stub initialize instead, returning 0/false and marking the repository initialized with an empty package list, which made ensure_initialized() skip the real initialization forever after. Take &mut self in RepositoryInterface::count/has_package so the lazy repositories (Filesystem, Platform, Composer) can guard with their real initialize, and return Result from has_package since that initialization can fail (PHP propagates the exception). InstallerInterface::is_installed and InstallationManager::is_package_installed/mark_alias_installed propagate the same way, which also resolves the TODO(phase-d) markers on Package/Path/Artifact/Vcs repositories about initialization errors being swallowed. Co-Authored-By: Claude Fable 5 --- crates/shirabe/src/repository/array_repository.rs | 27 ++++++++++++++--------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'crates/shirabe/src/repository/array_repository.rs') diff --git a/crates/shirabe/src/repository/array_repository.rs b/crates/shirabe/src/repository/array_repository.rs index 09c37766..30598b62 100644 --- a/crates/shirabe/src/repository/array_repository.rs +++ b/crates/shirabe/src/repository/array_repository.rs @@ -205,6 +205,16 @@ impl ArrayRepository { *self.packages.borrow_mut() = Some(vec![]); } + /// Shared body of `RepositoryInterface::count`, kept on `&self` for `get_repo_name` (PHP's + /// `getRepoName` also triggers lazy initialization through `count()`). + pub(crate) fn base_count(&self) -> usize { + if self.packages.borrow().is_none() { + self.initialize(); + } + + self.packages.borrow().as_ref().unwrap().len() + } + /// Resets the packages cache so the next access re-runs `initialize`. pub(crate) fn reset_packages(&self) { *self.packages.borrow_mut() = None; @@ -217,16 +227,12 @@ impl ArrayRepository { impl RepositoryInterface for ArrayRepository { /// Returns the number of packages in this repository - fn count(&self) -> anyhow::Result { - if self.packages.borrow().is_none() { - self.initialize(); - } - - Ok(self.packages.borrow().as_ref().unwrap().len()) + fn count(&mut self) -> anyhow::Result { + Ok(self.base_count()) } fn get_repo_name(&self) -> String { - let count = self.count().expect("ArrayRepository::count is infallible"); + let count = self.base_count(); format!( "array repo (defining {} package{})", count, @@ -409,7 +415,7 @@ impl RepositoryInterface for ArrayRepository { Ok(matches.into_values().collect()) } - fn has_package(&self, package: PackageInterfaceHandle) -> bool { + fn has_package(&mut self, package: PackageInterfaceHandle) -> anyhow::Result { if self.package_map.borrow().is_none() { let mut map: IndexMap = IndexMap::new(); for repo_package in self.get_packages_internal() { @@ -418,11 +424,12 @@ impl RepositoryInterface for ArrayRepository { *self.package_map.borrow_mut() = Some(map); } - self.package_map + Ok(self + .package_map .borrow() .as_ref() .unwrap() - .contains_key(&package.get_unique_name()) + .contains_key(&package.get_unique_name())) } fn get_providers( -- cgit v1.3.1