From 94791dcd67f4fe0b756dc7d629a1d762856a1592 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 15 Aug 2026 07:00:10 +0000 Subject: [PATCH] Keep apt gi by pinning Linux setup to system Python. uv sync --active was honoring .python-version (3.12) and replacing the system-site-packages venv, so python3-gi from Ubuntu's interpreter was no longer importable. Co-authored-by: Diaco Soltanpour --- desktop/setup_linux.sh | 41 ++++++++++++++++++++++++++++++++++++++- tests/test_setup_linux.py | 18 +++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/test_setup_linux.py diff --git a/desktop/setup_linux.sh b/desktop/setup_linux.sh index 2ceb347..2bbf9e8 100755 --- a/desktop/setup_linux.sh +++ b/desktop/setup_linux.sh @@ -5,6 +5,8 @@ set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$ROOT" +SYS_PY=/usr/bin/python3 + if ! command -v uv >/dev/null 2>&1; then echo "uv not found on PATH. Install uv first: https://docs.astral.sh/uv/" >&2 exit 1 @@ -16,6 +18,11 @@ if ! command -v apt-get >/dev/null 2>&1; then exit 1 fi +if [[ ! -x "$SYS_PY" ]]; then + echo "System Python not found at $SYS_PY" >&2 + exit 1 +fi + packages=(python3-gi gir1.2-gtk-3.0) if apt-cache show gir1.2-webkit2-4.1 >/dev/null 2>&1; then packages+=(gir1.2-webkit2-4.1) @@ -41,6 +48,23 @@ else echo "GTK/WebKitGTK requirements already installed." fi +if ! "$SYS_PY" -c 'import gi' >/dev/null 2>&1; then + echo "System $SYS_PY cannot import gi. Install python3-gi." >&2 + exit 1 +fi + +enable_system_site_packages() { + local cfg=.venv/pyvenv.cfg + if [[ ! -f "$cfg" ]]; then + return 1 + fi + if grep -q '^include-system-site-packages' "$cfg"; then + sed -i 's/^include-system-site-packages = .*/include-system-site-packages = true/' "$cfg" + else + printf '\ninclude-system-site-packages = true\n' >> "$cfg" + fi +} + if [[ ! -x .venv/bin/python ]] || ! .venv/bin/python -c 'import gi' >/dev/null 2>&1; then echo "Creating venv with the system Python and system site packages..." rm -rf .venv @@ -48,7 +72,22 @@ if [[ ! -x .venv/bin/python ]] || ! .venv/bin/python -c 'import gi' >/dev/null 2 fi echo "Syncing dependencies..." -uv sync --active +# Pin the same interpreter so uv does not honor .python-version (3.12) and +# recreate a managed venv without apt gi (python3-gi). +uv sync --python /usr/bin/python3 --no-managed-python --python-preference only-system + +if ! .venv/bin/python -c 'import gi' >/dev/null 2>&1; then + echo "Restoring system site packages on .venv so apt gi is importable..." + enable_system_site_packages +fi + +venv_ver="$(.venv/bin/python -c 'import sys; print("%d.%d" % sys.version_info[:2])')" +sys_ver="$("$SYS_PY" -c 'import sys; print("%d.%d" % sys.version_info[:2])')" +if [[ "$venv_ver" != "$sys_ver" ]]; then + echo "venv Python $venv_ver does not match system Python $sys_ver at $SYS_PY." >&2 + echo "uv likely honored .python-version instead of the system interpreter." >&2 + exit 1 +fi echo "Verifying GTK and WebKit2 bindings..." .venv/bin/python -c ' diff --git a/tests/test_setup_linux.py b/tests/test_setup_linux.py new file mode 100644 index 0000000..318ef4b --- /dev/null +++ b/tests/test_setup_linux.py @@ -0,0 +1,18 @@ +from pathlib import Path + + +def test_setup_linux_pins_system_python_for_uv_sync(): + script = Path("desktop/setup_linux.sh").read_text(encoding="utf-8") + + assert "uv venv --python /usr/bin/python3 --no-managed-python --system-site-packages" in script + + sync_lines = [ + line.strip() + for line in script.splitlines() + if line.strip().startswith("uv sync") + ] + assert sync_lines == [ + "uv sync --python /usr/bin/python3 --no-managed-python --python-preference only-system" + ] + assert "uv sync --active" not in script + assert "include-system-site-packages = true" in script