Skip to content

Commit

Permalink
Merge pull request #2021 from lqd/lib-dir
Browse files Browse the repository at this point in the history
Fix error handling when looking for toolchain components
  • Loading branch information
Kobzol authored Jan 9, 2025
2 parents c6c361b + 862da08 commit e22e086
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions collector/src/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl ToolchainComponents {
/// Finds known library components in the given `dir` and stores them in `self`.
fn fill_libraries(&mut self, dir: &Path) -> anyhow::Result<()> {
let files: Vec<(PathBuf, String)> = fs::read_dir(dir)
.context("Cannot read lib dir to find components")?
.with_context(|| format!("Cannot read lib dir `{}` to find components", dir.display()))?
.map(|entry| Ok(entry?))
.collect::<anyhow::Result<Vec<_>>>()?
.into_iter()
Expand Down Expand Up @@ -589,14 +589,23 @@ pub fn create_toolchain_from_published_version(
}

fn get_lib_dir_from_rustc(rustc: &Path) -> anyhow::Result<PathBuf> {
let sysroot = Command::new(rustc)
.arg("--print")
.arg("sysroot")
.output()?
.stdout;
let sysroot_path = String::from_utf8_lossy(&sysroot);

Ok(Path::new(sysroot_path.as_ref().trim()).join("lib"))
let output = Command::new(rustc).arg("--print").arg("sysroot").output()?;
if !output.status.success() {
anyhow::bail!(
"rustc failed to provide sysroot, exit status: {}\nstderr: {}",
output.status,
String::from_utf8_lossy(&output.stderr)
);
}
let sysroot_path = String::from_utf8_lossy(&output.stdout);
let lib_dir = Path::new(sysroot_path.as_ref().trim()).join("lib");
if !lib_dir.exists() {
anyhow::bail!(
"rustc returned non-existent sysroot: `{}`",
lib_dir.display()
);
}
Ok(lib_dir)
}

#[cfg(test)]
Expand Down

0 comments on commit e22e086

Please sign in to comment.