Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error handling when looking for toolchain components #2021

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading