Skip to content

Commit

Permalink
Merge pull request #1092 from pkgxdev/fix-error-absorbtion
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl authored Jan 25, 2025
2 parents 28d4e94 + b304ac3 commit 40f36cc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
36 changes: 20 additions & 16 deletions crates/lib/src/cellar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,19 @@ use tokio::fs;
pub async fn ls(project: &str, config: &Config) -> Result<Vec<Installation>, Box<dyn Error>> {
let d = config.pkgx_dir.join(project);

if !fs::metadata(&d).await?.is_dir() {
return Ok(vec![]);
match fs::metadata(&d).await {
Ok(metadata) => {
if !metadata.is_dir() {
return Err(format!("err: expected directory: {:?}", d).into());
}
}
Err(e) => {
if e.kind() == std::io::ErrorKind::NotFound {
return Ok(vec![]);
} else {
return Err(e.into());
}
}
}

let mut rv = vec![];
Expand Down Expand Up @@ -39,23 +50,16 @@ pub async fn ls(project: &str, config: &Config) -> Result<Vec<Installation>, Box
Ok(rv)
}

pub async fn resolve(pkgreq: &PackageReq, config: &Config) -> Result<Installation, Box<dyn Error>> {
let installations = ls(&pkgreq.project, config).await?;

if let Some(i) = installations
pub async fn resolve(
pkgreq: &PackageReq,
config: &Config,
) -> Result<Option<Installation>, Box<dyn Error>> {
Ok(ls(&pkgreq.project, config)
.await?
.iter()
.filter(|i| pkgreq.constraint.satisfies(&i.pkg.version))
.max_by_key(|i| i.pkg.version.clone())
{
Ok(i.clone())
} else {
// If no matching version is found, return an error
Err(format!("couldn’t resolve {:?}", pkgreq).into())
}
}

pub async fn has(pkg: &PackageReq, config: &Config) -> Option<Installation> {
resolve(pkg, config).await.ok()
.cloned())
}

pub fn dst(pkg: &Package, config: &Config) -> PathBuf {
Expand Down
4 changes: 2 additions & 2 deletions crates/lib/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ pub async fn resolve(reqs: Vec<PackageReq>, config: &Config) -> Result<Resolutio

for req in reqs {
futures.push(async move {
if let Some(installation) = cellar::has(&req, config).await {
if let Some(installation) = cellar::resolve(&req, config).await? {
Ok::<_, Box<dyn Error>>((
Some((installation.clone(), installation.pkg.clone())),
None,
))
} else if let Ok(Some(version)) = inventory::select(&req, config).await {
} else if let Some(version) = inventory::select(&req, config).await? {
let pkg = Package {
project: req.project.clone(),
version,
Expand Down

0 comments on commit 40f36cc

Please sign in to comment.