From 86a0d1d325797678cb1b80d4d11b936f757375ca Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Fri, 13 Nov 2026 08:47:24 -0600 Subject: [PATCH 01/12] PYTHON-6273 Let Dependabot fully manage uv.lock updates --- .evergreen/scripts/configure-env.sh | 9 +++-- .evergreen/scripts/install-dependencies.sh | 7 ++-- .evergreen/scripts/setup-dev-env.sh | 2 +- .evergreen/scripts/setup_uv.py | 17 +++++--- .github/dependabot.yml | 17 ++++++-- .github/workflows/uv-lock-update.yml | 42 ------------------- AGENTS.md | 3 ++ CONTRIBUTING.md | 47 +++++++++++++--------- fuzz/README.md | 6 +-- pyproject.toml | 12 +----- uv.lock | 26 +++++++++--- 11 files changed, 89 insertions(+), 99 deletions(-) delete mode 100644 .github/workflows/uv-lock-update.yml diff --git a/.evergreen/scripts/configure-env.sh b/.evergreen/scripts/configure-env.sh index 360706a944..e84be3b24e 100755 --- a/.evergreen/scripts/configure-env.sh +++ b/.evergreen/scripts/configure-env.sh @@ -39,8 +39,8 @@ fi # Add the latest MongoDB toolchain bin dir to PATH if it exists, so that hosts # with an old system Python (e.g. RHEL8's 3.6) still get a modern interpreter # for tool installs like `uv tool install rust-just`. It goes after -# PYMONGO_BIN_DIR so the pinned uv (installed there by setup_uv.py) takes -# precedence over the toolchain's uv. +# PYMONGO_BIN_DIR so the uv installed there by setup_uv.py takes precedence +# over the toolchain's uv. if [ "Windows_NT" = "${OS:-}" ]; then _toolchain_bin="/cygdrive/c/Python/Current/Scripts" elif [ "$(uname -s)" == "Darwin" ]; then @@ -106,8 +106,9 @@ if [ "$DRIVERS_TOOLS" = "$PROJECT_DIRECTORY/drivers-evergreen-tools" ]; then # Write a uv config boundary into the submodule: it is vendored inside this # project, so uv run by the tools' own scripts would otherwise walk up to - # pyproject.toml and enforce our required-version pin. Write-if-absent, so an - # upstream uv.toml fails the submodule update loudly instead of being clobbered. + # pyproject.toml and apply this project's [tool.uv] settings. + # Write-if-absent, so an upstream uv.toml fails the submodule update loudly + # instead of being clobbered. if [ -d "${DRIVERS_TOOLS}" ] && [ ! -f "${DRIVERS_TOOLS}/uv.toml" ]; then cat < "${DRIVERS_TOOLS}/uv.toml" # Written by mongo-python-driver to stop uv's config discovery here; see diff --git a/.evergreen/scripts/install-dependencies.sh b/.evergreen/scripts/install-dependencies.sh index 5c69b8875e..0a4e5791a6 100755 --- a/.evergreen/scripts/install-dependencies.sh +++ b/.evergreen/scripts/install-dependencies.sh @@ -35,7 +35,7 @@ fi export UV_TOOL_BIN_DIR="${UV_TOOL_BIN_DIR:-$PYMONGO_BIN_DIR}" # Ensure the bin dir is on PATH: hosts without env.sh (e.g. auth-aws-ecs) never -# export it, so a fresh pinned install there would be invisible to the probe +# export it, so a fresh uv install there would be invisible to the probe # below and to later steps like `uv tool install` and `uv sync`. case ":$PATH:" in *":$PYMONGO_BIN_DIR_POSIX:"*) ;; @@ -48,8 +48,9 @@ if [ "Windows_NT" = "${OS:-}" ]; then _uv_setup_script="$(cygpath -m "$_uv_setup_script")" fi -# Skip setup when the pinned uv is already installed: `setup_uv.py --check` -# compares the uv on PATH with the required-version pin, without side effects. +# Skip setup when a suitable uv is already installed: `setup_uv.py --check` +# verifies the required-version pin when set, else that any uv is on PATH, +# without side effects. # A full `uv sync` here would download Python and install/build dependencies # just to make this decision, and would run before setup-uv-python.sh sets # UV_PYTHON, creating the environment twice on local dev. diff --git a/.evergreen/scripts/setup-dev-env.sh b/.evergreen/scripts/setup-dev-env.sh index d9fbef2b04..a3b9b24f5b 100755 --- a/.evergreen/scripts/setup-dev-env.sh +++ b/.evergreen/scripts/setup-dev-env.sh @@ -16,7 +16,7 @@ if [ -f $HERE/test-env.sh ]; then . $HERE/test-env.sh fi -# The bin dir for the pinned uv/just. setup-system.sh sets it on evergreen hosts; +# The bin dir for uv/just. setup-system.sh sets it on evergreen hosts; # default it here so local dev (without setup-system.sh) also has a usable value. # Native (Windows) form on cygwin, like install-dependencies.sh. export PYMONGO_BIN_DIR="${PYMONGO_BIN_DIR:-$HOME/.local/bin}" diff --git a/.evergreen/scripts/setup_uv.py b/.evergreen/scripts/setup_uv.py index a3f5f9dc20..d99ccca645 100755 --- a/.evergreen/scripts/setup_uv.py +++ b/.evergreen/scripts/setup_uv.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 -"""Bootstrap the pinned uv/just for the test environment. +"""Bootstrap uv/just for the test environment, pinning uv only when +[tool.uv] required-version is set in pyproject.toml. install-dependencies.sh probes with `setup_uv.py --check`, sources ensure-uv.sh (which finds or installs uv), then runs this script for the rest. Only the @@ -26,7 +27,7 @@ def required_uv_pin() -> str: - """Return [tool.uv] required-version, e.g. '==0.12.12' ('' if absent).""" + """Return [tool.uv] required-version, e.g. '==1.2.3' ('' if absent).""" pattern = re.compile(r"required-version\s*=\s*['\"]?([^'\"\s]+)['\"]?") in_uv = False for line in (ROOT / "pyproject.toml").read_text().splitlines(): @@ -133,9 +134,15 @@ def _write_env() -> None: ENV_SH.write_bytes(("\n".join(keep) + "\n").encode()) -def _pinned_uv_installed() -> bool: - """Return True when the uv on PATH already matches the required-version pin.""" +def _required_uv_installed() -> bool: + """Return True when the uv on PATH already satisfies the setup requirement. + + With a required-version pin, the uv on PATH must match it exactly; without + a pin, any uv on PATH is acceptable. + """ pin = required_uv_pin() + if not pin: + return shutil.which("uv") is not None if not pin.startswith("=="): # Only exact pins are checked here; anything else falls back to setup. return False @@ -154,7 +161,7 @@ def main() -> int: # there would download Python and install dependencies just to decide # whether setup is needed. if "--check" in sys.argv[1:]: - return 0 if _pinned_uv_installed() else 1 + return 0 if _required_uv_installed() else 1 bin_dir = os.environ["UV_TOOL_BIN_DIR"] Path(bin_dir).mkdir(parents=True, exist_ok=True) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 8f82505fcb..c426ae42b4 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -11,14 +11,23 @@ updates: actions: patterns: - "*" - # Python (uv). Routine version updates are handled by the uv-lock-update - # workflow, so a limit of 0 turns off Dependabot version-update PRs here. - # Security updates are exempt from this limit and still open normally. + # Python (uv). Dependabot fully manages uv.lock: one grouped weekly pull + # request plus immediate, ungrouped security updates. Never set uv's + # exclude-newer in pyproject.toml instead: Dependabot resolves with it and + # produces pull requests that remove or downgrade packages + # (dependabot-core#14156). - package-ecosystem: "uv" directory: "/" schedule: interval: "weekly" - open-pull-requests-limit: 0 + cooldown: + default-days: 7 + allow: + - dependency-type: "all" + groups: + uv: + patterns: + - "*" # drivers-evergreen-tools submodule - package-ecosystem: "gitsubmodule" directory: "/" diff --git a/.github/workflows/uv-lock-update.yml b/.github/workflows/uv-lock-update.yml deleted file mode 100644 index 61744bbcb4..0000000000 --- a/.github/workflows/uv-lock-update.yml +++ /dev/null @@ -1,42 +0,0 @@ -name: uv Lock Update - -on: - schedule: - # Tuesdays at 12:00 UTC. - - cron: "0 12 * * 2" - workflow_dispatch: - inputs: - dry_run: - description: "Run without pushing or opening a pull request" - type: boolean - default: false - -permissions: - contents: read - # A live run uses the app token, but a dry run looks up the existing pull - # request with github.token, so that read has to be granted here. - pull-requests: read - -concurrency: - group: uv-lock-update - cancel-in-progress: false - -jobs: - update: - name: Update uv.lock - runs-on: ubuntu-latest - environment: automation - if: github.repository_owner == 'mongodb' || github.event_name == 'workflow_dispatch' - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - persist-credentials: false - - name: Install uv - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 - with: - python-version: "3.11" - - uses: mongodb-labs/drivers-github-tools/python/uv-lock-update@99b1cbd729bc83bf7fc74e1b2a97fffad3d65d7b # v3.1.1 - with: - app_id: ${{ vars.APP_ID }} - private_key: ${{ secrets.APP_PRIVATE_KEY }} - dry_run: ${{ inputs.dry_run || 'false' }} diff --git a/AGENTS.md b/AGENTS.md index 1018b63902..b11b8c09da 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -42,6 +42,9 @@ Async functions must not call blocking I/O. New features need integration tests. Bug fixes need regression tests. +## Dependency updates +Dependabot fully manages `uv.lock`: one grouped weekly pull request plus immediate security pull requests. Never set `[tool.uv] exclude-newer` in `pyproject.toml`; pin `required-version` only when a uv lockfile feature requires it. Details in CONTRIBUTING.md "Dependency updates". + ## Commit and PR Conventions - Prefix commits and PR titles with the JIRA ticket: `PYTHON-1234 Fix retryable write on mongos` - Backport PRs: append the target branch in brackets — `PYTHON-1234 [v4.9] Fix retryable write` diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9e0e058c94..415d71e9c8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -199,12 +199,13 @@ the pages will re-render and the browser will automatically refresh. create a virtual environment and run `pytest` directly. If you want to use a specific version of Python, set `UV_PYTHON` before running `just install`. - `just install` installs the pinned version of `uv` (from `[tool.uv] required-version`) into `$HOME/.local/bin`, - and adds that directory to your shell rc file when missing, so the pinned `uv` takes effect in new shells. If a - project `uv` command (e.g. `just test`) runs with a different `uv` version, `uv` fails fast and tells you how to update. - Scripts inside the vendored `drivers-evergreen-tools` submodule are exempt: the setup scripts write a `uv.toml` - boundary file into that checkout (see `.evergreen/scripts/configure-env.sh`), which stops uv's config discovery - there, since the tools' scripts run uv versions this project does not pin. + `just install` installs the latest `uv` into `$HOME/.local/bin` when no `uv` is on PATH + (no pin), and adds that directory to your shell rc file when missing, so `uv` takes effect in new shells. + The fail-fast version check only applies if a `required-version` pin is ever set under `[tool.uv]` in + `pyproject.toml`. Scripts inside the vendored `drivers-evergreen-tools` submodule are exempt from this + project's uv settings: the setup scripts write a `uv.toml` boundary file into that checkout (see + `.evergreen/scripts/configure-env.sh`), which stops uv's config discovery there, since the tools' scripts + run with their own uv settings. - Ensure you have started the appropriate Mongo Server(s). You can run `just run-server` with optional args to set up the server. All given options will be passed to [`run-mongodb.sh`](https://github.com/mongodb-labs/drivers-evergreen-tools/blob/master/.evergreen/run-mongodb.sh). Run `${DRIVERS_TOOLS:-drivers-evergreen-tools}/.evergreen/run-mongodb.sh start -h` @@ -484,13 +485,12 @@ CPython version whose toolchain dir exists, `UV_PYTHON_SEARCH_PATH` points uv at - The default if `UV_PYTHON` is not set is CPython 3.10. This is deterministic across host types, so a task that does not pin a version always gets the same Python (with the build headers (`Python.h`) needed to build the C extensions). - - The uv binary version is pinned once in `[tool.uv] required-version` in `pyproject.toml`. - `.evergreen/scripts/install-dependencies.sh` installs it with `uv tool install`, uv enforces it locally, and - `astral-sh/setup-uv` reads it on GitHub. Bump it manually when a newer uv is needed. If uv cannot find the - requested Python, it installs it; if that fails, the task fails. The pin is not enforced for scripts inside - the vendored `drivers-evergreen-tools` submodule: those scripts run uv versions this project does not pin, so - the setup scripts write a `uv.toml` boundary into the submodule checkout that stops uv's config discovery - (and with it the required-version check) at the submodule boundary. + - GitHub Actions use the latest uv (`astral-sh/setup-uv`'s default), and Evergreen ensures a uv via + `ensure-uv.sh` / `setup_uv.py`. A `[tool.uv] required-version` pin is added to `pyproject.toml` only when a + needed uv lockfile feature requires it. If uv cannot find the requested Python, it installs it; if that + fails, the task fails. The submodule boundary keeps this project's `[tool.uv]` settings from leaking into + the tools' scripts: the setup scripts write a `uv.toml` boundary into the submodule checkout that stops + uv's config discovery there. - Regenerate the test variants and tasks using `pre-commit run --all-files generate-config`. - PR tasks pin to the Python boundaries: `MIN_PYTHON` with `TEST_MIN_DEPS=1`, `MAX_PYTHON`, and one `FREE_THREADED_PYTHON`, against the newest numbered server (`MAX_VERSION`; the `latest` nightly @@ -657,10 +657,19 @@ From the spawn host or Ubuntu image, do the following: ## Dependency updates -Dependabot raises PRs at most once per week for GitHub Actions updates. For Python -dependencies it is limited to security updates, because routine upgrades are handled by -a scheduled workflow that runs weekly, running `uv lock --upgrade` and keeping a single -open pull request on the `uv-lock-update` branch. +Dependabot raises PRs at most once per week for GitHub Actions updates. Dependabot fully +manages `uv.lock`: one grouped weekly pull request for all Python dependencies (direct and +transitive) with a 7-day cooldown, plus immediate, ungrouped pull requests for security +updates, which the cooldown never delays. + +Never set `exclude-newer` under `[tool.uv]` in `pyproject.toml`: Dependabot resolves with +it and produces pull requests that remove or downgrade packages +([dependabot-core#14156](https://github.com/dependabot/dependabot-core/issues/14156)). To +pull a routine update forward past the cooldown, list the package under the cooldown's +`exclude` key in `.github/dependabot.yml`. + +Set `[tool.uv] required-version` only when a needed uv feature requires it, and keep it +satisfiable by the uv version Dependabot ships. If a typing dependency has changed, run `just typing` and handle any new findings. @@ -670,9 +679,7 @@ If a typing dependency has changed, run `just typing` and handle any new finding and in CI. Continuous integration runs `uv lock --check`, which fails when the lock file no longer matches `pyproject.toml`. -If that check fails on your pull request, regenerate the lock file and commit the result. -A 7 day cutoff (`exclude-newer` in `pyproject.toml`) applies automatically, so a package -version yanked shortly after release is less likely to land in the lock file: +If that check fails on your pull request, regenerate the lock file and commit the result: ```bash uv lock diff --git a/fuzz/README.md b/fuzz/README.md index 8069c0586c..1dfee074be 100644 --- a/fuzz/README.md +++ b/fuzz/README.md @@ -26,9 +26,9 @@ Atheris is a `uv` dependency group. Run targets from the repository root: uv run --group fuzz python fuzz/fuzz_decode.py fuzz/regression/* ``` -Atheris publishes CPython 3.12 to 3.14 wheels for Linux x86_64 only, so the -`fuzz` group cannot install on other interpreters or platforms. Pin a -compatible interpreter where one is available, and run in a Linux x86_64 +The latest atheris publishes CPython 3.12 to 3.14 wheels for Linux x86_64 +only, so the `fuzz` group may not install on other interpreters or platforms. +Pin a compatible interpreter where one is available, and run in a Linux x86_64 container otherwise: ```bash diff --git a/pyproject.toml b/pyproject.toml index 1d4023d14d..4ca3dc883c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,16 +44,6 @@ Documentation = "https://www.mongodb.com/docs/languages/python/pymongo-driver/cu Source = "https://github.com/mongodb/mongo-python-driver" Tracker = "https://jira.mongodb.org/projects/PYTHON/issues" -[tool.uv] -# Pin the uv binary version across local dev, GitHub Actions, and Evergreen. -# uv enforces this locally, astral-sh/setup-uv reads it on GitHub, and -# install-dependencies.sh installs it in Evergreen. Bump manually when a newer uv -# is needed. -required-version = "==0.12.12" -# Skip dependency releases newer than 7 days. Defining it here keeps every uv -# invocation on the same cutoff. -exclude-newer = "7 days" - [dependency-groups] dev = [] lint = [ @@ -71,7 +61,7 @@ coverage = [ mockupdb = [ "mockupdb>=1.8.1" ] -# Atheris publishes CPython 3.12-3.14 Linux x86_64 wheels only; see fuzz/README.md. +# The latest atheris publishes CPython 3.12-3.14 Linux x86_64 wheels only; see fuzz/README.md. fuzz = ["atheris>=2.3.0"] perf = ["simplejson>=3.17.0"] typing = [ diff --git a/uv.lock b/uv.lock index 01b4ceb7db..3736fc9132 100644 --- a/uv.lock +++ b/uv.lock @@ -1,15 +1,11 @@ version = 1 -revision = 3 +revision = 5 requires-python = ">=3.11" resolution-markers = [ "python_full_version >= '3.11.5'", "python_full_version < '3.11.5'", ] -[options] -exclude-newer = "0001-01-01T00:00:00Z" # This has no effect and is included for backwards compatibility when using relative exclude-newer values. -exclude-newer-span = "P7D" - [[package]] name = "accessible-pygments" version = "0.0.5" @@ -44,10 +40,27 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/a9/16/7757fb84c819accc7d7e9af5291b261e4abb8627ee040f99e7d35ef2457a/anyio-4.16.0-py3-none-any.whl", hash = "sha256:c66ae634e0b8228396b801741beb3294326d4b6102f7bba4041c0dfb58c29026", size = 134521, upload-time = "2026-11-01T11:20:05.59Z" }, ] +[[package]] +name = "atheris" +version = "3.0.0" +source = { registry = "https://pypi.org/simple" } +resolution-markers = [ + "python_full_version < '3.11.5'", +] +sdist = { url = "https://files.pythonhosted.org/packages/f8/58/5965955898e16bee17c8379eae12194993bf641c4629016991248b862069/atheris-3.0.0.tar.gz", hash = "sha256:1f0929c7bc3040f3fe4102e557718734190cf2d7718bbb8e3ce6d3eb56ef5bb3", size = 373239, upload-time = "2025-11-24T23:54:02.15Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/da/15/cf109e2e8696a54c8c4bc3ef79a79bec32361eceb64eaa36690a682e83a9/atheris-3.0.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:8a5c8a781467c187da40fd29139784193e2647058831f837f675d0bb8cbd8746", size = 34805555, upload-time = "2025-11-24T23:53:53.477Z" }, + { url = "https://files.pythonhosted.org/packages/85/8c/e9960b996e70e5f6a523670431166b2b238de52fef094955515dcf854da1/atheris-3.0.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:510e502c57b6dc615fb174066407af620d4c7f73cf08a782c86e7761bf12c4eb", size = 34907016, upload-time = "2025-11-24T23:53:56.535Z" }, + { url = "https://files.pythonhosted.org/packages/db/48/df670f75f458cc7c1752a01a394fd59c830b08172dd59cf29d73f31050f9/atheris-3.0.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:a402cdca8a650d1371050b1f9552eb4cdc488d2db64950d603c4560318365eac", size = 34858525, upload-time = "2025-11-24T23:53:59.925Z" }, +] + [[package]] name = "atheris" version = "3.1.0" source = { registry = "https://pypi.org/simple" } +resolution-markers = [ + "python_full_version >= '3.11.5'", +] wheels = [ { url = "https://files.pythonhosted.org/packages/0a/88/fd6ad595dafa9c7ce56dbfcaff0c7244988dac3af86c771166c6516ccf6b/atheris-3.1.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:ec5e11f21a4c197fe91f7aea2b2de88e623c73a21fc07b105ac6329a1588457b", size = 36875908, upload-time = "2026-06-17T00:04:01.104Z" }, { url = "https://files.pythonhosted.org/packages/4e/18/e19718c384fd7d801d0da7485407daef9af6194b6d8c8818175bec5efec6/atheris-3.1.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:f8a9f51ce8369026e8eb7b7174835e8c4c85a1a6db5d9add36c15100779d2a39", size = 36800563, upload-time = "2026-06-17T00:04:04.559Z" }, @@ -1454,7 +1467,8 @@ coverage = [ { name = "coverage", extra = ["toml"] }, ] fuzz = [ - { name = "atheris" }, + { name = "atheris", version = "3.0.0", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.11.5'" }, + { name = "atheris", version = "3.1.0", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.11.5'" }, ] gevent = [ { name = "gevent" }, From 6a11a936b926d63391c6d90ec8107c7d8cb9dd49 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Fri, 13 Nov 2026 09:59:34 -0600 Subject: [PATCH 02/12] Ensure a project-controlled uv and use toolchain Pythons by path --- .evergreen/scripts/install-dependencies.sh | 4 +- .evergreen/scripts/setup-uv-python.sh | 19 +++++- .evergreen/scripts/setup_uv.py | 73 +++++++++++++++++----- .pre-commit-config.yaml | 7 +++ CONTRIBUTING.md | 31 ++++----- 5 files changed, 102 insertions(+), 32 deletions(-) diff --git a/.evergreen/scripts/install-dependencies.sh b/.evergreen/scripts/install-dependencies.sh index 0a4e5791a6..d1b359dddf 100755 --- a/.evergreen/scripts/install-dependencies.sh +++ b/.evergreen/scripts/install-dependencies.sh @@ -49,8 +49,8 @@ if [ "Windows_NT" = "${OS:-}" ]; then fi # Skip setup when a suitable uv is already installed: `setup_uv.py --check` -# verifies the required-version pin when set, else that any uv is on PATH, -# without side effects. +# verifies the required-version pin when set, else that the uv on PATH is the +# one setup installed into UV_TOOL_BIN_DIR, without side effects. # A full `uv sync` here would download Python and install/build dependencies # just to make this decision, and would run before setup-uv-python.sh sets # UV_PYTHON, creating the environment twice on local dev. diff --git a/.evergreen/scripts/setup-uv-python.sh b/.evergreen/scripts/setup-uv-python.sh index 2fe4ff5f3c..b5ef0590dc 100755 --- a/.evergreen/scripts/setup-uv-python.sh +++ b/.evergreen/scripts/setup-uv-python.sh @@ -11,7 +11,9 @@ # UV_PYTHON_PREFERENCE - "system" so the toolchain wins over managed installs. # A preference set by the task is left alone. # UV_PYTHON - the Python interpreter uv uses; defaults to CPython -# 3.11 when the task does not set one. +# 3.11 when the task does not set one, and becomes the +# toolchain interpreter's path when the toolchain +# provides the requested version. set -euo pipefail HERE=$(dirname "${BASH_SOURCE:-$0}") @@ -70,6 +72,9 @@ function _toolchain_dir() { else dir="/opt/python/${version}/bin" fi + # PYMONGO_TOOLCHAIN_ROOT overrides the unix toolchain root for the tests; the + # /bin layout under it is unchanged. + dir="${PYMONGO_TOOLCHAIN_ROOT:-$(dirname "$dir")}/${version}/bin" [ -d "$dir" ] || return 1 echo "$dir" } @@ -115,6 +120,18 @@ if [[ "$UV_PYTHON" == */* || "$UV_PYTHON" == *\\* ]]; then _skip_ensure=1 elif _dir=$(_toolchain_dir "$UV_PYTHON"); then _search_path="$_dir" + # Prefer the toolchain interpreter by explicit path when it has the requested + # version and the task did not set its own preference (PYTHON-6135): the + # explicit path bypasses uv's discovery, so a task-set preference would be + # silently ignored. uv's discovery is version-dependent and has failed on CI + # with uv 0.12.24: `uv python find` aborts on a search-path interpreter it + # cannot inspect (RHEL8's /usr/bin/python3 is 3.6) before reaching the + # toolchain dir, and `uv python install` has no cpython-3.15 download for it. + # uv uses an explicit path as-is, so bypass both. + if [ -z "${UV_PYTHON_PREFERENCE:-}" ] && [ -x "$_dir/python${UV_PYTHON}" ]; then + export UV_PYTHON="$_dir/python${UV_PYTHON}" + _skip_ensure=1 + fi fi # Point uv at the toolchain Python when there is one and the task did not set diff --git a/.evergreen/scripts/setup_uv.py b/.evergreen/scripts/setup_uv.py index d99ccca645..ad254a90d5 100755 --- a/.evergreen/scripts/setup_uv.py +++ b/.evergreen/scripts/setup_uv.py @@ -1,11 +1,16 @@ #!/usr/bin/env python3 -"""Bootstrap uv/just for the test environment, pinning uv only when -[tool.uv] required-version is set in pyproject.toml. +"""Bootstrap uv/just for the test environment. install-dependencies.sh probes with `setup_uv.py --check`, sources ensure-uv.sh (which finds or installs uv), then runs this script for the rest. Only the standard library is used. +The project always controls a uv in UV_TOOL_BIN_DIR: the pinned version when +[tool.uv] required-version is set in pyproject.toml, the latest uv otherwise. +The uv a host or the vendored drivers-evergreen-tools submodule puts on PATH +may be invisible to later steps (it is not in the project's bin dir) and may +lack behavior the project relies on, so it is never accepted as-is. + This runs under the host's system python3, which may be older than our minimum supported version. """ @@ -66,21 +71,26 @@ def _install_uv_astral() -> None: _add_path(os.environ["UV_TOOL_BIN_DIR"]) -def _pin_uv(uv_pin: str) -> None: - """Install the pinned uv via uv tool install --force. +def _install_uv(pin: str) -> None: + """Install uv via uv tool install --force into UV_TOOL_BIN_DIR. - UV_TOOL_BIN_DIR and UV_TOOL_DIR are inherited from the environment (set by - install-dependencies.sh / ensure-uv.sh, in native form on Windows). uv tool - install writes the binary into UV_TOOL_BIN_DIR and the tool venv into - UV_TOOL_DIR; --force lets it overwrite an existing install. + Installs the pinned version when pin is set ("==0.12.12"), the latest uv + otherwise (pin ""). UV_TOOL_BIN_DIR and UV_TOOL_DIR are inherited from the + environment (set by install-dependencies.sh / ensure-uv.sh, in native form + on Windows). uv tool install writes the binary into UV_TOOL_BIN_DIR and the + tool venv into UV_TOOL_DIR; --force lets it overwrite an existing install. - Windows will not overwrite a running executable, so when the current uv is - already the target bin dir, run the install from a copy of the binary. + Windows will not overwrite a running executable, so when the uv driving + the install is itself the binary about to be overwritten, run the install + from a copy of it. """ uv_path = shutil.which("uv") + bin_dir = os.environ["UV_TOOL_BIN_DIR"] + exe = "uv.exe" if os.name == "nt" else "uv" + overwrite_self = uv_path is not None and _same_path(uv_path, str(Path(bin_dir, exe))) tool = uv_path tmp_uv = None - if os.name == "nt": + if overwrite_self: tmp_uv = Path(tempfile.mkdtemp()) / Path(uv_path).name shutil.copy2(uv_path, tmp_uv) tool = str(tmp_uv) @@ -94,7 +104,7 @@ def _pin_uv(uv_pin: str) -> None: "-q", "--force", "--from", - f"uv{uv_pin}", + f"uv{pin}", "uv", ], check=True, @@ -134,15 +144,41 @@ def _write_env() -> None: ENV_SH.write_bytes(("\n".join(keep) + "\n").encode()) +def _same_path(a: str, b: str) -> bool: + """Compare two path strings, tolerating forms realpath cannot unify. + + On cygwin, which() may report a /cygdrive form while UV_TOOL_BIN_DIR is + native ("C:/..."), and realpath cannot translate between the two forms. + """ + try: + if os.path.realpath(a) == os.path.realpath(b): + return True + except OSError: + pass + return os.path.normcase(os.path.normpath(a)) == os.path.normcase(os.path.normpath(b)) + + +def _project_uv_installed() -> bool: + """Return True when the uv on PATH is the one this setup put in UV_TOOL_BIN_DIR.""" + uv_path = shutil.which("uv") + bin_dir = os.environ.get("UV_TOOL_BIN_DIR") + if not uv_path or not bin_dir: + return False + exe = "uv.exe" if os.name == "nt" else "uv" + return _same_path(uv_path, str(Path(bin_dir, exe))) + + def _required_uv_installed() -> bool: """Return True when the uv on PATH already satisfies the setup requirement. With a required-version pin, the uv on PATH must match it exactly; without - a pin, any uv on PATH is acceptable. + a pin, it must be the project-controlled uv in UV_TOOL_BIN_DIR. A uv from + elsewhere on PATH (a host install, the vendored submodule) does not count: + later steps may not see it and its behavior is not the project's choice. """ pin = required_uv_pin() if not pin: - return shutil.which("uv") is not None + return _project_uv_installed() if not pin.startswith("=="): # Only exact pins are checked here; anything else falls back to setup. return False @@ -173,7 +209,14 @@ def main() -> int: uv_pin = required_uv_pin() if uv_pin: - _pin_uv(uv_pin) + _install_uv(uv_pin) + elif not _project_uv_installed(): + # Unpinned: still install the latest uv into UV_TOOL_BIN_DIR, so the + # project controls the uv that later steps find on PATH. A uv found + # elsewhere (the vendored submodule's, the host's) may be missing from + # those steps' PATH entirely, and may lack behavior the project relies + # on, such as provisioning newer CPythons. + _install_uv("") _write_env() return 0 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 27e4e2bd02..703716619a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -129,3 +129,10 @@ repos: language: python require_serial: true additional_dependencies: ["pytest", "pytest-asyncio", "shrub.py>=3.10.0", "pyyaml>=6.0.2"] + - id: evergreen-scripts + name: evergreen-scripts + entry: python -m pytest .evergreen/scripts/test_setup_uv.py -m "" + language: python + require_serial: true + pass_filenames: false + additional_dependencies: ["pytest", "pytest-asyncio"] diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 415d71e9c8..16bb0521f7 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -199,13 +199,13 @@ the pages will re-render and the browser will automatically refresh. create a virtual environment and run `pytest` directly. If you want to use a specific version of Python, set `UV_PYTHON` before running `just install`. - `just install` installs the latest `uv` into `$HOME/.local/bin` when no `uv` is on PATH - (no pin), and adds that directory to your shell rc file when missing, so `uv` takes effect in new shells. - The fail-fast version check only applies if a `required-version` pin is ever set under `[tool.uv]` in - `pyproject.toml`. Scripts inside the vendored `drivers-evergreen-tools` submodule are exempt from this - project's uv settings: the setup scripts write a `uv.toml` boundary file into that checkout (see - `.evergreen/scripts/configure-env.sh`), which stops uv's config discovery there, since the tools' scripts - run with their own uv settings. + `just install` installs the latest `uv` into `$HOME/.local/bin` (no pin) unless that uv is + already on PATH, and adds that directory to your shell rc file when missing, so `uv` takes effect + in new shells. The fail-fast version check only applies if a `required-version` pin is ever set + under `[tool.uv]` in `pyproject.toml`. Scripts inside the vendored `drivers-evergreen-tools` + submodule are exempt from this project's uv settings: the setup scripts write a `uv.toml` boundary + file into that checkout (see `.evergreen/scripts/configure-env.sh`), which stops uv's config + discovery there, since the tools' scripts run with their own uv settings. - Ensure you have started the appropriate Mongo Server(s). You can run `just run-server` with optional args to set up the server. All given options will be passed to [`run-mongodb.sh`](https://github.com/mongodb-labs/drivers-evergreen-tools/blob/master/.evergreen/run-mongodb.sh). Run `${DRIVERS_TOOLS:-drivers-evergreen-tools}/.evergreen/run-mongodb.sh start -h` @@ -482,15 +482,18 @@ Tasks are host-agnostic, so the interpreter lookup happens on the host: for a pl CPython version whose toolchain dir exists, `UV_PYTHON_SEARCH_PATH` points uv at it, and for anything else (a version the toolchain lacks) uv downloads it. - If a specific Python binary is needed (for example on the FIPS host), set `UV_PYTHON=/path/to/python`. - - The default if `UV_PYTHON` is not set is CPython 3.10. This is deterministic across host types, so a task + - The default if `UV_PYTHON` is not set is CPython 3.11. This is deterministic across host types, so a task that does not pin a version always gets the same Python (with the build headers (`Python.h`) needed to build the C extensions). - - GitHub Actions use the latest uv (`astral-sh/setup-uv`'s default), and Evergreen ensures a uv via - `ensure-uv.sh` / `setup_uv.py`. A `[tool.uv] required-version` pin is added to `pyproject.toml` only when a - needed uv lockfile feature requires it. If uv cannot find the requested Python, it installs it; if that - fails, the task fails. The submodule boundary keeps this project's `[tool.uv]` settings from leaking into - the tools' scripts: the setup scripts write a `uv.toml` boundary into the submodule checkout that stops - uv's config discovery there. + - GitHub Actions use the latest uv (`astral-sh/setup-uv`'s default), and Evergreen ensures a + project-controlled uv in `UV_TOOL_BIN_DIR` via `ensure-uv.sh` / `setup_uv.py` (the pinned version + when a pin is set, the latest otherwise). A `[tool.uv] required-version` pin is added to + `pyproject.toml` only when a needed uv lockfile feature requires it. If uv cannot find the + requested Python, it installs it; if that fails, the task fails. When the MongoDB toolchain + provides the requested version, `setup-uv-python.sh` points `UV_PYTHON` at that interpreter by + path, which keeps provisioning independent of the uv version on PATH. The submodule boundary + keeps this project's `[tool.uv]` settings from leaking into the tools' scripts: the setup scripts + write a `uv.toml` boundary into the submodule checkout that stops uv's config discovery there. - Regenerate the test variants and tasks using `pre-commit run --all-files generate-config`. - PR tasks pin to the Python boundaries: `MIN_PYTHON` with `TEST_MIN_DEPS=1`, `MAX_PYTHON`, and one `FREE_THREADED_PYTHON`, against the newest numbered server (`MAX_VERSION`; the `latest` nightly From f3199d1e18d4c5d1d6bab5cfaefeacb4319bc2aa Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Fri, 13 Nov 2026 09:59:55 -0600 Subject: [PATCH 03/12] add test file --- .evergreen/scripts/test_setup_uv.py | 398 ++++++++++++++++++++++++++++ 1 file changed, 398 insertions(+) create mode 100644 .evergreen/scripts/test_setup_uv.py diff --git a/.evergreen/scripts/test_setup_uv.py b/.evergreen/scripts/test_setup_uv.py new file mode 100644 index 0000000000..597405d72d --- /dev/null +++ b/.evergreen/scripts/test_setup_uv.py @@ -0,0 +1,398 @@ +"""Tests for .evergreen/scripts/setup_uv.py and .evergreen/scripts/setup-uv-python.sh. + +Run with: python -m pytest .evergreen/scripts/test_setup_uv.py + +The bash tests drive setup-uv-python.sh with a stub `uv` whose `python find` / +`python install` fail the way CI's uv does on hosts like rhel8.10 (an +un-inspectable /usr/bin/python3 and no cpython-3.15 download), plus a fake +toolchain root selected through PYMONGO_TOOLCHAIN_ROOT. +""" + +from __future__ import annotations + +import os +import shutil +import stat +import subprocess +import sys +from pathlib import Path + +import pytest + +HERE = Path(__file__).resolve().parent +SETUP_UV = HERE / "setup_uv.py" +SETUP_UV_PYTHON_SH = HERE / "setup-uv-python.sh" + +PIN = "==0.12.12" + +UNPINNED_PYPROJECT = '[project]\nname = "test"\n' +PINNED_PYPROJECT = f'[project]\nname = "test"\n\n[tool.uv]\nrequired-version = "{PIN}"\n' + +# A stub `uv`: records each invocation to $UV_STUB_LOG, reports +# $UV_STUB_VERSION for --version, fails `python find`/`python install` the way +# CI's uv does on rhel8.10 (except a ">=3.9" find, which reports +# $UV_STUB_FALLBACK when set), and emulates `uv tool install ... --from ` +# by writing itself into $UV_TOOL_BIN_DIR. +STUB_UV = """\ +#!/bin/bash +[ -n "${UV_STUB_LOG:-}" ] && printf '%s\\n' "$*" >> "$UV_STUB_LOG" +case "${1:-}" in + --version|version) + printf 'uv %s (x86_64-unknown-linux-gnu)\\n' "${UV_STUB_VERSION:-0.12.24}" + exit 0 ;; +esac +case "$1 $2" in + "python find") + if [ "${3:-}" = ">=3.9" ] && [ -n "${UV_STUB_FALLBACK:-}" ]; then + printf '%s\\n' "$UV_STUB_FALLBACK" + exit 0 + fi + printf 'error: Failed to inspect Python interpreter from search path at `/usr/bin/python3`\\n' >&2 + exit 1 ;; + "python install") + printf 'error: No download found for request: cpython-3.15-linux-x86_64-gnu\\n' >&2 + exit 1 ;; + "tool install") + req="" + prev="" + for arg in "$@"; do + if [ "$prev" = "--from" ]; then req="$arg"; fi + prev="$arg" + done + [ -n "${UV_STUB_LOG:-}" ] && printf 'installed-from=%s\\n' "$req" >> "$UV_STUB_LOG" + mkdir -p "${UV_TOOL_BIN_DIR:?}" + cp "$0" "${UV_TOOL_BIN_DIR}/uv" + exit 0 ;; +esac +printf 'stub uv: unhandled invocation: %s\\n' "$*" >&2 +exit 64 +""" + + +def make_executable(path: Path) -> Path: + path.chmod(path.stat().st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) + return path + + +def write_stub_uv(bin_dir: Path) -> Path: + bin_dir.mkdir(parents=True, exist_ok=True) + stub = bin_dir / "uv" + stub.write_text(STUB_UV) + return make_executable(stub) + + +def make_toolchain(root: Path, version: str) -> Path: + """Create a fake toolchain interpreter, e.g. /3.15/bin/python3.15.""" + interpreter = root / version / "bin" / f"python{version}" + interpreter.parent.mkdir(parents=True, exist_ok=True) + interpreter.write_text("#!/bin/sh\nexit 0\n") + return make_executable(interpreter) + + +def stub_calls(log: Path) -> list[str]: + if not log.exists(): + return [] + return [line for line in log.read_text().splitlines() if not line.startswith("installed-from=")] + + +# --- setup-uv-python.sh ------------------------------------------------------ + +needs_bash = pytest.mark.skipif( + sys.platform in ("win32", "cygwin") or shutil.which("bash") is None, + reason="needs a unix shell", +) + + +def run_setup_uv_python(tmp_path: Path, extra_env: dict) -> subprocess.CompletedProcess: + """Source setup-uv-python.sh in a child shell; report rc and the exported UV_PYTHON.""" + env = { + "HOME": str(tmp_path), + "CI": "true", + } + env.update(extra_env) + return subprocess.run( # noqa: S603 + [ + "/bin/bash", + "-c", + ( + f'. "{SETUP_UV_PYTHON_SH}"; ' + 'printf "\\nUV_PYTHON=%s" "$UV_PYTHON"; ' + 'printf "\\nSEARCH_PATH=%s" "${UV_PYTHON_SEARCH_PATH-}"; ' + 'printf "\\nPREFERENCE=%s" "${UV_PYTHON_PREFERENCE-}"' + ), + ], + check=False, + env=env, + capture_output=True, + text=True, + ) + + +@pytest.fixture +def stub_env(tmp_path): + """A stub uv bin dir and its invocation log; returns (log, env-overrides).""" + log = tmp_path / "uv-stub.log" + write_stub_uv(tmp_path / "stub-bin") + return log, {"PATH": f"{tmp_path / 'stub-bin'}:/usr/bin:/bin", "UV_STUB_LOG": str(log)} + + +@needs_bash +def test_toolchain_python_is_used_by_path(tmp_path, stub_env): + """CI: an exact toolchain match wins by explicit path, without uv find/install.""" + log, env = stub_env + interpreter = make_toolchain(tmp_path / "toolchain", "3.15") + proc = run_setup_uv_python( + tmp_path, + extra_env={ + **env, + "UV_PYTHON": "3.15", + "PYMONGO_TOOLCHAIN_ROOT": str(tmp_path / "toolchain"), + }, + ) + assert proc.returncode == 0, proc.stderr + assert f"UV_PYTHON={interpreter}" in proc.stdout + assert f"SEARCH_PATH={interpreter.parent}" in proc.stdout + assert "PREFERENCE=system" in proc.stdout + # uv's discovery is bypassed entirely: no find, no install. + assert stub_calls(log) == [] + + +@needs_bash +def test_free_threaded_toolchain_python_is_used_by_path(tmp_path, stub_env): + """CI: the same bypass applies to free-threaded toolchain versions.""" + log, env = stub_env + interpreter = make_toolchain(tmp_path / "toolchain", "3.15t") + proc = run_setup_uv_python( + tmp_path, + extra_env={ + **env, + "UV_PYTHON": "3.15t", + "PYMONGO_TOOLCHAIN_ROOT": str(tmp_path / "toolchain"), + }, + ) + assert proc.returncode == 0, proc.stderr + assert f"UV_PYTHON={interpreter}" in proc.stdout + assert stub_calls(log) == [] + + +@needs_bash +@needs_bash +def test_toolchain_falls_through_when_interpreter_missing(tmp_path, stub_env): + """CI: a toolchain dir without the requested version keeps using uv find/install.""" + log, env = stub_env + make_toolchain(tmp_path / "toolchain", "3.14") + proc = run_setup_uv_python( + tmp_path, + extra_env={ + **env, + "UV_PYTHON": "3.15", + "PYMONGO_TOOLCHAIN_ROOT": str(tmp_path / "toolchain"), + }, + ) + assert proc.returncode == 1 + assert "could not find or install Python" in proc.stderr + assert stub_calls(log) == ["python find 3.15", "python install 3.15"] + + +@needs_bash +def test_task_preference_disables_bypass(tmp_path, stub_env): + """A task-set UV_PYTHON_PREFERENCE (PYTHON-6135) keeps the pre-bypass behavior.""" + log, env = stub_env + make_toolchain(tmp_path / "toolchain", "3.15") + proc = run_setup_uv_python( + tmp_path, + extra_env={ + **env, + "UV_PYTHON": "3.15", + "UV_PYTHON_PREFERENCE": "only-managed", + "PYMONGO_TOOLCHAIN_ROOT": str(tmp_path / "toolchain"), + }, + ) + assert proc.returncode == 1 + assert "could not find or install Python" in proc.stderr + # No bypass: the script failed in uv's find/install (rc 0 would mean the + # explicit-path branch ran, ignoring the task's preference). + assert stub_calls(log) == ["python find 3.15", "python install 3.15"] + + +def test_ci_fails_loudly_without_toolchain(tmp_path, stub_env): + """CI without the toolchain keeps failing after find and install both fail.""" + log, env = stub_env + proc = run_setup_uv_python( + tmp_path, + extra_env={ + **env, + "UV_PYTHON": "3.15", + # An empty toolchain root: no version can match it. + "PYMONGO_TOOLCHAIN_ROOT": str(tmp_path / "no-toolchain"), + }, + ) + assert proc.returncode == 1 + assert "could not find or install Python" in proc.stderr + # find was attempted, then install. + assert stub_calls(log) == ["python find 3.15", "python install 3.15"] + + +@needs_bash +def test_local_fallback_without_toolchain(tmp_path, stub_env): + """Local (no CI) hosts fall back to any interpreter >=3.9 when find/install fail.""" + _, env = stub_env + fallback = make_toolchain(tmp_path / "elsewhere", "3.11") + proc = run_setup_uv_python( + tmp_path, + extra_env={ + **env, + "UV_PYTHON": "3.15", + "CI": "", + "UV_STUB_FALLBACK": str(fallback), + # An empty toolchain root: no version can match it. + "PYMONGO_TOOLCHAIN_ROOT": str(tmp_path / "no-toolchain"), + }, + ) + assert proc.returncode == 0, proc.stderr + assert f"UV_PYTHON={fallback}" in proc.stdout + + +@needs_bash +def test_explicit_interpreter_path_skips_probing(tmp_path, stub_env): + """An explicit interpreter path in UV_PYTHON is used as-is.""" + log, env = stub_env + interpreter = make_toolchain(tmp_path / "elsewhere", "3.12") + proc = run_setup_uv_python(tmp_path, extra_env={**env, "UV_PYTHON": str(interpreter)}) + assert proc.returncode == 0, proc.stderr + assert f"UV_PYTHON={interpreter}" in proc.stdout + assert stub_calls(log) == [] + + +# --- setup_uv.py ------------------------------------------------------------ + + +@pytest.fixture +def project(tmp_path): + """A fake project tree: a pyproject.toml plus a copy of setup_uv.py.""" + proj = tmp_path / "proj" + scripts = proj / ".evergreen" / "scripts" + scripts.mkdir(parents=True) + shutil.copy(SETUP_UV, scripts / "setup_uv.py") + (proj / "pyproject.toml").write_text(UNPINNED_PYPROJECT) + return proj + + +def run_setup_uv(project: Path, args: list[str], env: dict) -> subprocess.CompletedProcess: + return subprocess.run( # noqa: S603 + [sys.executable, str(project / ".evergreen" / "scripts" / "setup_uv.py"), *args], + check=False, + env={**os.environ, **env}, + capture_output=True, + text=True, + ) + + +def test_check_unpinned_requires_project_uv(tmp_path, project): + """Unpinned --check passes only for the uv setup installed, not any uv on PATH.""" + bin_dir = tmp_path / "project-bin" + foreign = write_stub_uv(tmp_path / "foreign-bin") + bin_dir.mkdir() + write_stub_uv(bin_dir) + env = {"PATH": f"{foreign.parent}:{bin_dir}:/usr/bin:/bin", "UV_TOOL_BIN_DIR": str(bin_dir)} + assert run_setup_uv(project, ["--check"], env).returncode == 1 + + +def test_check_unpinned_accepts_project_uv(tmp_path, project): + """Unpinned --check passes when the uv on PATH is the project-installed one.""" + bin_dir = tmp_path / "project-bin" + bin_dir.mkdir() + write_stub_uv(bin_dir) + env = {"PATH": f"{bin_dir}:/usr/bin:/bin", "UV_TOOL_BIN_DIR": str(bin_dir)} + assert run_setup_uv(project, ["--check"], env).returncode == 0 + + +def test_check_unpinned_without_uv_fails(tmp_path, project): + """Unpinned --check fails when no uv is on PATH at all.""" + bin_dir = tmp_path / "project-bin" + bin_dir.mkdir() + env = {"PATH": "/usr/bin:/bin", "UV_TOOL_BIN_DIR": str(bin_dir)} + assert run_setup_uv(project, ["--check"], env).returncode == 1 + + +def test_check_pinned_requires_exact_version(tmp_path, project): + """Pinned --check keeps comparing the uv on PATH with the pin.""" + (project / "pyproject.toml").write_text(PINNED_PYPROJECT) + bin_dir = tmp_path / "project-bin" + bin_dir.mkdir() + write_stub_uv(bin_dir) + env = { + "PATH": f"{bin_dir}:/usr/bin:/bin", + "UV_TOOL_BIN_DIR": str(bin_dir), + "UV_STUB_VERSION": "0.12.24", + } + assert run_setup_uv(project, ["--check"], env).returncode == 1 + env["UV_STUB_VERSION"] = "0.12.12" + assert run_setup_uv(project, ["--check"], env).returncode == 0 + + +def test_main_unpinned_installs_latest_uv_into_bin_dir(tmp_path, project): + """Unpinned setup installs the latest uv into UV_TOOL_BIN_DIR, even with a foreign uv on PATH.""" + log = tmp_path / "log" + foreign = write_stub_uv(tmp_path / "foreign-bin") + bin_dir = tmp_path / "project-bin" + env = { + "PATH": f"{foreign.parent}:/usr/bin:/bin", + "UV_TOOL_BIN_DIR": str(bin_dir), + "UV_STUB_LOG": str(log), + } + proc = run_setup_uv(project, [], env) + assert proc.returncode == 0, proc.stderr + assert (bin_dir / "uv").exists() + assert "installed-from=uv\n" in log.read_text() + + +def test_main_unpinned_skips_install_when_project_uv_on_path(tmp_path, project): + """Unpinned setup does not reinstall when the project uv is already on PATH.""" + log = tmp_path / "log" + bin_dir = tmp_path / "project-bin" + write_stub_uv(bin_dir) + env = { + "PATH": f"{bin_dir}:/usr/bin:/bin", + "UV_TOOL_BIN_DIR": str(bin_dir), + "UV_STUB_LOG": str(log), + } + proc = run_setup_uv(project, [], env) + assert proc.returncode == 0, proc.stderr + assert not log.exists() or "installed-from=" not in log.read_text() + + +def test_main_pinned_installs_pinned_uv(tmp_path, project): + """Pinned setup keeps installing the pinned uv into UV_TOOL_BIN_DIR.""" + (project / "pyproject.toml").write_text(PINNED_PYPROJECT) + log = tmp_path / "log" + foreign = write_stub_uv(tmp_path / "foreign-bin") + bin_dir = tmp_path / "project-bin" + env = { + "PATH": f"{foreign.parent}:/usr/bin:/bin", + "UV_TOOL_BIN_DIR": str(bin_dir), + "UV_STUB_LOG": str(log), + "UV_STUB_VERSION": "0.12.24", + } + proc = run_setup_uv(project, [], env) + assert proc.returncode == 0, proc.stderr + assert (bin_dir / "uv").exists() + assert f"installed-from=uv{PIN}\n" in log.read_text() + + +def test_main_pinned_self_overwrite_runs_from_copy(tmp_path, project): + """When the driving uv is the binary being replaced, the install runs from a copy.""" + (project / "pyproject.toml").write_text(PINNED_PYPROJECT) + log = tmp_path / "log" + bin_dir = tmp_path / "project-bin" + write_stub_uv(bin_dir) + env = { + "PATH": f"{bin_dir}:/usr/bin:/bin", + "UV_TOOL_BIN_DIR": str(bin_dir), + "UV_STUB_LOG": str(log), + "UV_STUB_VERSION": "0.12.12", + } + proc = run_setup_uv(project, [], env) + assert proc.returncode == 0, proc.stderr + assert (bin_dir / "uv").exists() + assert f"installed-from=uv{PIN}\n" in log.read_text() From 987d458e562a2c9555f5b8b5980117a8c0565770 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Fri, 13 Nov 2026 10:56:33 -0600 Subject: [PATCH 04/12] trim prose --- .evergreen/scripts/configure-env.sh | 5 ++-- .evergreen/scripts/install-dependencies.sh | 4 +-- .evergreen/scripts/setup-uv-python.sh | 17 ++++------- .evergreen/scripts/setup_uv.py | 28 +++++++----------- .pre-commit-config.yaml | 1 + CONTRIBUTING.md | 34 ++++++++++------------ 6 files changed, 36 insertions(+), 53 deletions(-) diff --git a/.evergreen/scripts/configure-env.sh b/.evergreen/scripts/configure-env.sh index e84be3b24e..c6f7d6f502 100755 --- a/.evergreen/scripts/configure-env.sh +++ b/.evergreen/scripts/configure-env.sh @@ -38,9 +38,8 @@ fi # Add the latest MongoDB toolchain bin dir to PATH if it exists, so that hosts # with an old system Python (e.g. RHEL8's 3.6) still get a modern interpreter -# for tool installs like `uv tool install rust-just`. It goes after -# PYMONGO_BIN_DIR so the uv installed there by setup_uv.py takes precedence -# over the toolchain's uv. +# for tool installs like `uv tool install rust-just`. Goes after PYMONGO_BIN_DIR +# so the project's uv wins. if [ "Windows_NT" = "${OS:-}" ]; then _toolchain_bin="/cygdrive/c/Python/Current/Scripts" elif [ "$(uname -s)" == "Darwin" ]; then diff --git a/.evergreen/scripts/install-dependencies.sh b/.evergreen/scripts/install-dependencies.sh index d1b359dddf..3ad8d2e9d5 100755 --- a/.evergreen/scripts/install-dependencies.sh +++ b/.evergreen/scripts/install-dependencies.sh @@ -49,8 +49,8 @@ if [ "Windows_NT" = "${OS:-}" ]; then fi # Skip setup when a suitable uv is already installed: `setup_uv.py --check` -# verifies the required-version pin when set, else that the uv on PATH is the -# one setup installed into UV_TOOL_BIN_DIR, without side effects. +# verifies the required-version pin, or the project-controlled uv in +# UV_TOOL_BIN_DIR when unpinned, without side effects. # A full `uv sync` here would download Python and install/build dependencies # just to make this decision, and would run before setup-uv-python.sh sets # UV_PYTHON, creating the environment twice on local dev. diff --git a/.evergreen/scripts/setup-uv-python.sh b/.evergreen/scripts/setup-uv-python.sh index b5ef0590dc..c5ba0e397a 100755 --- a/.evergreen/scripts/setup-uv-python.sh +++ b/.evergreen/scripts/setup-uv-python.sh @@ -11,9 +11,8 @@ # UV_PYTHON_PREFERENCE - "system" so the toolchain wins over managed installs. # A preference set by the task is left alone. # UV_PYTHON - the Python interpreter uv uses; defaults to CPython -# 3.11 when the task does not set one, and becomes the -# toolchain interpreter's path when the toolchain -# provides the requested version. +# 3.11, or the toolchain interpreter's path when the +# toolchain has the requested version. set -euo pipefail HERE=$(dirname "${BASH_SOURCE:-$0}") @@ -120,14 +119,10 @@ if [[ "$UV_PYTHON" == */* || "$UV_PYTHON" == *\\* ]]; then _skip_ensure=1 elif _dir=$(_toolchain_dir "$UV_PYTHON"); then _search_path="$_dir" - # Prefer the toolchain interpreter by explicit path when it has the requested - # version and the task did not set its own preference (PYTHON-6135): the - # explicit path bypasses uv's discovery, so a task-set preference would be - # silently ignored. uv's discovery is version-dependent and has failed on CI - # with uv 0.12.24: `uv python find` aborts on a search-path interpreter it - # cannot inspect (RHEL8's /usr/bin/python3 is 3.6) before reaching the - # toolchain dir, and `uv python install` has no cpython-3.15 download for it. - # uv uses an explicit path as-is, so bypass both. + # Use the toolchain interpreter by explicit path when the task did not set its + # own preference (PYTHON-6135): uv uses the path as-is, so provisioning cannot + # fail on uv's interpreter discovery (it aborts on interpreters it cannot + # inspect, like RHEL8's /usr/bin/python3 3.6) or on a missing download. if [ -z "${UV_PYTHON_PREFERENCE:-}" ] && [ -x "$_dir/python${UV_PYTHON}" ]; then export UV_PYTHON="$_dir/python${UV_PYTHON}" _skip_ensure=1 diff --git a/.evergreen/scripts/setup_uv.py b/.evergreen/scripts/setup_uv.py index ad254a90d5..d2cdaefde2 100755 --- a/.evergreen/scripts/setup_uv.py +++ b/.evergreen/scripts/setup_uv.py @@ -6,10 +6,9 @@ standard library is used. The project always controls a uv in UV_TOOL_BIN_DIR: the pinned version when -[tool.uv] required-version is set in pyproject.toml, the latest uv otherwise. -The uv a host or the vendored drivers-evergreen-tools submodule puts on PATH -may be invisible to later steps (it is not in the project's bin dir) and may -lack behavior the project relies on, so it is never accepted as-is. +[tool.uv] required-version is set, the latest uv otherwise. A uv found +elsewhere on PATH may be invisible to later steps, so setup never accepts it +as-is. This runs under the host's system python3, which may be older than our minimum supported version. """ @@ -72,17 +71,12 @@ def _install_uv_astral() -> None: def _install_uv(pin: str) -> None: - """Install uv via uv tool install --force into UV_TOOL_BIN_DIR. + """Install uv into UV_TOOL_BIN_DIR with `uv tool install --force`. Installs the pinned version when pin is set ("==0.12.12"), the latest uv - otherwise (pin ""). UV_TOOL_BIN_DIR and UV_TOOL_DIR are inherited from the - environment (set by install-dependencies.sh / ensure-uv.sh, in native form - on Windows). uv tool install writes the binary into UV_TOOL_BIN_DIR and the - tool venv into UV_TOOL_DIR; --force lets it overwrite an existing install. - - Windows will not overwrite a running executable, so when the uv driving - the install is itself the binary about to be overwritten, run the install - from a copy of it. + otherwise (pin ""). Windows will not overwrite a running executable, so + when the uv driving the install is the binary being replaced, run the + install from a copy of it. """ uv_path = shutil.which("uv") bin_dir = os.environ["UV_TOOL_BIN_DIR"] @@ -145,10 +139,10 @@ def _write_env() -> None: def _same_path(a: str, b: str) -> bool: - """Compare two path strings, tolerating forms realpath cannot unify. + """Compare two paths, tolerating forms realpath cannot unify. On cygwin, which() may report a /cygdrive form while UV_TOOL_BIN_DIR is - native ("C:/..."), and realpath cannot translate between the two forms. + native ("C:/..."). """ try: if os.path.realpath(a) == os.path.realpath(b): @@ -172,9 +166,7 @@ def _required_uv_installed() -> bool: """Return True when the uv on PATH already satisfies the setup requirement. With a required-version pin, the uv on PATH must match it exactly; without - a pin, it must be the project-controlled uv in UV_TOOL_BIN_DIR. A uv from - elsewhere on PATH (a host install, the vendored submodule) does not count: - later steps may not see it and its behavior is not the project's choice. + a pin, it must be the project-controlled uv in UV_TOOL_BIN_DIR. """ pin = required_uv_pin() if not pin: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 703716619a..994feb5d30 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -135,4 +135,5 @@ repos: language: python require_serial: true pass_filenames: false + files: ^\.evergreen/scripts/ additional_dependencies: ["pytest", "pytest-asyncio"] diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 16bb0521f7..b89cbf7ff7 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -199,13 +199,12 @@ the pages will re-render and the browser will automatically refresh. create a virtual environment and run `pytest` directly. If you want to use a specific version of Python, set `UV_PYTHON` before running `just install`. - `just install` installs the latest `uv` into `$HOME/.local/bin` (no pin) unless that uv is - already on PATH, and adds that directory to your shell rc file when missing, so `uv` takes effect - in new shells. The fail-fast version check only applies if a `required-version` pin is ever set - under `[tool.uv]` in `pyproject.toml`. Scripts inside the vendored `drivers-evergreen-tools` - submodule are exempt from this project's uv settings: the setup scripts write a `uv.toml` boundary - file into that checkout (see `.evergreen/scripts/configure-env.sh`), which stops uv's config - discovery there, since the tools' scripts run with their own uv settings. + `just install` installs the latest `uv` into `$HOME/.local/bin` (no pin) unless one is already + on PATH, and adds that directory to your shell rc file when missing. The fail-fast version check + applies only when a `required-version` pin is set under `[tool.uv]` in `pyproject.toml`. The + vendored `drivers-evergreen-tools` submodule is exempt from this project's uv settings: the setup + scripts write a `uv.toml` boundary into that checkout (see `.evergreen/scripts/configure-env.sh`) + to stop uv's config discovery there. - Ensure you have started the appropriate Mongo Server(s). You can run `just run-server` with optional args to set up the server. All given options will be passed to [`run-mongodb.sh`](https://github.com/mongodb-labs/drivers-evergreen-tools/blob/master/.evergreen/run-mongodb.sh). Run `${DRIVERS_TOOLS:-drivers-evergreen-tools}/.evergreen/run-mongodb.sh start -h` @@ -485,15 +484,13 @@ CPython version whose toolchain dir exists, `UV_PYTHON_SEARCH_PATH` points uv at - The default if `UV_PYTHON` is not set is CPython 3.11. This is deterministic across host types, so a task that does not pin a version always gets the same Python (with the build headers (`Python.h`) needed to build the C extensions). - - GitHub Actions use the latest uv (`astral-sh/setup-uv`'s default), and Evergreen ensures a - project-controlled uv in `UV_TOOL_BIN_DIR` via `ensure-uv.sh` / `setup_uv.py` (the pinned version - when a pin is set, the latest otherwise). A `[tool.uv] required-version` pin is added to - `pyproject.toml` only when a needed uv lockfile feature requires it. If uv cannot find the - requested Python, it installs it; if that fails, the task fails. When the MongoDB toolchain - provides the requested version, `setup-uv-python.sh` points `UV_PYTHON` at that interpreter by - path, which keeps provisioning independent of the uv version on PATH. The submodule boundary - keeps this project's `[tool.uv]` settings from leaking into the tools' scripts: the setup scripts - write a `uv.toml` boundary into the submodule checkout that stops uv's config discovery there. + - GitHub Actions use the latest uv (`astral-sh/setup-uv`'s default); Evergreen ensures a + project-controlled uv in `UV_TOOL_BIN_DIR` via `ensure-uv.sh` / `setup_uv.py` (the pinned + version when a pin is set, the latest otherwise). A `[tool.uv] required-version` pin is added + only when a needed uv lockfile feature requires it. If uv cannot find the requested Python, it + installs it; if that fails, the task fails. When the MongoDB toolchain provides the requested + version, `setup-uv-python.sh` points `UV_PYTHON` at that interpreter by path, keeping + provisioning independent of the uv version on PATH. - Regenerate the test variants and tasks using `pre-commit run --all-files generate-config`. - PR tasks pin to the Python boundaries: `MIN_PYTHON` with `TEST_MIN_DEPS=1`, `MAX_PYTHON`, and one `FREE_THREADED_PYTHON`, against the newest numbered server (`MAX_VERSION`; the `latest` nightly @@ -661,9 +658,8 @@ From the spawn host or Ubuntu image, do the following: ## Dependency updates Dependabot raises PRs at most once per week for GitHub Actions updates. Dependabot fully -manages `uv.lock`: one grouped weekly pull request for all Python dependencies (direct and -transitive) with a 7-day cooldown, plus immediate, ungrouped pull requests for security -updates, which the cooldown never delays. +manages `uv.lock`: one grouped weekly pull request with a 7-day cooldown, plus immediate, +ungrouped security updates the cooldown never delays. Never set `exclude-newer` under `[tool.uv]` in `pyproject.toml`: Dependabot resolves with it and produces pull requests that remove or downgrade packages From 6c32ccbe8f0c8e3e185902d053dd02a5e5e6f2c6 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Fri, 13 Nov 2026 12:27:01 -0600 Subject: [PATCH 05/12] Ensure a project-controlled uv and use toolchain Pythons by path --- .evergreen/scripts/setup-uv-python.sh | 21 +++-- .evergreen/scripts/test_setup_uv.py | 114 +++++++++++++++++++++++--- 2 files changed, 118 insertions(+), 17 deletions(-) diff --git a/.evergreen/scripts/setup-uv-python.sh b/.evergreen/scripts/setup-uv-python.sh index c5ba0e397a..c270693098 100755 --- a/.evergreen/scripts/setup-uv-python.sh +++ b/.evergreen/scripts/setup-uv-python.sh @@ -49,12 +49,17 @@ fi # uv version in pyproject.toml. export UV_PYTHON="${UV_PYTHON:-3.11}" -# Print the Python toolchain bin dir for a version like "3.11" or "3.14t". -function _toolchain_dir() { +# Print the Python toolchain bin dir for a version like "3.11" or "3.14t", +# without checking that it exists. +function _toolchain_bin_dir() { local version="$1" dir # Only plain CPython versions live in the toolchain. [[ "$version" =~ ^[0-9]+\.[0-9]+t?$ ]] || return 1 - if [ "Windows_NT" = "${OS:-}" ]; then + if [ -n "${PYMONGO_TOOLCHAIN_ROOT:-}" ]; then + # Test override of the unix toolchain root; the /bin layout under + # it is unchanged. + dir="${PYMONGO_TOOLCHAIN_ROOT}/${version}/bin" + elif [ "Windows_NT" = "${OS:-}" ]; then local winver="${version%t}" winver="${winver//./}" if [ -n "${IS_WIN32:-}" ]; then @@ -71,9 +76,13 @@ function _toolchain_dir() { else dir="/opt/python/${version}/bin" fi - # PYMONGO_TOOLCHAIN_ROOT overrides the unix toolchain root for the tests; the - # /bin layout under it is unchanged. - dir="${PYMONGO_TOOLCHAIN_ROOT:-$(dirname "$dir")}/${version}/bin" + echo "$dir" +} + +# Print the toolchain bin dir for the version when it exists on this host. +function _toolchain_dir() { + local dir + dir=$(_toolchain_bin_dir "$1") || return 1 [ -d "$dir" ] || return 1 echo "$dir" } diff --git a/.evergreen/scripts/test_setup_uv.py b/.evergreen/scripts/test_setup_uv.py index 597405d72d..d297d76af6 100644 --- a/.evergreen/scripts/test_setup_uv.py +++ b/.evergreen/scripts/test_setup_uv.py @@ -97,14 +97,25 @@ def stub_calls(log: Path) -> list[str]: # --- setup-uv-python.sh ------------------------------------------------------ -needs_bash = pytest.mark.skipif( +# The whole module is unix-only: the bash tests need a unix shell, and the +# setup_uv.py tests use bash-script uv stubs and colon-separated PATH values. +pytestmark = pytest.mark.skipif( sys.platform in ("win32", "cygwin") or shutil.which("bash") is None, reason="needs a unix shell", ) def run_setup_uv_python(tmp_path: Path, extra_env: dict) -> subprocess.CompletedProcess: - """Source setup-uv-python.sh in a child shell; report rc and the exported UV_PYTHON.""" + """Source a copy of setup-uv-python.sh in a temporary project tree. + + The copy's HERE/ROOT resolve inside the tree, so the script sources the + tree's env.sh (if any) and probes the tree's .venv, never the checkout's. + Reports rc and the exported UV_PYTHON / search path / preference. + """ + scripts = tmp_path / "proj" / ".evergreen" / "scripts" + scripts.mkdir(parents=True, exist_ok=True) + script_copy = scripts / "setup-uv-python.sh" + shutil.copy(SETUP_UV_PYTHON_SH, script_copy) env = { "HOME": str(tmp_path), "CI": "true", @@ -115,10 +126,11 @@ def run_setup_uv_python(tmp_path: Path, extra_env: dict) -> subprocess.Completed "/bin/bash", "-c", ( - f'. "{SETUP_UV_PYTHON_SH}"; ' + f'. "{script_copy}"; ' 'printf "\\nUV_PYTHON=%s" "$UV_PYTHON"; ' 'printf "\\nSEARCH_PATH=%s" "${UV_PYTHON_SEARCH_PATH-}"; ' - 'printf "\\nPREFERENCE=%s" "${UV_PYTHON_PREFERENCE-}"' + 'printf "\\nPREFERENCE=%s" "${UV_PYTHON_PREFERENCE-}"; ' + 'printf "\\nTOOL_BIN_DIR=%s" "${UV_TOOL_BIN_DIR-}"' ), ], check=False, @@ -136,7 +148,6 @@ def stub_env(tmp_path): return log, {"PATH": f"{tmp_path / 'stub-bin'}:/usr/bin:/bin", "UV_STUB_LOG": str(log)} -@needs_bash def test_toolchain_python_is_used_by_path(tmp_path, stub_env): """CI: an exact toolchain match wins by explicit path, without uv find/install.""" log, env = stub_env @@ -157,7 +168,6 @@ def test_toolchain_python_is_used_by_path(tmp_path, stub_env): assert stub_calls(log) == [] -@needs_bash def test_free_threaded_toolchain_python_is_used_by_path(tmp_path, stub_env): """CI: the same bypass applies to free-threaded toolchain versions.""" log, env = stub_env @@ -175,8 +185,6 @@ def test_free_threaded_toolchain_python_is_used_by_path(tmp_path, stub_env): assert stub_calls(log) == [] -@needs_bash -@needs_bash def test_toolchain_falls_through_when_interpreter_missing(tmp_path, stub_env): """CI: a toolchain dir without the requested version keeps using uv find/install.""" log, env = stub_env @@ -194,7 +202,6 @@ def test_toolchain_falls_through_when_interpreter_missing(tmp_path, stub_env): assert stub_calls(log) == ["python find 3.15", "python install 3.15"] -@needs_bash def test_task_preference_disables_bypass(tmp_path, stub_env): """A task-set UV_PYTHON_PREFERENCE (PYTHON-6135) keeps the pre-bypass behavior.""" log, env = stub_env @@ -233,7 +240,6 @@ def test_ci_fails_loudly_without_toolchain(tmp_path, stub_env): assert stub_calls(log) == ["python find 3.15", "python install 3.15"] -@needs_bash def test_local_fallback_without_toolchain(tmp_path, stub_env): """Local (no CI) hosts fall back to any interpreter >=3.9 when find/install fail.""" _, env = stub_env @@ -253,7 +259,6 @@ def test_local_fallback_without_toolchain(tmp_path, stub_env): assert f"UV_PYTHON={fallback}" in proc.stdout -@needs_bash def test_explicit_interpreter_path_skips_probing(tmp_path, stub_env): """An explicit interpreter path in UV_PYTHON is used as-is.""" log, env = stub_env @@ -264,6 +269,93 @@ def test_explicit_interpreter_path_skips_probing(tmp_path, stub_env): assert stub_calls(log) == [] +def toolchain_bin_dir(script_copy: Path, sentinel: Path, version: str) -> str: + """Call the script's _toolchain_bin_dir helper and return the printed path.""" + proc = subprocess.run( # noqa: S603 + [ + "/bin/bash", + "-c", + f'UV_PYTHON="{sentinel}" . "{script_copy}"; _toolchain_bin_dir "{version}"', + ], + check=False, + env={"PATH": "/usr/bin:/bin"}, + capture_output=True, + text=True, + ) + assert proc.returncode == 0, proc.stderr + return proc.stdout.strip() + + +def test_toolchain_paths_preserved_without_override(tmp_path): + """Without the test override, the platform toolchain paths are preserved as-is.""" + scripts = tmp_path / "proj" / ".evergreen" / "scripts" + scripts.mkdir(parents=True) + script_copy = scripts / "setup-uv-python.sh" + shutil.copy(SETUP_UV_PYTHON_SH, script_copy) + sentinel = tmp_path / "sentinel" + sentinel.write_text("#!/bin/sh\nexit 0\n") + make_executable(sentinel) + if sys.platform == "darwin": + expected = { + "3.15": "/Library/Frameworks/Python.Framework/Versions/3.15/bin", + "3.15t": "/Library/Frameworks/PythonT.Framework/Versions/3.15/bin", + } + else: + expected = { + "3.15": "/opt/python/3.15/bin", + "3.15t": "/opt/python/3.15t/bin", + } + for version, path in expected.items(): + assert toolchain_bin_dir(script_copy, sentinel, version) == path + + +def test_toolchain_override_keeps_version_layout(tmp_path): + """With the test override, the /bin layout under it is kept.""" + scripts = tmp_path / "proj" / ".evergreen" / "scripts" + scripts.mkdir(parents=True) + script_copy = scripts / "setup-uv-python.sh" + shutil.copy(SETUP_UV_PYTHON_SH, script_copy) + sentinel = tmp_path / "sentinel" + sentinel.write_text("#!/bin/sh\nexit 0\n") + make_executable(sentinel) + proc = subprocess.run( # noqa: S603 + [ + "/bin/bash", + "-c", + ( + f'export UV_PYTHON="{sentinel}" ' + f'PYMONGO_TOOLCHAIN_ROOT="{tmp_path / "root"}"; ' + f'. "{script_copy}"; _toolchain_bin_dir "3.15"' + ), + ], + check=False, + env={"PATH": "/usr/bin:/bin"}, + capture_output=True, + text=True, + ) + assert proc.returncode == 0, proc.stderr + assert proc.stdout.strip() == str(tmp_path / "root" / "3.15" / "bin") + + +def test_script_sources_env_sh_from_its_tree(tmp_path, stub_env): + """The script sources env.sh from the copy's tree, not the checkout's.""" + _, env = stub_env + fallback = make_toolchain(tmp_path / "elsewhere", "3.11") + proc = run_setup_uv_python( + tmp_path, + extra_env={ + **env, + "CI": "", + "UV_STUB_FALLBACK": str(fallback), + }, + ) + # The runner's tree has no env.sh, so the checkout's env.sh must not have + # leaked: it exports UV_TOOL_BIN_DIR, which stays unset here. + assert proc.returncode == 0, proc.stderr + # The last printf has no trailing newline. + assert proc.stdout.rstrip("\n").endswith("TOOL_BIN_DIR=") + + # --- setup_uv.py ------------------------------------------------------------ From c955483082b1780192728343801a0e736fccb09e Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Fri, 13 Nov 2026 12:47:56 -0600 Subject: [PATCH 06/12] fix script invocation --- .evergreen/scripts/test_setup_uv.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/.evergreen/scripts/test_setup_uv.py b/.evergreen/scripts/test_setup_uv.py index d297d76af6..0e18a45f00 100644 --- a/.evergreen/scripts/test_setup_uv.py +++ b/.evergreen/scripts/test_setup_uv.py @@ -19,6 +19,16 @@ import pytest +# The repo's pytest addopts filter on "default or default_async"; marking the +# module keeps bare pytest invocations from deselecting it. +pytestmark = [ + pytest.mark.default, + pytest.mark.skipif( + sys.platform in ("win32", "cygwin") or shutil.which("bash") is None, + reason="needs a unix shell", + ), +] + HERE = Path(__file__).resolve().parent SETUP_UV = HERE / "setup_uv.py" SETUP_UV_PYTHON_SH = HERE / "setup-uv-python.sh" @@ -97,13 +107,6 @@ def stub_calls(log: Path) -> list[str]: # --- setup-uv-python.sh ------------------------------------------------------ -# The whole module is unix-only: the bash tests need a unix shell, and the -# setup_uv.py tests use bash-script uv stubs and colon-separated PATH values. -pytestmark = pytest.mark.skipif( - sys.platform in ("win32", "cygwin") or shutil.which("bash") is None, - reason="needs a unix shell", -) - def run_setup_uv_python(tmp_path: Path, extra_env: dict) -> subprocess.CompletedProcess: """Source a copy of setup-uv-python.sh in a temporary project tree. From a5c5d37a9fac1b82dd25ca9bc7adfc6f3b0dbbd7 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Fri, 13 Nov 2026 12:59:31 -0600 Subject: [PATCH 07/12] Fix the documented uv reuse condition --- CONTRIBUTING.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b89cbf7ff7..007b73cfd7 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -199,8 +199,8 @@ the pages will re-render and the browser will automatically refresh. create a virtual environment and run `pytest` directly. If you want to use a specific version of Python, set `UV_PYTHON` before running `just install`. - `just install` installs the latest `uv` into `$HOME/.local/bin` (no pin) unless one is already - on PATH, and adds that directory to your shell rc file when missing. The fail-fast version check + `just install` installs the latest `uv` into `$HOME/.local/bin` (no pin) unless the `uv` on PATH + is that copy, and adds that directory to your shell rc file when missing. The fail-fast version check applies only when a `required-version` pin is set under `[tool.uv]` in `pyproject.toml`. The vendored `drivers-evergreen-tools` submodule is exempt from this project's uv settings: the setup scripts write a `uv.toml` boundary into that checkout (see `.evergreen/scripts/configure-env.sh`) From 454be7dffc93d7df2fc002b9687c595a982007dd Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 16 Nov 2026 07:19:35 -0600 Subject: [PATCH 08/12] fix windows path handling --- .evergreen/scripts/setup-uv-python.sh | 18 ++- .evergreen/scripts/setup_uv.py | 15 ++- .evergreen/scripts/test_setup_uv.py | 162 ++++++++++++++++++++++---- 3 files changed, 169 insertions(+), 26 deletions(-) diff --git a/.evergreen/scripts/setup-uv-python.sh b/.evergreen/scripts/setup-uv-python.sh index c270693098..126e84a24a 100755 --- a/.evergreen/scripts/setup-uv-python.sh +++ b/.evergreen/scripts/setup-uv-python.sh @@ -79,6 +79,19 @@ function _toolchain_bin_dir() { echo "$dir" } +# Print the toolchain interpreter file for a version like "3.15t", without +# checking that it exists. Windows interpreters are exe files; cygwin bash's +# -x test also matches the bare name, but uv is a native Windows binary and +# needs the exact file name. +function _interpreter_file() { + local dir="$1" version="$2" + if [ "Windows_NT" = "${OS:-}" ]; then + echo "$dir/python${version}.exe" + else + echo "$dir/python${version}" + fi +} + # Print the toolchain bin dir for the version when it exists on this host. function _toolchain_dir() { local dir @@ -132,8 +145,9 @@ elif _dir=$(_toolchain_dir "$UV_PYTHON"); then # own preference (PYTHON-6135): uv uses the path as-is, so provisioning cannot # fail on uv's interpreter discovery (it aborts on interpreters it cannot # inspect, like RHEL8's /usr/bin/python3 3.6) or on a missing download. - if [ -z "${UV_PYTHON_PREFERENCE:-}" ] && [ -x "$_dir/python${UV_PYTHON}" ]; then - export UV_PYTHON="$_dir/python${UV_PYTHON}" + _file=$(_interpreter_file "$_dir" "$UV_PYTHON") + if [ -z "${UV_PYTHON_PREFERENCE:-}" ] && [ -x "$_file" ]; then + export UV_PYTHON="$_file" _skip_ensure=1 fi fi diff --git a/.evergreen/scripts/setup_uv.py b/.evergreen/scripts/setup_uv.py index d2cdaefde2..e3de7d0356 100755 --- a/.evergreen/scripts/setup_uv.py +++ b/.evergreen/scripts/setup_uv.py @@ -138,6 +138,15 @@ def _write_env() -> None: ENV_SH.write_bytes(("\n".join(keep) + "\n").encode()) +def _cygdrive(path: str) -> str: + """Map a native Windows path to its cygwin /cygdrive form.""" + match = re.match(r"^([A-Za-z]):[/\\](.*)$", path) + if not match: + return path + drive, rest = match.groups() + return "/cygdrive/" + drive.lower() + "/" + rest.replace("\\", "/") + + def _same_path(a: str, b: str) -> bool: """Compare two paths, tolerating forms realpath cannot unify. @@ -149,7 +158,11 @@ def _same_path(a: str, b: str) -> bool: return True except OSError: pass - return os.path.normcase(os.path.normpath(a)) == os.path.normcase(os.path.normpath(b)) + return any( + os.path.normcase(os.path.normpath(form_a)) == os.path.normcase(os.path.normpath(form_b)) + for form_a in (a, _cygdrive(a)) + for form_b in (b, _cygdrive(b)) + ) def _project_uv_installed() -> bool: diff --git a/.evergreen/scripts/test_setup_uv.py b/.evergreen/scripts/test_setup_uv.py index 0e18a45f00..470026c9fd 100644 --- a/.evergreen/scripts/test_setup_uv.py +++ b/.evergreen/scripts/test_setup_uv.py @@ -10,6 +10,7 @@ from __future__ import annotations +import importlib.util import os import shutil import stat @@ -107,8 +108,41 @@ def stub_calls(log: Path) -> list[str]: # --- setup-uv-python.sh ------------------------------------------------------ +setup_uv = importlib.util.module_from_spec( + importlib.util.spec_from_file_location("setup_uv", HERE / "setup_uv.py") +) +setup_uv.__spec__.loader.exec_module(setup_uv) -def run_setup_uv_python(tmp_path: Path, extra_env: dict) -> subprocess.CompletedProcess: + +def call_toolchain_bin_dir(script_copy: Path, version: str, env_overrides: dict) -> str: + """Call the script's _toolchain_bin_dir helper and return the printed path.""" + proc = subprocess.run( # noqa: S603 + ["/bin/bash", "-c", f'. "{script_copy}"; _toolchain_bin_dir "{version}"'], + check=False, + env={ + "PATH": "/usr/bin:/bin", + "HOME": str(script_copy.parents[2]), + **env_overrides, + }, + capture_output=True, + text=True, + ) + assert proc.returncode == 0, proc.stderr + return proc.stdout.strip() + + +def script_copy_in(tmp_path: Path) -> Path: + """Copy setup-uv-python.sh into a temporary project tree and return its path.""" + scripts = tmp_path / "proj" / ".evergreen" / "scripts" + scripts.mkdir(parents=True) + script_copy = scripts / "setup-uv-python.sh" + shutil.copy(SETUP_UV_PYTHON_SH, script_copy) + return script_copy + + +def run_setup_uv_python( + tmp_path: Path, extra_env: dict, cwd: Path | None = None +) -> subprocess.CompletedProcess: """Source a copy of setup-uv-python.sh in a temporary project tree. The copy's HERE/ROOT resolve inside the tree, so the script sources the @@ -138,6 +172,7 @@ def run_setup_uv_python(tmp_path: Path, extra_env: dict) -> subprocess.Completed ], check=False, env=env, + cwd=str(cwd or tmp_path), capture_output=True, text=True, ) @@ -272,29 +307,9 @@ def test_explicit_interpreter_path_skips_probing(tmp_path, stub_env): assert stub_calls(log) == [] -def toolchain_bin_dir(script_copy: Path, sentinel: Path, version: str) -> str: - """Call the script's _toolchain_bin_dir helper and return the printed path.""" - proc = subprocess.run( # noqa: S603 - [ - "/bin/bash", - "-c", - f'UV_PYTHON="{sentinel}" . "{script_copy}"; _toolchain_bin_dir "{version}"', - ], - check=False, - env={"PATH": "/usr/bin:/bin"}, - capture_output=True, - text=True, - ) - assert proc.returncode == 0, proc.stderr - return proc.stdout.strip() - - def test_toolchain_paths_preserved_without_override(tmp_path): """Without the test override, the platform toolchain paths are preserved as-is.""" - scripts = tmp_path / "proj" / ".evergreen" / "scripts" - scripts.mkdir(parents=True) - script_copy = scripts / "setup-uv-python.sh" - shutil.copy(SETUP_UV_PYTHON_SH, script_copy) + script_copy = script_copy_in(tmp_path) sentinel = tmp_path / "sentinel" sentinel.write_text("#!/bin/sh\nexit 0\n") make_executable(sentinel) @@ -309,7 +324,7 @@ def test_toolchain_paths_preserved_without_override(tmp_path): "3.15t": "/opt/python/3.15t/bin", } for version, path in expected.items(): - assert toolchain_bin_dir(script_copy, sentinel, version) == path + assert call_toolchain_bin_dir(script_copy, version, {"UV_PYTHON": str(sentinel)}) == path def test_toolchain_override_keeps_version_layout(tmp_path): @@ -340,6 +355,99 @@ def test_toolchain_override_keeps_version_layout(tmp_path): assert proc.stdout.strip() == str(tmp_path / "root" / "3.15" / "bin") +def test_toolchain_branch_for_darwin(tmp_path): + """The darwin branch computes the framework toolchain path.""" + script_copy = script_copy_in(tmp_path) + (tmp_path / "shims").mkdir() + shim = tmp_path / "shims" / "uname" + shim.write_text("#!/bin/sh\necho Darwin\n") + make_executable(shim) + sentinel = tmp_path / "sentinel" + sentinel.write_text("#!/bin/sh\nexit 0\n") + make_executable(sentinel) + shim_env = {"PATH": f"{tmp_path / 'shims'}:/usr/bin:/bin", "UV_PYTHON": str(sentinel)} + assert ( + call_toolchain_bin_dir(script_copy, "3.15", shim_env) + == "/Library/Frameworks/Python.Framework/Versions/3.15/bin" + ) + assert ( + call_toolchain_bin_dir(script_copy, "3.15t", shim_env) + == "/Library/Frameworks/PythonT.Framework/Versions/3.15/bin" + ) + + +def test_toolchain_branch_for_windows(tmp_path): + """The Windows branch computes the python toolchain path.""" + script_copy = script_copy_in(tmp_path) + sentinel = tmp_path / "sentinel" + sentinel.write_text("#!/bin/sh\nexit 0\n") + make_executable(sentinel) + env = {"OS": "Windows_NT", "UV_PYTHON": str(sentinel)} + assert call_toolchain_bin_dir(script_copy, "3.15", env) == "C:/python/Python315" + + +def call_interpreter_file(script_copy: Path, version: str, env_overrides: dict) -> str: + """Call the script's _interpreter_file helper and return the printed path.""" + proc = subprocess.run( # noqa: S603 + [ + "/bin/bash", + "-c", + f'. "{script_copy}"; _interpreter_file "/toolchain/dir" "{version}"', + ], + check=False, + env={ + "PATH": "/usr/bin:/bin", + "HOME": str(script_copy.parents[2]), + **env_overrides, + }, + capture_output=True, + text=True, + ) + assert proc.returncode == 0, proc.stderr + return proc.stdout.strip() + + +def test_interpreter_file_names_the_exact_file(tmp_path): + """Windows interpreters are exe files; the other platforms use the bare name. + + Native uv needs the exact file name; cygwin bash's -x test also matches + the bare name (PYTHON-6273). + """ + script_copy = script_copy_in(tmp_path) + sentinel = tmp_path / "sentinel" + sentinel.write_text("#!/bin/sh\nexit 0\n") + make_executable(sentinel) + windows = {"OS": "Windows_NT", "UV_PYTHON": str(sentinel)} + assert call_interpreter_file(script_copy, "3.15t", windows) == "/toolchain/dir/python3.15t.exe" + assert call_interpreter_file(script_copy, "3.15t", {"UV_PYTHON": str(sentinel)}) == ( + "/toolchain/dir/python3.15t" + ) + assert call_interpreter_file(script_copy, "3.11", windows) == "/toolchain/dir/python3.11.exe" + + +def test_windows_toolchain_interpreter_is_exported_as_exe_file(tmp_path, stub_env): + """CI (Windows): the exported interpreter path names the .exe file exactly. + + The script computes the toolchain dir as the native path + "C:/python/Python315", which resolves under this test's cwd. + """ + log, env = stub_env + python_dir = tmp_path / "C:" / "python" / "Python315" + python_dir.mkdir(parents=True) + interpreter = python_dir / "python3.15t.exe" + interpreter.write_text("#!/bin/sh\nexit 0\n") + make_executable(interpreter) + proc = run_setup_uv_python( + tmp_path, + extra_env={**env, "UV_PYTHON": "3.15t", "OS": "Windows_NT"}, + cwd=tmp_path, + ) + assert proc.returncode == 0, proc.stderr + assert "UV_PYTHON=C:/python/Python315/python3.15t.exe" in proc.stdout + # The exact file bypasses uv's discovery entirely: no find, no install. + assert stub_calls(log) == [] + + def test_script_sources_env_sh_from_its_tree(tmp_path, stub_env): """The script sources env.sh from the copy's tree, not the checkout's.""" _, env = stub_env @@ -359,6 +467,14 @@ def test_script_sources_env_sh_from_its_tree(tmp_path, stub_env): assert proc.stdout.rstrip("\n").endswith("TOOL_BIN_DIR=") +def test_same_path_maps_cygwin_forms(): + """A native Windows path matches its cygwin /cygdrive form.""" + assert setup_uv._same_path("C:/tools/uv.exe", "C:/tools/uv.exe") + assert setup_uv._same_path("C:/tools/uv.exe", "/cygdrive/c/tools/uv.exe") + assert setup_uv._same_path("/cygdrive/c/tools/uv.exe", "C:/tools/uv.exe") + assert not setup_uv._same_path("C:/tools/uv.exe", "C:/elsewhere/uv.exe") + + # --- setup_uv.py ------------------------------------------------------------ From 188b7ad1577ceee39724ec1ebf1a6c6e104a0821 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 16 Nov 2026 07:51:39 -0600 Subject: [PATCH 09/12] trim comment --- .evergreen/scripts/setup-uv-python.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.evergreen/scripts/setup-uv-python.sh b/.evergreen/scripts/setup-uv-python.sh index 126e84a24a..188c6a1dbb 100755 --- a/.evergreen/scripts/setup-uv-python.sh +++ b/.evergreen/scripts/setup-uv-python.sh @@ -80,8 +80,7 @@ function _toolchain_bin_dir() { } # Print the toolchain interpreter file for a version like "3.15t", without -# checking that it exists. Windows interpreters are exe files; cygwin bash's -# -x test also matches the bare name, but uv is a native Windows binary and +# checking that it exists. Windows interpreters are exe files: native uv # needs the exact file name. function _interpreter_file() { local dir="$1" version="$2" From b056b3d3dc3c5dacb645e9125c6c1faffdd6f773 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 16 Nov 2026 08:00:59 -0600 Subject: [PATCH 10/12] cleanup tests --- .evergreen/scripts/test_setup_uv.py | 91 ++--------------------------- 1 file changed, 4 insertions(+), 87 deletions(-) diff --git a/.evergreen/scripts/test_setup_uv.py b/.evergreen/scripts/test_setup_uv.py index 470026c9fd..ca91ab079a 100644 --- a/.evergreen/scripts/test_setup_uv.py +++ b/.evergreen/scripts/test_setup_uv.py @@ -149,10 +149,7 @@ def run_setup_uv_python( tree's env.sh (if any) and probes the tree's .venv, never the checkout's. Reports rc and the exported UV_PYTHON / search path / preference. """ - scripts = tmp_path / "proj" / ".evergreen" / "scripts" - scripts.mkdir(parents=True, exist_ok=True) - script_copy = scripts / "setup-uv-python.sh" - shutil.copy(SETUP_UV_PYTHON_SH, script_copy) + script_copy = script_copy_in(tmp_path) env = { "HOME": str(tmp_path), "CI": "true", @@ -206,23 +203,6 @@ def test_toolchain_python_is_used_by_path(tmp_path, stub_env): assert stub_calls(log) == [] -def test_free_threaded_toolchain_python_is_used_by_path(tmp_path, stub_env): - """CI: the same bypass applies to free-threaded toolchain versions.""" - log, env = stub_env - interpreter = make_toolchain(tmp_path / "toolchain", "3.15t") - proc = run_setup_uv_python( - tmp_path, - extra_env={ - **env, - "UV_PYTHON": "3.15t", - "PYMONGO_TOOLCHAIN_ROOT": str(tmp_path / "toolchain"), - }, - ) - assert proc.returncode == 0, proc.stderr - assert f"UV_PYTHON={interpreter}" in proc.stdout - assert stub_calls(log) == [] - - def test_toolchain_falls_through_when_interpreter_missing(tmp_path, stub_env): """CI: a toolchain dir without the requested version keeps using uv find/install.""" log, env = stub_env @@ -260,24 +240,6 @@ def test_task_preference_disables_bypass(tmp_path, stub_env): assert stub_calls(log) == ["python find 3.15", "python install 3.15"] -def test_ci_fails_loudly_without_toolchain(tmp_path, stub_env): - """CI without the toolchain keeps failing after find and install both fail.""" - log, env = stub_env - proc = run_setup_uv_python( - tmp_path, - extra_env={ - **env, - "UV_PYTHON": "3.15", - # An empty toolchain root: no version can match it. - "PYMONGO_TOOLCHAIN_ROOT": str(tmp_path / "no-toolchain"), - }, - ) - assert proc.returncode == 1 - assert "could not find or install Python" in proc.stderr - # find was attempted, then install. - assert stub_calls(log) == ["python find 3.15", "python install 3.15"] - - def test_local_fallback_without_toolchain(tmp_path, stub_env): """Local (no CI) hosts fall back to any interpreter >=3.9 when find/install fail.""" _, env = stub_env @@ -329,50 +291,13 @@ def test_toolchain_paths_preserved_without_override(tmp_path): def test_toolchain_override_keeps_version_layout(tmp_path): """With the test override, the /bin layout under it is kept.""" - scripts = tmp_path / "proj" / ".evergreen" / "scripts" - scripts.mkdir(parents=True) - script_copy = scripts / "setup-uv-python.sh" - shutil.copy(SETUP_UV_PYTHON_SH, script_copy) - sentinel = tmp_path / "sentinel" - sentinel.write_text("#!/bin/sh\nexit 0\n") - make_executable(sentinel) - proc = subprocess.run( # noqa: S603 - [ - "/bin/bash", - "-c", - ( - f'export UV_PYTHON="{sentinel}" ' - f'PYMONGO_TOOLCHAIN_ROOT="{tmp_path / "root"}"; ' - f'. "{script_copy}"; _toolchain_bin_dir "3.15"' - ), - ], - check=False, - env={"PATH": "/usr/bin:/bin"}, - capture_output=True, - text=True, - ) - assert proc.returncode == 0, proc.stderr - assert proc.stdout.strip() == str(tmp_path / "root" / "3.15" / "bin") - - -def test_toolchain_branch_for_darwin(tmp_path): - """The darwin branch computes the framework toolchain path.""" script_copy = script_copy_in(tmp_path) - (tmp_path / "shims").mkdir() - shim = tmp_path / "shims" / "uname" - shim.write_text("#!/bin/sh\necho Darwin\n") - make_executable(shim) sentinel = tmp_path / "sentinel" sentinel.write_text("#!/bin/sh\nexit 0\n") make_executable(sentinel) - shim_env = {"PATH": f"{tmp_path / 'shims'}:/usr/bin:/bin", "UV_PYTHON": str(sentinel)} - assert ( - call_toolchain_bin_dir(script_copy, "3.15", shim_env) - == "/Library/Frameworks/Python.Framework/Versions/3.15/bin" - ) - assert ( - call_toolchain_bin_dir(script_copy, "3.15t", shim_env) - == "/Library/Frameworks/PythonT.Framework/Versions/3.15/bin" + override = {"UV_PYTHON": str(sentinel), "PYMONGO_TOOLCHAIN_ROOT": str(tmp_path / "root")} + assert call_toolchain_bin_dir(script_copy, "3.15t", override) == str( + tmp_path / "root" / "3.15t" / "bin" ) @@ -518,14 +443,6 @@ def test_check_unpinned_accepts_project_uv(tmp_path, project): assert run_setup_uv(project, ["--check"], env).returncode == 0 -def test_check_unpinned_without_uv_fails(tmp_path, project): - """Unpinned --check fails when no uv is on PATH at all.""" - bin_dir = tmp_path / "project-bin" - bin_dir.mkdir() - env = {"PATH": "/usr/bin:/bin", "UV_TOOL_BIN_DIR": str(bin_dir)} - assert run_setup_uv(project, ["--check"], env).returncode == 1 - - def test_check_pinned_requires_exact_version(tmp_path, project): """Pinned --check keeps comparing the uv on PATH with the pin.""" (project / "pyproject.toml").write_text(PINNED_PYPROJECT) From 36c64240ed23dd70e903a3ea430a17153583ee40 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 16 Nov 2026 08:06:50 -0600 Subject: [PATCH 11/12] trim comments --- .evergreen/scripts/test_setup_uv.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/.evergreen/scripts/test_setup_uv.py b/.evergreen/scripts/test_setup_uv.py index ca91ab079a..2f42298592 100644 --- a/.evergreen/scripts/test_setup_uv.py +++ b/.evergreen/scripts/test_setup_uv.py @@ -20,8 +20,7 @@ import pytest -# The repo's pytest addopts filter on "default or default_async"; marking the -# module keeps bare pytest invocations from deselecting it. +# The repo's pytest addopts deselect modules without the "default" mark. pytestmark = [ pytest.mark.default, pytest.mark.skipif( @@ -39,11 +38,9 @@ UNPINNED_PYPROJECT = '[project]\nname = "test"\n' PINNED_PYPROJECT = f'[project]\nname = "test"\n\n[tool.uv]\nrequired-version = "{PIN}"\n' -# A stub `uv`: records each invocation to $UV_STUB_LOG, reports -# $UV_STUB_VERSION for --version, fails `python find`/`python install` the way -# CI's uv does on rhel8.10 (except a ">=3.9" find, which reports -# $UV_STUB_FALLBACK when set), and emulates `uv tool install ... --from ` -# by writing itself into $UV_TOOL_BIN_DIR. +# A stub `uv` that fails `python find`/`python install` the way CI's uv does +# on rhel8.10, records each invocation to $UV_STUB_LOG, and emulates +# `uv tool install` by copying itself into $UV_TOOL_BIN_DIR. STUB_UV = """\ #!/bin/bash [ -n "${UV_STUB_LOG:-}" ] && printf '%s\\n' "$*" >> "$UV_STUB_LOG" @@ -199,7 +196,6 @@ def test_toolchain_python_is_used_by_path(tmp_path, stub_env): assert f"UV_PYTHON={interpreter}" in proc.stdout assert f"SEARCH_PATH={interpreter.parent}" in proc.stdout assert "PREFERENCE=system" in proc.stdout - # uv's discovery is bypassed entirely: no find, no install. assert stub_calls(log) == [] @@ -235,8 +231,7 @@ def test_task_preference_disables_bypass(tmp_path, stub_env): ) assert proc.returncode == 1 assert "could not find or install Python" in proc.stderr - # No bypass: the script failed in uv's find/install (rc 0 would mean the - # explicit-path branch ran, ignoring the task's preference). + # rc 1 means the explicit-path branch did not run. assert stub_calls(log) == ["python find 3.15", "python install 3.15"] @@ -369,7 +364,6 @@ def test_windows_toolchain_interpreter_is_exported_as_exe_file(tmp_path, stub_en ) assert proc.returncode == 0, proc.stderr assert "UV_PYTHON=C:/python/Python315/python3.15t.exe" in proc.stdout - # The exact file bypasses uv's discovery entirely: no find, no install. assert stub_calls(log) == [] @@ -385,10 +379,8 @@ def test_script_sources_env_sh_from_its_tree(tmp_path, stub_env): "UV_STUB_FALLBACK": str(fallback), }, ) - # The runner's tree has no env.sh, so the checkout's env.sh must not have - # leaked: it exports UV_TOOL_BIN_DIR, which stays unset here. + # A leaked checkout env.sh would set UV_TOOL_BIN_DIR; the runner tree has none. assert proc.returncode == 0, proc.stderr - # The last printf has no trailing newline. assert proc.stdout.rstrip("\n").endswith("TOOL_BIN_DIR=") From 253f7ac5a88631a023ad60b411c8a5f76ed41da8 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 16 Nov 2026 09:16:31 -0600 Subject: [PATCH 12/12] code cleanup --- .evergreen/scripts/configure-env.sh | 4 ++-- .evergreen/scripts/setup_uv.py | 11 +++++++---- .evergreen/scripts/test_setup_uv.py | 30 ++++++++++++++++------------- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/.evergreen/scripts/configure-env.sh b/.evergreen/scripts/configure-env.sh index c6f7d6f502..2143a56660 100755 --- a/.evergreen/scripts/configure-env.sh +++ b/.evergreen/scripts/configure-env.sh @@ -38,8 +38,8 @@ fi # Add the latest MongoDB toolchain bin dir to PATH if it exists, so that hosts # with an old system Python (e.g. RHEL8's 3.6) still get a modern interpreter -# for tool installs like `uv tool install rust-just`. Goes after PYMONGO_BIN_DIR -# so the project's uv wins. +# for tool installs like `uv tool install rust-just`. It goes after +# PYMONGO_BIN_DIR so the project's uv takes precedence. if [ "Windows_NT" = "${OS:-}" ]; then _toolchain_bin="/cygdrive/c/Python/Current/Scripts" elif [ "$(uname -s)" == "Darwin" ]; then diff --git a/.evergreen/scripts/setup_uv.py b/.evergreen/scripts/setup_uv.py index e3de7d0356..76d0d4e9b1 100755 --- a/.evergreen/scripts/setup_uv.py +++ b/.evergreen/scripts/setup_uv.py @@ -70,6 +70,11 @@ def _install_uv_astral() -> None: _add_path(os.environ["UV_TOOL_BIN_DIR"]) +def _uv_exe() -> str: + """The uv binary name on this platform.""" + return "uv.exe" if os.name == "nt" else "uv" + + def _install_uv(pin: str) -> None: """Install uv into UV_TOOL_BIN_DIR with `uv tool install --force`. @@ -80,8 +85,7 @@ def _install_uv(pin: str) -> None: """ uv_path = shutil.which("uv") bin_dir = os.environ["UV_TOOL_BIN_DIR"] - exe = "uv.exe" if os.name == "nt" else "uv" - overwrite_self = uv_path is not None and _same_path(uv_path, str(Path(bin_dir, exe))) + overwrite_self = uv_path is not None and _same_path(uv_path, str(Path(bin_dir, _uv_exe()))) tool = uv_path tmp_uv = None if overwrite_self: @@ -171,8 +175,7 @@ def _project_uv_installed() -> bool: bin_dir = os.environ.get("UV_TOOL_BIN_DIR") if not uv_path or not bin_dir: return False - exe = "uv.exe" if os.name == "nt" else "uv" - return _same_path(uv_path, str(Path(bin_dir, exe))) + return _same_path(uv_path, str(Path(bin_dir, _uv_exe()))) def _required_uv_installed() -> bool: diff --git a/.evergreen/scripts/test_setup_uv.py b/.evergreen/scripts/test_setup_uv.py index 2f42298592..1fd49a4bb2 100644 --- a/.evergreen/scripts/test_setup_uv.py +++ b/.evergreen/scripts/test_setup_uv.py @@ -97,6 +97,13 @@ def make_toolchain(root: Path, version: str) -> Path: return make_executable(interpreter) +def make_sentinel(root: Path) -> Path: + """Create a no-op executable, used as an interpreter path the script accepts.""" + sentinel = root / "sentinel" + sentinel.write_text("#!/bin/sh\nexit 0\n") + return make_executable(sentinel) + + def stub_calls(log: Path) -> list[str]: if not log.exists(): return [] @@ -267,9 +274,7 @@ def test_explicit_interpreter_path_skips_probing(tmp_path, stub_env): def test_toolchain_paths_preserved_without_override(tmp_path): """Without the test override, the platform toolchain paths are preserved as-is.""" script_copy = script_copy_in(tmp_path) - sentinel = tmp_path / "sentinel" - sentinel.write_text("#!/bin/sh\nexit 0\n") - make_executable(sentinel) + sentinel = make_sentinel(tmp_path) if sys.platform == "darwin": expected = { "3.15": "/Library/Frameworks/Python.Framework/Versions/3.15/bin", @@ -287,9 +292,7 @@ def test_toolchain_paths_preserved_without_override(tmp_path): def test_toolchain_override_keeps_version_layout(tmp_path): """With the test override, the /bin layout under it is kept.""" script_copy = script_copy_in(tmp_path) - sentinel = tmp_path / "sentinel" - sentinel.write_text("#!/bin/sh\nexit 0\n") - make_executable(sentinel) + sentinel = make_sentinel(tmp_path) override = {"UV_PYTHON": str(sentinel), "PYMONGO_TOOLCHAIN_ROOT": str(tmp_path / "root")} assert call_toolchain_bin_dir(script_copy, "3.15t", override) == str( tmp_path / "root" / "3.15t" / "bin" @@ -299,9 +302,7 @@ def test_toolchain_override_keeps_version_layout(tmp_path): def test_toolchain_branch_for_windows(tmp_path): """The Windows branch computes the python toolchain path.""" script_copy = script_copy_in(tmp_path) - sentinel = tmp_path / "sentinel" - sentinel.write_text("#!/bin/sh\nexit 0\n") - make_executable(sentinel) + sentinel = make_sentinel(tmp_path) env = {"OS": "Windows_NT", "UV_PYTHON": str(sentinel)} assert call_toolchain_bin_dir(script_copy, "3.15", env) == "C:/python/Python315" @@ -334,9 +335,7 @@ def test_interpreter_file_names_the_exact_file(tmp_path): the bare name (PYTHON-6273). """ script_copy = script_copy_in(tmp_path) - sentinel = tmp_path / "sentinel" - sentinel.write_text("#!/bin/sh\nexit 0\n") - make_executable(sentinel) + sentinel = make_sentinel(tmp_path) windows = {"OS": "Windows_NT", "UV_PYTHON": str(sentinel)} assert call_interpreter_file(script_copy, "3.15t", windows) == "/toolchain/dir/python3.15t.exe" assert call_interpreter_file(script_copy, "3.15t", {"UV_PYTHON": str(sentinel)}) == ( @@ -407,6 +406,11 @@ def project(tmp_path): def run_setup_uv(project: Path, args: list[str], env: dict) -> subprocess.CompletedProcess: + """Run setup_uv.py with the inherited environment plus the overrides. + + Unlike the bash harness, the environment is inherited: setup_uv.py runs + subprocesses (uv tool install) that need a working PATH. + """ return subprocess.run( # noqa: S603 [sys.executable, str(project / ".evergreen" / "scripts" / "setup_uv.py"), *args], check=False, @@ -452,7 +456,7 @@ def test_check_pinned_requires_exact_version(tmp_path, project): def test_main_unpinned_installs_latest_uv_into_bin_dir(tmp_path, project): - """Unpinned setup installs the latest uv into UV_TOOL_BIN_DIR, even with a foreign uv on PATH.""" + """Unpinned setup installs the latest uv into UV_TOOL_BIN_DIR over a foreign uv on PATH.""" log = tmp_path / "log" foreign = write_stub_uv(tmp_path / "foreign-bin") bin_dir = tmp_path / "project-bin"