diff --git a/.github/workflows/nightly-changelog.yml b/.github/workflows/nightly-changelog.yml index 644232624eea..e04455cf4994 100644 --- a/.github/workflows/nightly-changelog.yml +++ b/.github/workflows/nightly-changelog.yml @@ -185,9 +185,9 @@ jobs: - uses: astral-sh/setup-uv@d0cc045d04ccac9d8b7881df0226f9e82c39688e # v6.8.0 with: - # Keep equal to the uv that generated uv.lock: another uv release can - # re-serialize the whole lockfile, which ``compile`` then refuses. - version: "0.13.1" + # Keep equal to the uv that generated uv.lock (pre-commit check-uv-version): another uv + # release can re-serialize the whole lockfile, which ``compile`` then refuses. + version: "0.12.9" - name: Compile fragments # Capture the compile exit code so the next step can commit/push diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 747bf6fa0b3c..cb1bc8c0cec2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -88,6 +88,13 @@ repos: entry: tools/pre_commit/check_git_lfs_pointers.sh language: script pass_filenames: true + - id: check-uv-version + name: check uv version pins and uv.lock revision + entry: python tools/pre_commit/check_uv_version.py + language: python + additional_dependencies: [pyyaml] + files: ^(uv\.lock|docker/.*|source/isaaclab/test/install_ci/Dockerfile\.installci|\.github/.*\.ya?ml)$ + pass_filenames: false - id: no-print-warnings name: use logger.warning or warnings.warn instead of print for warnings # See "Warnings and Logging" in docs/source/refs/contributing.rst. diff --git a/tools/pre_commit/check_uv_version.py b/tools/pre_commit/check_uv_version.py new file mode 100644 index 000000000000..613b0b418052 --- /dev/null +++ b/tools/pre_commit/check_uv_version.py @@ -0,0 +1,96 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Pre-commit hook: every uv pin in the repository names one version, and ``uv.lock`` was written by it. + +uv releases serialize ``uv.lock`` differently (its ``revision`` and some dependency lists). A lock written by one uv +and re-locked by another changes more than package versions, which the nightly changelog compile rejects. +``uv lock --check`` accepts either form, so the lock's ``revision`` is checked instead. +""" + +from __future__ import annotations + +import re +import sys +from pathlib import Path + +import yaml + +REPO_ROOT = Path(__file__).resolve().parents[2] + +LOCK_REVISION = {"0.12.9": 3} +"""The ``uv.lock`` revision each pinned uv writes. Add the new entry when changing the pin.""" + +IMAGE_FILES = ("docker/Dockerfile.*", "docker/test/*.py", "source/isaaclab/test/install_ci/Dockerfile.installci") +IMAGE_PIN = re.compile(r"ghcr\.io/astral-sh/uv:(\d+\.\d+\.\d+)") + + +def image_pins() -> list[tuple[str, str]]: + """Return ``(location, version)`` for every ``ghcr.io/astral-sh/uv`` image reference.""" + pins = [] + for pattern in IMAGE_FILES: + for path in sorted(REPO_ROOT.glob(pattern)): + for number, line in enumerate(path.read_text(encoding="utf-8").splitlines(), start=1): + location = f"{path.relative_to(REPO_ROOT)}:{number}" + pins += [(location, version) for version in IMAGE_PIN.findall(line)] + return pins + + +def workflow_pins() -> list[tuple[str, str]]: + """Return ``(location, version)`` for every ``astral-sh/setup-uv`` step that sets ``version``. + + Steps without ``version`` are skipped: they install the latest uv and do not write ``uv.lock``. + """ + pins = [] + for path in sorted((REPO_ROOT / ".github").rglob("*.y*ml")): + document = yaml.safe_load(path.read_text(encoding="utf-8")) + if not isinstance(document, dict): + continue + steps = [step for job in (document.get("jobs") or {}).values() for step in job.get("steps") or []] + steps += (document.get("runs") or {}).get("steps") or [] # composite actions + for step in steps: + inputs = step.get("with") or {} + if str(step.get("uses", "")).startswith("astral-sh/setup-uv@") and "version" in inputs: + location = f"{path.relative_to(REPO_ROOT)} (step {step.get('name') or step['uses']!r})" + pins.append((location, str(inputs["version"]))) + return pins + + +def lock_revision() -> int | None: + """Return the ``revision`` from the ``uv.lock`` header, or ``None`` if it has none.""" + with open(REPO_ROOT / "uv.lock", encoding="utf-8") as lock: + for line in lock: + if match := re.fullmatch(r"revision = (\d+)\s*", line): + return int(match[1]) + if line.startswith("["): + return None + return None + + +def main() -> int: + """Print every disagreement and return 1 if there is one.""" + pins = image_pins() + workflow_pins() + versions = sorted({version for _, version in pins}) + if len(versions) != 1: + listing = "\n".join(f" {location}: {version}" for location, version in pins) + print(f"uv is pinned to {len(versions)} versions; pin one everywhere:\n{listing}", file=sys.stderr) + return 1 + version = versions[0] + if version not in LOCK_REVISION: + print(f"uv {version} has no LOCK_REVISION entry in {Path(__file__).relative_to(REPO_ROOT)}", file=sys.stderr) + return 1 + revision, expected = lock_revision(), LOCK_REVISION[version] + if revision != expected: + print( + f"uv.lock has revision {revision}, but uv {version} writes revision {expected}: another uv wrote it." + f" Restore uv.lock from your base branch and re-lock with `uvx uv@{version} lock`.", + file=sys.stderr, + ) + return 1 + return 0 + + +if __name__ == "__main__": + sys.exit(main())