Skip to content

Commit 40f36cc

Browse files
authored
Merge pull request #1092 from pkgxdev/fix-error-absorbtion
2 parents 28d4e94 + b304ac3 commit 40f36cc

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

crates/lib/src/cellar.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,19 @@ use tokio::fs;
88
pub async fn ls(project: &str, config: &Config) -> Result<Vec<Installation>, Box<dyn Error>> {
99
let d = config.pkgx_dir.join(project);
1010

11-
if !fs::metadata(&d).await?.is_dir() {
12-
return Ok(vec![]);
11+
match fs::metadata(&d).await {
12+
Ok(metadata) => {
13+
if !metadata.is_dir() {
14+
return Err(format!("err: expected directory: {:?}", d).into());
15+
}
16+
}
17+
Err(e) => {
18+
if e.kind() == std::io::ErrorKind::NotFound {
19+
return Ok(vec![]);
20+
} else {
21+
return Err(e.into());
22+
}
23+
}
1324
}
1425

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

42-
pub async fn resolve(pkgreq: &PackageReq, config: &Config) -> Result<Installation, Box<dyn Error>> {
43-
let installations = ls(&pkgreq.project, config).await?;
44-
45-
if let Some(i) = installations
53+
pub async fn resolve(
54+
pkgreq: &PackageReq,
55+
config: &Config,
56+
) -> Result<Option<Installation>, Box<dyn Error>> {
57+
Ok(ls(&pkgreq.project, config)
58+
.await?
4659
.iter()
4760
.filter(|i| pkgreq.constraint.satisfies(&i.pkg.version))
4861
.max_by_key(|i| i.pkg.version.clone())
49-
{
50-
Ok(i.clone())
51-
} else {
52-
// If no matching version is found, return an error
53-
Err(format!("couldn’t resolve {:?}", pkgreq).into())
54-
}
55-
}
56-
57-
pub async fn has(pkg: &PackageReq, config: &Config) -> Option<Installation> {
58-
resolve(pkg, config).await.ok()
62+
.cloned())
5963
}
6064

6165
pub fn dst(pkg: &Package, config: &Config) -> PathBuf {

crates/lib/src/resolve.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ pub async fn resolve(reqs: Vec<PackageReq>, config: &Config) -> Result<Resolutio
2626

2727
for req in reqs {
2828
futures.push(async move {
29-
if let Some(installation) = cellar::has(&req, config).await {
29+
if let Some(installation) = cellar::resolve(&req, config).await? {
3030
Ok::<_, Box<dyn Error>>((
3131
Some((installation.clone(), installation.pkg.clone())),
3232
None,
3333
))
34-
} else if let Ok(Some(version)) = inventory::select(&req, config).await {
34+
} else if let Some(version) = inventory::select(&req, config).await? {
3535
let pkg = Package {
3636
project: req.project.clone(),
3737
version,

0 commit comments

Comments
 (0)