Skip to content

Commit e22e086

Browse files
authored
Merge pull request #2021 from lqd/lib-dir
Fix error handling when looking for toolchain components
2 parents c6c361b + 862da08 commit e22e086

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

collector/src/toolchain.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ impl ToolchainComponents {
278278
/// Finds known library components in the given `dir` and stores them in `self`.
279279
fn fill_libraries(&mut self, dir: &Path) -> anyhow::Result<()> {
280280
let files: Vec<(PathBuf, String)> = fs::read_dir(dir)
281-
.context("Cannot read lib dir to find components")?
281+
.with_context(|| format!("Cannot read lib dir `{}` to find components", dir.display()))?
282282
.map(|entry| Ok(entry?))
283283
.collect::<anyhow::Result<Vec<_>>>()?
284284
.into_iter()
@@ -589,14 +589,23 @@ pub fn create_toolchain_from_published_version(
589589
}
590590

591591
fn get_lib_dir_from_rustc(rustc: &Path) -> anyhow::Result<PathBuf> {
592-
let sysroot = Command::new(rustc)
593-
.arg("--print")
594-
.arg("sysroot")
595-
.output()?
596-
.stdout;
597-
let sysroot_path = String::from_utf8_lossy(&sysroot);
598-
599-
Ok(Path::new(sysroot_path.as_ref().trim()).join("lib"))
592+
let output = Command::new(rustc).arg("--print").arg("sysroot").output()?;
593+
if !output.status.success() {
594+
anyhow::bail!(
595+
"rustc failed to provide sysroot, exit status: {}\nstderr: {}",
596+
output.status,
597+
String::from_utf8_lossy(&output.stderr)
598+
);
599+
}
600+
let sysroot_path = String::from_utf8_lossy(&output.stdout);
601+
let lib_dir = Path::new(sysroot_path.as_ref().trim()).join("lib");
602+
if !lib_dir.exists() {
603+
anyhow::bail!(
604+
"rustc returned non-existent sysroot: `{}`",
605+
lib_dir.display()
606+
);
607+
}
608+
Ok(lib_dir)
600609
}
601610

602611
#[cfg(test)]

0 commit comments

Comments
 (0)