Centralize uv command setup

Closes #4306
This commit is contained in:
Damien Elmes 2025-09-03 19:58:45 +10:00
parent 2491eb0316
commit db1d04f622

View file

@ -193,8 +193,8 @@ fn extract_aqt_version(state: &State) -> Option<String> {
return None; return None;
} }
let output = Command::new(&state.uv_path) let output = uv_command(state)
.current_dir(&state.uv_install_root) .ok()?
.env("VIRTUAL_ENV", &state.venv_folder) .env("VIRTUAL_ENV", &state.venv_folder)
.args(["pip", "show", "aqt"]) .args(["pip", "show", "aqt"])
.output() .output()
@ -262,15 +262,7 @@ fn handle_version_install_or_update(state: &State, choice: MainMenuChoice) -> Re
}; };
// Prepare to sync the venv // Prepare to sync the venv
let mut command = Command::new(&state.uv_path); let mut command = uv_command(state)?;
command.current_dir(&state.uv_install_root);
// remove UV_* environment variables to avoid interference
for (key, _) in std::env::vars() {
if key.starts_with("UV_") || key == "VIRTUAL_ENV" {
command.env_remove(key);
}
}
if cfg!(target_os = "macos") { if cfg!(target_os = "macos") {
// remove CONDA_PREFIX/bin from PATH to avoid conda interference // remove CONDA_PREFIX/bin from PATH to avoid conda interference
@ -316,13 +308,6 @@ fn handle_version_install_or_update(state: &State, choice: MainMenuChoice) -> Re
command.env("UV_NO_CACHE", "1"); command.env("UV_NO_CACHE", "1");
} }
// Add mirror environment variable if enabled
if let Some((python_mirror, pypi_mirror)) = get_mirror_urls(state)? {
command
.env("UV_PYTHON_INSTALL_MIRROR", &python_mirror)
.env("UV_DEFAULT_INDEX", &pypi_mirror);
}
match command.ensure_success() { match command.ensure_success() {
Ok(_) => { Ok(_) => {
// Sync succeeded // Sync succeeded
@ -672,9 +657,8 @@ fn filter_and_normalize_versions(
fn fetch_versions(state: &State) -> Result<Vec<String>> { fn fetch_versions(state: &State) -> Result<Vec<String>> {
let versions_script = state.resources_dir.join("versions.py"); let versions_script = state.resources_dir.join("versions.py");
let mut cmd = Command::new(&state.uv_path); let mut cmd = uv_command(state)?;
cmd.current_dir(&state.uv_install_root) cmd.args(["run", "--no-project", "--no-config", "--managed-python"])
.args(["run", "--no-project", "--no-config", "--managed-python"])
.args(["--with", "pip-system-certs,requests[socks]"]); .args(["--with", "pip-system-certs,requests[socks]"]);
let python_version = read_file(&state.dist_python_version_path)?; let python_version = read_file(&state.dist_python_version_path)?;
@ -687,12 +671,6 @@ fn fetch_versions(state: &State) -> Result<Vec<String>> {
cmd.arg(&versions_script); cmd.arg(&versions_script);
// Add mirror environment variable if enabled
if let Some((python_mirror, pypi_mirror)) = get_mirror_urls(state)? {
cmd.env("UV_PYTHON_INSTALL_MIRROR", &python_mirror)
.env("UV_DEFAULT_INDEX", &pypi_mirror);
}
let output = match cmd.utf8_output() { let output = match cmd.utf8_output() {
Ok(output) => output, Ok(output) => output,
Err(e) => { Err(e) => {
@ -937,6 +915,27 @@ fn handle_uninstall(state: &State) -> Result<bool> {
Ok(true) Ok(true)
} }
fn uv_command(state: &State) -> Result<Command> {
let mut command = Command::new(&state.uv_path);
command.current_dir(&state.uv_install_root);
// remove UV_* environment variables to avoid interference
for (key, _) in std::env::vars() {
if key.starts_with("UV_") || key == "VIRTUAL_ENV" {
command.env_remove(key);
}
}
// Add mirror environment variable if enabled
if let Some((python_mirror, pypi_mirror)) = get_mirror_urls(state)? {
command
.env("UV_PYTHON_INSTALL_MIRROR", &python_mirror)
.env("UV_DEFAULT_INDEX", &pypi_mirror);
}
Ok(command)
}
fn build_python_command(state: &State, args: &[String]) -> Result<Command> { fn build_python_command(state: &State, args: &[String]) -> Result<Command> {
let python_exe = if cfg!(target_os = "windows") { let python_exe = if cfg!(target_os = "windows") {
let show_console = std::env::var("ANKI_CONSOLE").is_ok(); let show_console = std::env::var("ANKI_CONSOLE").is_ok();