From 88e5992cf4a781d7384b2afae07f515dad56c369 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Sun, 18 Oct 2026 17:46:01 +0000 Subject: [PATCH] fix(microsandbox): get uv with the image's Python, not the pip-less virtualenv An install made before uvx was needed had uv and the backend's virtualenv in the volume but no uvx, so run.sh fetched uv again with python3, which PATH resolved to the virtualenv's (no pip): "No module named pip" at "Getting uv". uv.sh now asks the image's Python (sys._base_executable), whatever is first in PATH; test-uv.sh reproduces it with a pip-less venv. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01JvRqz2gFGRwwZc4qAfj74Q --- changelog.d/microsandbox-uv-pip.md | 3 +++ microsandbox/project.json | 7 ++--- microsandbox/run.sh | 11 ++++---- microsandbox/test-uv.sh | 43 ++++++++++++++++++++++++++++++ microsandbox/uv.sh | 21 +++++++++++++++ 5 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 changelog.d/microsandbox-uv-pip.md create mode 100755 microsandbox/test-uv.sh create mode 100644 microsandbox/uv.sh diff --git a/changelog.d/microsandbox-uv-pip.md b/changelog.d/microsandbox-uv-pip.md new file mode 100644 index 000000000..bf755560a --- /dev/null +++ b/changelog.d/microsandbox-uv-pip.md @@ -0,0 +1,3 @@ +**Reinstalling in a microsandbox no longer stops at "Getting uv".** A sandbox whose volume an earlier install had +built in asked the backend's virtualenv, which has no pip, for uv and failed with "No module named pip". It now asks +the image's own Python. Rerun the install line to pick it up. diff --git a/microsandbox/project.json b/microsandbox/project.json index beb2a3f9f..97e3de28a 100644 --- a/microsandbox/project.json +++ b/microsandbox/project.json @@ -9,8 +9,8 @@ "options": { "cwd": "{projectRoot}", "commands": [ - "sh -n run.sh && sh -n dns.sh && sh -n test-dns.sh && sh -n programs.sh && sh -n test-programs.sh && sh -n wipe.sh && sh -n test-wipe.sh", - "uvx --from shellcheck-py shellcheck -s sh -x run.sh dns.sh test-dns.sh programs.sh test-programs.sh wipe.sh test-wipe.sh" + "sh -n run.sh && sh -n dns.sh && sh -n test-dns.sh && sh -n programs.sh && sh -n test-programs.sh && sh -n wipe.sh && sh -n test-wipe.sh && sh -n uv.sh && sh -n test-uv.sh", + "uvx --from shellcheck-py shellcheck -s sh -x run.sh dns.sh test-dns.sh programs.sh test-programs.sh wipe.sh test-wipe.sh uv.sh test-uv.sh" ], "parallel": false } @@ -22,7 +22,8 @@ "commands": [ "docker run --rm -v \"$PWD:/lens:ro\" mirror.gcr.io/library/python:3.12-slim-trixie sh /lens/microsandbox/test-dns.sh", "docker run --rm -v \"$PWD:/lens:ro\" mirror.gcr.io/library/python:3.12-slim-trixie sh /lens/microsandbox/test-programs.sh", - "docker run --rm -v \"$PWD:/lens:ro\" mirror.gcr.io/library/python:3.12-slim-trixie sh /lens/microsandbox/test-wipe.sh" + "docker run --rm -v \"$PWD:/lens:ro\" mirror.gcr.io/library/python:3.12-slim-trixie sh /lens/microsandbox/test-wipe.sh", + "docker run --rm -v \"$PWD:/lens:ro\" mirror.gcr.io/library/python:3.12-slim-trixie sh /lens/microsandbox/test-uv.sh" ], "parallel": false } diff --git a/microsandbox/run.sh b/microsandbox/run.sh index 694cb6abd..ecb5e0f55 100755 --- a/microsandbox/run.sh +++ b/microsandbox/run.sh @@ -108,11 +108,10 @@ if [ ! -x "$OPT/node/bin/node" ]; then tar -xJf "/tmp/lens/$file" -C "$OPT/node" --strip-components 1 rm "/tmp/lens/$file" fi -if [ ! -x "$OPT/bin/uv" ] || [ ! -x "$OPT/bin/uvx" ]; then # uvx: the MCP runner's, for servers from PyPI - log "Getting uv..." - python3 -m pip install -q --no-cache-dir --target /tmp/lens/uv "uv>=0.8,<0.9" - cp /tmp/lens/uv/bin/uv /tmp/lens/uv/bin/uvx "$OPT/bin/" && rm -rf /tmp/lens/uv -fi +# uv, and uvx: the MCP runner's, for servers from PyPI +# shellcheck source=SCRIPTDIR/uv.sh +. "$CODE/microsandbox/uv.sh" +get_uv "$OPT/bin" # --- Build: the backend and the web app, again only when the code changed (the edition doesn't change them) ------- build_id="$(cat "$CODE/.lens-build" 2>/dev/null || echo unknown)" @@ -136,7 +135,7 @@ if [ "$(cat "$OPT/build-id" 2>/dev/null)" != "$build_id" ]; then (cd "$OPT/backend" && export UV_PROJECT_ENVIRONMENT="$OPT/venv" UV_PYTHON_DOWNLOADS=never UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy \ UV_HTTP_TIMEOUT=300 UV_HTTP_RETRIES=5 UV_CACHE_DIR="$OPT/uv-cache" && - retry uv sync --frozen --no-dev --python "$(command -v python3)" \ + retry uv sync --frozen --no-dev --python "$(image_python)" \ $(for e in ${EXTRAS:-}; do printf -- '--extra %s ' "$e"; done)) web() { (cd "$WEB_SRC" && exec env COREPACK_ENABLE_DOWNLOAD_PROMPT=0 COREPACK_HOME="$OPT/corepack" "$@"); } diff --git a/microsandbox/test-uv.sh b/microsandbox/test-uv.sh new file mode 100755 index 000000000..91ebdca54 --- /dev/null +++ b/microsandbox/test-uv.sh @@ -0,0 +1,43 @@ +#!/bin/sh +# Tests uv.sh: getting uv and uvx with the backend's virtualenv, which has no pip, first in PATH, as on a sandbox whose +# volume an earlier install built in. Run it in the image run.sh runs in: +# +# docker run --rm -v "$PWD:/lens:ro" python:3.12-slim-trixie sh /lens/microsandbox/test-uv.sh +set -eu + +here="$(cd "$(dirname "$0")" && pwd)" +work="$(mktemp -d)" +trap 'rm -rf "$work"' EXIT + +fail() { + printf 'FAIL: %s\n' "$*" >&2 + exit 1 +} +ok() { printf 'ok: %s\n' "$*"; } +logged="" +log() { logged="$logged$*;"; } + +# shellcheck source=SCRIPTDIR/uv.sh +. "$here/uv.sh" +mkdir -p /tmp/lens "$work/bin" + +# the virtualenv a build made, without pip, first in PATH as run.sh puts it +python3 -m venv --without-pip "$work/venv" +PATH="$work/bin:$work/venv/bin:$PATH" +[ "$(command -v python3)" = "$work/venv/bin/python3" ] || fail "the virtualenv's python3 isn't first in PATH" +if python3 -m pip --version >/dev/null 2>&1; then fail "the virtualenv has pip, so this tests nothing"; fi +"$(image_python)" -m pip --version >/dev/null || fail "image_python names $(image_python), which has no pip either" +ok "image_python names a Python with pip ($(image_python)) with the virtualenv first in PATH" + +# an install from before uvx was needed: uv, no uvx +printf '#!/bin/sh\n' >"$work/bin/uv" && chmod +x "$work/bin/uv" +get_uv "$work/bin" || fail "get_uv failed with the virtualenv first in PATH" +"$work/bin/uv" --version >/dev/null || fail "uv doesn't run" +"$work/bin/uvx" --version >/dev/null || fail "uvx doesn't run" +case "$logged" in *"Getting uv..."*) ;; *) fail "get_uv didn't say what it was doing" ;; esac +ok "get_uv got uv and uvx where uvx was missing" + +logged="" +get_uv "$work/bin" || fail "get_uv failed with both there" +[ -z "$logged" ] || fail "get_uv downloaded again with both there" +ok "get_uv keeps uv and uvx that are there" diff --git a/microsandbox/uv.sh b/microsandbox/uv.sh new file mode 100644 index 000000000..a5083c8ff --- /dev/null +++ b/microsandbox/uv.sh @@ -0,0 +1,21 @@ +# uv and uvx in the sandbox (docs/microsandbox.md), sourced by run.sh, which defines log() and fetch(). test-uv.sh tests +# it. +# +# uv comes from PyPI through the image's pip. The backend's virtualenv, which uv made without pip, is first in PATH +# once a build has made it, so its python3 can't install anything ("No module named pip"): an install made before uvx +# was needed has uv and the virtualenv but not uvx, and got stuck at "Getting uv". image_python names the image's own. + +# image_python: the Python the image ships, the same with the virtualenv in PATH or not +image_python() { + python3 -c 'import sys; print(getattr(sys, "_base_executable", "") or sys.executable)' +} + +# get_uv BIN: uv and uvx into BIN, unless both are there +get_uv() { + [ -x "$1/uv" ] && [ -x "$1/uvx" ] && return 0 + log "Getting uv..." + py="$(image_python)" + rm -rf /tmp/lens/uv + "$py" -m pip install -q --no-cache-dir --disable-pip-version-check --root-user-action=ignore --target /tmp/lens/uv "uv>=0.8,<0.9" && + cp /tmp/lens/uv/bin/uv /tmp/lens/uv/bin/uvx "$1/" && rm -rf /tmp/lens/uv +}