#![cfg_attr(windows, windows_subsystem = "windows")]
use std::convert::Infallible;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitCode, ExitStatus};
fn exec_spawn(cmd: &mut Command) -> std::io::Result<Infallible> {
#[cfg(unix)]
{
use std::os::unix::process::CommandExt;
let err = cmd.exec();
Err(err)
}
#[cfg(windows)]
{
uv_windows::spawn_child(cmd, true)
}
}
fn get_uvw_suffix(current_exe: &Path) -> Option<&str> {
let os_file_name = current_exe.file_name()?;
let file_name_str = os_file_name.to_str()?;
file_name_str.strip_prefix("uvw")
}
fn get_uv_path(current_exe_parent: &Path, uvw_suffix: Option<&str>) -> std::io::Result<PathBuf> {
let uv_with_suffix = uvw_suffix.map(|suffix| current_exe_parent.join(format!("uv{suffix}")));
if let Some(uv_with_suffix) = &uv_with_suffix {
#[expect(clippy::print_stderr, reason = "printing a very rare warning")]
match uv_with_suffix.try_exists() {
Ok(true) => return Ok(uv_with_suffix.to_owned()),
Ok(false) => { }
Err(err) => {
eprintln!(
"warning: failed to determine if `{}` exists, trying `uv` instead: {err}",
uv_with_suffix.display()
);
}
}
}
let uv = current_exe_parent.join(format!("uv{}", std::env::consts::EXE_SUFFIX));
if matches!(uv.try_exists(), Ok(false)) {
let message = if let Some(uv_with_suffix) = uv_with_suffix {
format!(
"Could not find the `uv` binary at either of:\n {}\n {}",
uv_with_suffix.display(),
uv.display(),
)
} else {
format!("Could not find the `uv` binary at: {}", uv.display())
};
Err(std::io::Error::new(std::io::ErrorKind::NotFound, message))
} else {
Ok(uv)
}
}
fn run() -> std::io::Result<ExitStatus> {
let current_exe = std::env::current_exe()?;
let Some(bin) = current_exe.parent() else {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Could not determine the location of the `uvw` binary",
));
};
let uvw_suffix = get_uvw_suffix(¤t_exe);
let uv = get_uv_path(bin, uvw_suffix)?;
let args = std::env::args_os()
.skip(1)
.collect::<Vec<_>>();
let mut cmd = Command::new(uv);
cmd.args(&args);
match exec_spawn(&mut cmd)? {}
}
#[expect(clippy::print_stderr)]
fn main() -> ExitCode {
let result = run();
match result {
Ok(status) => u8::try_from(status.code().unwrap_or(2)).unwrap_or(2).into(),
Err(err) => {
eprintln!("error: {err}");
ExitCode::from(2)
}
}
}