Add more context to errors in high level crates. (#13351)
Some checks are pending
CI / cargo shear (push) Waiting to run
CI / cargo test | ubuntu (push) Blocked by required conditions
CI / integration test | pypy on windows (push) Blocked by required conditions
CI / cargo test | macos (push) Blocked by required conditions
CI / cargo test | windows (push) Blocked by required conditions
CI / check windows trampoline | aarch64 (push) Blocked by required conditions
CI / check windows trampoline | i686 (push) Blocked by required conditions
CI / build binary | linux libc (push) Blocked by required conditions
CI / build binary | macos aarch64 (push) Blocked by required conditions
CI / build binary | macos x86_64 (push) Blocked by required conditions
CI / build binary | windows x86_64 (push) Blocked by required conditions
CI / build binary | windows aarch64 (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / build binary | freebsd (push) Blocked by required conditions
CI / ecosystem test | pydantic/pydantic-core (push) Blocked by required conditions
CI / ecosystem test | prefecthq/prefect (push) Blocked by required conditions
CI / ecosystem test | pallets/flask (push) Blocked by required conditions
CI / smoke test | macos (push) Blocked by required conditions
CI / smoke test | windows x86_64 (push) Blocked by required conditions
CI / smoke test | windows aarch64 (push) Blocked by required conditions
CI / integration test | conda on ubuntu (push) Blocked by required conditions
CI / integration test | deadsnakes python3.9 on ubuntu (push) Blocked by required conditions
CI / integration test | free-threaded on linux (push) Blocked by required conditions
CI / integration test | free-threaded on windows (push) Blocked by required conditions
CI / integration test | graalpy on ubuntu (push) Blocked by required conditions
CI / integration test | graalpy on windows (push) Blocked by required conditions
CI / integration test | github actions (push) Blocked by required conditions
CI / integration test | free-threaded python on github actions (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / lint (push) Waiting to run
CI / cargo clippy | ubuntu (push) Blocked by required conditions
CI / cargo clippy | windows (push) Blocked by required conditions
CI / cargo dev generate-all (push) Blocked by required conditions
CI / check windows trampoline | x86_64 (push) Blocked by required conditions
CI / test windows trampoline | i686 (push) Blocked by required conditions
CI / test windows trampoline | x86_64 (push) Blocked by required conditions
CI / typos (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / integration test | pypy on ubuntu (push) Blocked by required conditions
CI / build binary | linux musl (push) Blocked by required conditions
CI / smoke test | linux (push) Blocked by required conditions
CI / check system | alpine (push) Blocked by required conditions
CI / integration test | determine publish changes (push) Blocked by required conditions
CI / integration test | uv publish (push) Blocked by required conditions
CI / integration test | uv_build (push) Blocked by required conditions
CI / check cache | ubuntu (push) Blocked by required conditions
CI / check cache | macos aarch64 (push) Blocked by required conditions
CI / check system | python on debian (push) Blocked by required conditions
CI / check system | python on fedora (push) Blocked by required conditions
CI / check system | python on ubuntu (push) Blocked by required conditions
CI / check system | python on opensuse (push) Blocked by required conditions
CI / check system | python on rocky linux 8 (push) Blocked by required conditions
CI / check system | python on rocky linux 9 (push) Blocked by required conditions
CI / check system | graalpy on ubuntu (push) Blocked by required conditions
CI / check system | pypy on ubuntu (push) Blocked by required conditions
CI / check system | pyston (push) Blocked by required conditions
CI / check system | python on macos aarch64 (push) Blocked by required conditions
CI / check system | homebrew python on macos aarch64 (push) Blocked by required conditions
CI / check system | python on macos x86-64 (push) Blocked by required conditions
CI / check system | python3.10 on windows x86-64 (push) Blocked by required conditions
CI / check system | python3.10 on windows x86 (push) Blocked by required conditions
CI / check system | python3.13 on windows x86-64 (push) Blocked by required conditions
CI / check system | x86-64 python3.13 on windows aarch64 (push) Blocked by required conditions
CI / check system | windows registry (push) Blocked by required conditions
CI / check system | python3.12 via chocolatey (push) Blocked by required conditions
CI / check system | python3.9 via pyenv (push) Blocked by required conditions
CI / check system | python3.13 (push) Blocked by required conditions
CI / check system | conda3.11 on macos aarch64 (push) Blocked by required conditions
CI / check system | conda3.8 on macos aarch64 (push) Blocked by required conditions
CI / check system | conda3.11 on linux x86-64 (push) Blocked by required conditions
CI / check system | conda3.8 on linux x86-64 (push) Blocked by required conditions
CI / check system | conda3.11 on windows x86-64 (push) Blocked by required conditions
CI / check system | conda3.8 on windows x86-64 (push) Blocked by required conditions
CI / check system | amazonlinux (push) Blocked by required conditions
CI / check system | embedded python3.10 on windows x86-64 (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions

Add error context in some places where it was previously missing, and a
few style improvements.
This commit is contained in:
konsti 2025-05-12 13:04:41 +02:00 committed by GitHub
parent c65b895e2e
commit 1afadda819
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 50 additions and 31 deletions

View file

@ -55,6 +55,6 @@ same-file = { workspace = true }
self-replace = { workspace = true }
[dev-dependencies]
anyhow = { version = "1.0.89" }
anyhow = { workspace = true }
assert_fs = { version = "1.1.2" }
indoc = { workspace = true }

View file

@ -55,10 +55,10 @@ impl SitePackages {
for site_packages in interpreter.site_packages() {
// Read the site-packages directory.
let site_packages = match fs::read_dir(site_packages) {
Ok(site_packages) => {
let site_packages = match fs::read_dir(site_packages.as_ref()) {
Ok(read_dir) => {
// Collect sorted directory paths; `read_dir` is not stable across platforms
let dist_likes: BTreeSet<_> = site_packages
let dist_likes: BTreeSet<_> = read_dir
.filter_map(|read_dir| match read_dir {
Ok(entry) => match entry.file_type() {
Ok(file_type) => (file_type.is_dir()
@ -71,7 +71,13 @@ impl SitePackages {
},
Err(err) => Some(Err(err)),
})
.collect::<Result<_, std::io::Error>>()?;
.collect::<Result<_, std::io::Error>>()
.with_context(|| {
format!(
"Failed to read site-packages directory contents: {}",
site_packages.user_display()
)
})?;
dist_likes
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {

View file

@ -72,7 +72,7 @@ windows-result = { workspace = true }
windows-sys = { workspace = true }
[dev-dependencies]
anyhow = { version = "1.0.89" }
anyhow = { workspace = true }
assert_fs = { version = "1.1.2" }
indoc = { workspace = true }
insta = { version = "1.40.0" }

View file

@ -38,7 +38,7 @@ url = { workspace = true }
http = ["reqwest", "reqwest-middleware"]
[dev-dependencies]
anyhow = { version = "1.0.89" }
anyhow = { workspace = true }
assert_fs = { version = "1.1.2" }
indoc = { workspace = true }
insta = { version = "1.40.0", features = ["filters"] }

View file

@ -170,7 +170,8 @@ impl RequirementsSource {
let prompt = format!(
"`{name}` looks like a local requirements file but was passed as a package name. Did you mean `-r {name}`?"
);
let confirmation = uv_console::confirm(&prompt, &term, true)?;
let confirmation =
uv_console::confirm(&prompt, &term, true).context("Confirm prompt failed")?;
if confirmation {
return Self::from_requirements_file(name.into());
}
@ -190,7 +191,8 @@ impl RequirementsSource {
let prompt = format!(
"`{name}` looks like a local metadata file but was passed as a package name. Did you mean `-r {name}`?"
);
let confirmation = uv_console::confirm(&prompt, &term, true)?;
let confirmation =
uv_console::confirm(&prompt, &term, true).context("Confirm prompt failed")?;
if confirmation {
return Self::from_requirements_file(name.into());
}
@ -218,7 +220,8 @@ impl RequirementsSource {
let prompt = format!(
"`{name}` looks like a local requirements file but was passed as a package name. Did you mean `--with-requirements {name}`?"
);
let confirmation = uv_console::confirm(&prompt, &term, true)?;
let confirmation =
uv_console::confirm(&prompt, &term, true).context("Confirm prompt failed")?;
if confirmation {
return Self::from_requirements_file(name.into());
}
@ -238,7 +241,8 @@ impl RequirementsSource {
let prompt = format!(
"`{name}` looks like a local metadata file but was passed as a package name. Did you mean `--with-requirements {name}`?"
);
let confirmation = uv_console::confirm(&prompt, &term, true)?;
let confirmation =
uv_console::confirm(&prompt, &term, true).context("Confirm prompt failed")?;
if confirmation {
return Self::from_requirements_file(name.into());
}

View file

@ -31,6 +31,6 @@ zip = { workspace = true }
[dev-dependencies]
assert_cmd = { version = "2.0.16" }
assert_fs = { version = "1.1.2" }
anyhow = { version = "1.0.89" }
anyhow = { workspace = true }
fs-err = { workspace = true }
which = { workspace = true }

View file

@ -260,8 +260,7 @@ async fn build_impl(
let workspace = match workspace {
Ok(ref workspace) => workspace,
Err(err) => {
return Err(anyhow::Error::from(err)
.context("`--package` was provided, but no workspace was found"));
return Err(err).context("`--package` was provided, but no workspace was found");
}
};
@ -289,8 +288,8 @@ async fn build_impl(
let workspace = match workspace {
Ok(ref workspace) => workspace,
Err(err) => {
return Err(anyhow::Error::from(err)
.context("`--all-packages` was provided, but no workspace was found"));
return Err(err)
.context("`--all-packages` was provided, but no workspace was found");
}
};

View file

@ -3,6 +3,7 @@ use std::fmt::Write;
use std::path::PathBuf;
use std::sync::Arc;
use anyhow::Context;
use itertools::Itertools;
use owo_colors::OwoColorize;
use tracing::{debug, enabled, Level};
@ -437,7 +438,8 @@ pub(crate) async fn pip_install(
let install_path = std::path::absolute(&pylock)?;
let install_path = install_path.parent().unwrap();
let content = fs_err::tokio::read_to_string(&pylock).await?;
let lock = toml::from_str::<PylockToml>(&content)?;
let lock = toml::from_str::<PylockToml>(&content)
.with_context(|| format!("Not a valid pylock.toml file: {}", pylock.user_display()))?;
let resolution =
lock.to_resolution(install_path, marker_env.markers(), &tags, &build_options)?;

View file

@ -2,7 +2,7 @@ use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Write;
use std::sync::Arc;
use anyhow::Result;
use anyhow::{Context, Result};
use owo_colors::OwoColorize;
use tracing::debug;
@ -372,7 +372,8 @@ pub(crate) async fn pip_sync(
let install_path = std::path::absolute(&pylock)?;
let install_path = install_path.parent().unwrap();
let content = fs_err::tokio::read_to_string(&pylock).await?;
let lock = toml::from_str::<PylockToml>(&content)?;
let lock = toml::from_str::<PylockToml>(&content)
.with_context(|| format!("Not a valid pylock.toml file: {}", pylock.user_display()))?;
let resolution =
lock.to_resolution(install_path, marker_env.markers(), &tags, &build_options)?;

View file

@ -236,10 +236,12 @@ async fn init_script(
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => None,
Err(err) => {
return Err(anyhow::Error::from(err).context(format!(
"Failed to read script at `{}`",
script_path.simplified_display().cyan()
)));
return Err(err).with_context(|| {
format!(
"Failed to read script at `{}`",
script_path.simplified_display().cyan()
)
});
}
};
@ -328,10 +330,12 @@ async fn init_project(
warn!("Ignoring workspace discovery error due to `--no-workspace`: {err}");
None
} else {
return Err(anyhow::Error::from(err).context(format!(
"Failed to discover parent workspace; use `{}` to ignore",
"uv init --no-workspace".green()
)));
return Err(err).with_context(|| {
format!(
"Failed to discover parent workspace; use `{}` to ignore",
"uv init --no-workspace".green()
)
});
}
}
}

View file

@ -1,7 +1,8 @@
use crate::commands::ExitStatus;
use tokio::process::Child;
use tracing::debug;
use crate::commands::ExitStatus;
/// Wait for the child process to complete, handling signals and error codes.
///
/// Note that this registers handles to ignore some signals in the parent process. This is safe as
@ -39,6 +40,7 @@ pub(crate) async fn run_to_completion(mut handle: Child) -> anyhow::Result<ExitS
let status = {
use std::ops::Deref;
use anyhow::Context;
use nix::sys::signal;
use nix::unistd::{getpgid, Pid};
use tokio::select;
@ -85,7 +87,7 @@ pub(crate) async fn run_to_completion(mut handle: Child) -> anyhow::Result<ExitS
}
// Get the parent PGID
let parent_pgid = getpgid(None)?;
let parent_pgid = getpgid(None).context("Failed to get current PID")?;
if let Some(child_pid) = *ChildPid::from(&handle) {
debug!("Spawned child {child_pid} in process group {parent_pgid}");
}
@ -147,7 +149,7 @@ pub(crate) async fn run_to_completion(mut handle: Child) -> anyhow::Result<ExitS
};
// Check if the child pgid has changed
let child_pgid = getpgid(Some(child_pid))?;
let child_pgid = getpgid(Some(child_pid)).context("Failed to get PID of child process")?;
// Increment the number of interrupts seen
sigint_count += 1;

View file

@ -357,7 +357,8 @@ pub(crate) async fn run(
.iter()
.flat_map(std::env::split_paths),
),
)?;
)
.context("Failed to build new PATH variable")?;
process.env(EnvVars::PATH, new_path);
// Spawn and wait for completion