diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-02 08:42:55 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-02 08:42:55 +0900 |
| commit | b5d6d91054b9ceef95aef0819bb5c4ef7db1abdd (patch) | |
| tree | 0bd12e599f48fcc63375363363416ca48766c698 /crates/shirabe/src/repository/array_repository.rs | |
| parent | 3e367f78eec3521106979461fda717926717515a (diff) | |
| download | php-shirabe-b5d6d91054b9ceef95aef0819bb5c4ef7db1abdd.tar.gz php-shirabe-b5d6d91054b9ceef95aef0819bb5c4ef7db1abdd.tar.zst php-shirabe-b5d6d91054b9ceef95aef0819bb5c4ef7db1abdd.zip | |
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 <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/repository/array_repository.rs')
| -rw-r--r-- | crates/shirabe/src/repository/array_repository.rs | 27 |
1 files changed, 17 insertions, 10 deletions
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<usize> { - if self.packages.borrow().is_none() { - self.initialize(); - } - - Ok(self.packages.borrow().as_ref().unwrap().len()) + fn count(&mut self) -> anyhow::Result<usize> { + 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<bool> { if self.package_map.borrow().is_none() { let mut map: IndexMap<String, BasePackageHandle> = 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( |
