Repository: astral-sh/uv
File: crates/uv/src/commands/build_frontend.rs (Rust)

Code before:
                }
            }
            Err(err) => {
                #[derive(Debug, miette::Diagnostic, thiserror::Error)]
                #[error("Failed to build `{source}`", source = source.cyan())]
                #[diagnostic()]
                struct Diagnostic {
                    source: String,
                    #[source]
                    cause: anyhow::Error,
                    #[help]
                    help: Option<String>,
                }

                let help = if let Error::Extract(uv_extract::Error::Tar(err)) = &err {
                    // TODO(konsti): astral-tokio-tar should use a proper error instead of
                    // encoding everything in strings
                    if err.to_string().contains("/bin/python")
                        && std::error::Error::source(err).is_some_and(|err| {
                            let err = err.to_string();
                            err.ends_with("outside of the target directory")
                                || err.ends_with("external symlinks are not allowed")
                        })
                    {
                        Some(
                            "This file seems to be part of a virtual environment. Virtual environments must be excluded from source distributions."
                                .to_string(),
                        )
                    } else {
                        None
                    }
                } else {
                    None
                };

                let report = miette::Report::new(Diagnostic {
                    source: source.to_string(),
                    cause: err.into(),
                    help,
                });
                anstream::eprint!("{report:?}");

                success = false;
            }
        }
    }

    if success {
        Ok(BuildResult::Success)
    } else {
        Ok(BuildResult::Failure)

Review comment (suggestion):
Note: both errors are still possible here, but we probably really only expect the first (absent an intentionally malicious embedded venv). I see @konstin has already hit the pain of insufficient error typing here 😅 

Code after:
                }
            }
            Err(err) => {
                #[derive(Debug, miette::Diagnostic, thiserror::Error)]
                #[error("Failed to build `{source}`", source = source.cyan())]
                #[diagnostic()]
                struct Diagnostic {
                    source: String,
                    #[source]
                    cause: anyhow::Error,
                    #[help]
                    help: Option<String>,
                }

                let help = if let Error::Extract(uv_extract::Error::Tar(err)) = &err {
                    // TODO(konsti): astral-tokio-tar should use a proper error instead of
                    // encoding everything in strings
                    // NOTE(ww): We check for both messages below because the both indicate
                    // different external extraction scenarios; the first is for any
                    // absolute path outside of the target directory, and the second
                    // is specifically for symlinks that point outside.
                    if err.to_string().contains("/bin/python")
                        && std::error::Error::source(err).is_some_and(|err| {
                            let err = err.to_string();
                            err.ends_with("outside of the target directory")
                                || err.ends_with("external symlinks are not allowed")
                        })
                    {
                        Some(
                            "This file seems to be part of a virtual environment. Virtual environments must be excluded from source distributions."
                                .to_string(),
                        )
                    } else {
                        None
                    }
                } else {
                    None
                };

                let report = miette::Report::new(Diagnostic {
                    source: source.to_string(),
                    cause: err.into(),
                    help,
                });
                anstream::eprint!("{report:?}");

                success = false;
            }
        }
    }