Skip to content
Merged
Changes from 2 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
183 changes: 181 additions & 2 deletions crates/uv-python/src/downloads.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt::Display;
use std::fmt::{self, Display};
use std::io::Read;
use std::path::{Path, PathBuf};
use std::pin::Pin;
Expand Down Expand Up @@ -51,7 +51,7 @@ use crate::managed::ManagedPythonInstallation;
use crate::python_version::{BuildVersionError, python_build_version_from_env};
use crate::{Interpreter, PythonRequest, PythonVersion, VersionRequest};

#[derive(Error, Debug)]
#[derive(Error)]
pub enum Error {
#[error(transparent)]
Io(#[from] io::Error),
Expand Down Expand Up @@ -142,6 +142,145 @@ pub enum Error {
SystemTime(#[from] SystemTimeError),
}

impl fmt::Debug for Error {
// Keep the large formatter shared by references and boxed download errors.
#[inline(never)]
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must say I am a bit opposed to implementing Debug manually for this purpose... it feels wrong, like we should be fixing this in the compiler or something

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is actually intentional behavior on Rust's part: #[derive] currently implies #[inline] since in most cases the inline hint is correct (e.g. for clones):

https://doc.rust-lang.org/reference/attributes/derive.html#the-automatically_derived-attribute

(Our case is pathological, most Debug implementations aren't this large.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When are we using the Debug impl? Do we actually need it? Why is it so large?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just don't see this code as worth maintaining for 150KB of savings...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a cleaner solution with our own proc macro, working on it now. That approach will be repeatable as well for whenever we run into large inlinings.

As for why it's so large: we have 10+ variants and many have large nested errors, and the current Debug impl for all of that gets duplicated liberally by inlining. We could probably trim a bit from the size by trimming the variants, but the multiplier is fundamentally big thanks to the inlining.

Another option here would be to effectively delete Debug entirely, and just forward its impl to Display. That feels pretty hacky to me, and I'm not 100% sure it would actually save instructions. I'll see...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(We use the Debug impl in trace logging, where we show the entire nested error context. That seems valuable to keep.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm personally okay with it but could go either way. It would be nice if we had a more general solution to this, though?

Yeah, working on this with uv-macros!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dunno we only show the debug repr in trace logging to try to hunt down weird http retry issues lol so it's sort of questionable importance overall

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A general solution would satisfy me

match self {
Self::Io(error) => formatter.debug_tuple("Io").field(error).finish(),
Self::ImplementationError(error) => formatter
.debug_tuple("ImplementationError")
.field(error)
.finish(),
Self::MissingExtension(url, error) => formatter
.debug_tuple("MissingExtension")
.field(url)
.field(error)
.finish(),
Self::InvalidPythonVersion(version) => formatter
.debug_tuple("InvalidPythonVersion")
.field(version)
.finish(),
Self::EmptyRequest => formatter.write_str("EmptyRequest"),
Self::TooManyParts(request) => formatter
.debug_tuple("TooManyParts")
.field(request)
.finish(),
Self::NetworkError(url, error) => formatter
.debug_tuple("NetworkError")
.field(url)
.field(error)
.finish(),
Self::NetworkErrorWithRetries {
err,
retries,
duration,
} => formatter
.debug_struct("NetworkErrorWithRetries")
.field("err", err)
.field("retries", retries)
.field("duration", duration)
.finish(),
Self::NetworkMiddlewareError(url, error) => formatter
.debug_tuple("NetworkMiddlewareError")
.field(url)
.field(error)
.finish(),
Self::ExtractError(archive, error) => formatter
.debug_tuple("ExtractError")
.field(archive)
.field(error)
.finish(),
Self::HashExhaustion(error) => formatter
.debug_tuple("HashExhaustion")
.field(error)
.finish(),
Self::HashMismatch {
installation,
expected,
actual,
} => formatter
.debug_struct("HashMismatch")
.field("installation", installation)
.field("expected", expected)
.field("actual", actual)
.finish(),
Self::InvalidUrl(error) => formatter.debug_tuple("InvalidUrl").field(error).finish(),
Self::InvalidUrlFormat(url) => formatter
.debug_tuple("InvalidUrlFormat")
.field(url)
.finish(),
Self::InvalidFileUrl(url) => {
formatter.debug_tuple("InvalidFileUrl").field(url).finish()
}
Self::DownloadDirError(error) => formatter
.debug_tuple("DownloadDirError")
.field(error)
.finish(),
Self::CopyError { to, err } => formatter
.debug_struct("CopyError")
.field("to", to)
.field("err", err)
.finish(),
Self::ReadError { dir, err } => formatter
.debug_struct("ReadError")
.field("dir", dir)
.field("err", err)
.finish(),
Self::InvalidRequestPlatform(error) => formatter
.debug_tuple("InvalidRequestPlatform")
.field(error)
.finish(),
Self::NoDownloadFound(request) => formatter
.debug_tuple("NoDownloadFound")
.field(request)
.finish(),
Self::Mirror(variable, mirror) => formatter
.debug_tuple("Mirror")
.field(variable)
.field(mirror)
.finish(),
Self::LibcDetection(error) => {
formatter.debug_tuple("LibcDetection").field(error).finish()
}
Self::InvalidPythonDownloadsJSON(url, error) => formatter
.debug_tuple("InvalidPythonDownloadsJSON")
.field(url)
.field(error)
.finish(),
Self::UnsupportedPythonDownloadsJSON(url) => formatter
.debug_tuple("UnsupportedPythonDownloadsJSON")
.field(url)
.finish(),
Self::FetchingPythonDownloadsJSONError(url, error) => formatter
.debug_tuple("FetchingPythonDownloadsJSONError")
.field(url)
.field(error)
.finish(),
Self::RemotePythonDownloadsJSONClient(error) => formatter
.debug_tuple("RemotePythonDownloadsJSONClient")
.field(error)
.finish(),
Self::ClientBuild(error) => formatter.debug_tuple("ClientBuild").field(error).finish(),
Self::OfflinePythonMissing {
file,
url,
python_builds_dir,
} => formatter
.debug_struct("OfflinePythonMissing")
.field("file", file)
.field("url", url)
.field("python_builds_dir", python_builds_dir)
.finish(),
Self::BuildVersion(error) => {
formatter.debug_tuple("BuildVersion").field(error).finish()
}
Self::NoPythonDownloadUrlFound => formatter.write_str("NoPythonDownloadUrlFound"),
Self::SystemTime(error) => formatter.debug_tuple("SystemTime").field(error).finish(),
}
}
}

impl RetriableError for Error {
// Return the number of retries that were made to complete this request before this error was
// returned.
Expand Down Expand Up @@ -1890,6 +2029,46 @@ mod tests {

use super::*;

#[test]
fn test_download_error_debug() {
let errors = [
Error::EmptyRequest,
Error::Mirror("UV_PYTHON_INSTALL_MIRROR", "file:///mirror".to_owned()),
Error::NetworkErrorWithRetries {
err: Box::new(Error::InvalidPythonVersion("3.x".to_owned())),
retries: 2,
duration: Duration::from_secs(3),
},
Error::HashMismatch {
installation: "cpython-3.12.0-linux-x86_64-gnu".to_owned(),
expected: "abc".to_owned(),
actual: "def".to_owned(),
},
];

insta::assert_debug_snapshot!(errors, @r#"
[
EmptyRequest,
Mirror(
"UV_PYTHON_INSTALL_MIRROR",
"file:///mirror",
),
NetworkErrorWithRetries {
err: InvalidPythonVersion(
"3.x",
),
retries: 2,
duration: 3s,
},
HashMismatch {
installation: "cpython-3.12.0-linux-x86_64-gnu",
expected: "abc",
actual: "def",
},
]
"#);
}

/// Parse a request with all of its fields.
#[test]
fn test_python_download_request_from_str_complete() {
Expand Down
Loading
⌂ 换网址