From fc6447a938798ccf90c51485d1c2135bf58364fd Mon Sep 17 00:00:00 2001 From: Kevin Nakamura Date: Fri, 4 Jul 2025 16:35:50 +0000 Subject: [PATCH] Launcher: Run `uv python install` before running `uv sync` (#4162) * Launcher: Run `uv python install` before running `uv sync` * Less copy/paste. * Minor readability improvements * Make sure we check file presence before attempting to read --------- Co-authored-by: Damien Elmes --- CONTRIBUTORS | 1 + ftl/core-repo | 2 +- ftl/qt-repo | 2 +- qt/launcher/src/main.rs | 41 +++++++++++++++++++++++++++++++++-------- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index fc3bc44e6..327e37f27 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -233,6 +233,7 @@ Spiritual Father Emmanuel Ferdman Sunong2008 Marvin Kopf +Kevin Nakamura ******************** The text of the 3 clause BSD license follows: diff --git a/ftl/core-repo b/ftl/core-repo index 8e3a5b5c8..4a65d6012 160000 --- a/ftl/core-repo +++ b/ftl/core-repo @@ -1 +1 @@ -Subproject commit 8e3a5b5c841c512e5139daea0a9f0e2abdc47b8e +Subproject commit 4a65d6012ac022a35f5c80c80b2b665447b6a525 diff --git a/ftl/qt-repo b/ftl/qt-repo index 324e39e5b..f42461a64 160000 --- a/ftl/qt-repo +++ b/ftl/qt-repo @@ -1 +1 @@ -Subproject commit 324e39e5bc7fa8311242c48104a1552d57d6ecfd +Subproject commit f42461a6438cbe844150f543128d79a669bc4ef2 diff --git a/qt/launcher/src/main.rs b/qt/launcher/src/main.rs index 629fbd881..2f4648f5b 100644 --- a/qt/launcher/src/main.rs +++ b/qt/launcher/src/main.rs @@ -284,6 +284,37 @@ fn main_menu_loop(state: &State) -> Result<()> { // Remove sync marker before attempting sync let _ = remove_file(&state.sync_complete_marker); + println!("\x1B[1mUpdating Anki...\x1B[0m\n"); + + let python_version_trimmed = if state.user_python_version_path.exists() { + let python_version = read_file(&state.user_python_version_path)?; + let python_version_str = String::from_utf8(python_version) + .context("Invalid UTF-8 in .python-version")?; + Some(python_version_str.trim().to_string()) + } else { + None + }; + + // `uv sync` does not pull in Python automatically, unlike `uv run`. + // This might be system/platform specific and/or a uv bug. + let mut command = Command::new(&state.uv_path); + command + .current_dir(&state.uv_install_root) + .env("UV_CACHE_DIR", &state.uv_cache_dir) + .env("UV_PYTHON_INSTALL_DIR", &state.uv_python_install_dir) + .args(["python", "install"]); + + // Add python version if .python-version file exists + if let Some(version) = &python_version_trimmed { + command.args([version]); + } + + if let Err(e) = command.ensure_success() { + println!("Python install failed: {e:#}"); + println!(); + continue; + } + // Sync the venv let mut command = Command::new(&state.uv_path); command @@ -293,12 +324,8 @@ fn main_menu_loop(state: &State) -> Result<()> { .args(["sync", "--upgrade", "--managed-python"]); // Add python version if .python-version file exists - if state.user_python_version_path.exists() { - let python_version = read_file(&state.user_python_version_path)?; - let python_version_str = String::from_utf8(python_version) - .context("Invalid UTF-8 in .python-version")?; - let python_version_trimmed = python_version_str.trim(); - command.args(["--python", python_version_trimmed]); + if let Some(version) = &python_version_trimmed { + command.args(["--python", version]); } // Set UV_PRERELEASE=allow if beta mode is enabled @@ -310,8 +337,6 @@ fn main_menu_loop(state: &State) -> Result<()> { command.env("UV_NO_CACHE", "1"); } - println!("\x1B[1mUpdating Anki...\x1B[0m\n"); - match command.ensure_success() { Ok(_) => { // Sync succeeded