|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +# /// script |
| 6 | +# requires-python = ">=3.11" |
| 7 | +# /// |
| 8 | + |
| 9 | +"""Check that aligned Cargo workspaces lock shared crates like the root does. |
| 10 | +
|
| 11 | +Some workspaces outside the root build root crates through path |
| 12 | +dependencies, for example the conformance suites build `openshell-sdk`. Each |
| 13 | +workspace has its own Cargo.lock, and `cargo --locked` only checks a lockfile |
| 14 | +against its own manifest, so nothing stops the two lockfiles from resolving |
| 15 | +the same crate to different versions. The root's versions are the ones CI and |
| 16 | +the Nix toolchain are built for; a newer `aws-lc-sys`, for example, does not |
| 17 | +link against the AWS-LC that Nix provides. |
| 18 | +
|
| 19 | +This check fails when an aligned workspace locks a registry crate that the |
| 20 | +root lockfile also contains at a version the root lockfile does not have. |
| 21 | +With `--fix`, it re-locks each aligned workspace starting from the root |
| 22 | +lockfile. |
| 23 | +""" |
| 24 | + |
| 25 | +from __future__ import annotations |
| 26 | + |
| 27 | +import argparse |
| 28 | +import re |
| 29 | +import shutil |
| 30 | +import subprocess |
| 31 | +import sys |
| 32 | +import tomllib |
| 33 | +from dataclasses import dataclass |
| 34 | +from pathlib import Path |
| 35 | + |
| 36 | +# Workspaces whose shared crates must match the root lockfile. Add a |
| 37 | +# workspace here once its lockfile has been aligned with `--fix`. |
| 38 | +ALIGNED_WORKSPACES = ("tests/suites/conformance",) |
| 39 | + |
| 40 | + |
| 41 | +@dataclass(frozen=True) |
| 42 | +class Mismatch: |
| 43 | + workspace: str |
| 44 | + name: str |
| 45 | + version: str |
| 46 | + root_versions: tuple[str, ...] |
| 47 | + |
| 48 | + def target(self) -> str | None: |
| 49 | + """The root version semver-compatible with the locked one, if any.""" |
| 50 | + key = compat_key(self.version) |
| 51 | + candidates = [v for v in self.root_versions if compat_key(v) == key] |
| 52 | + return max(candidates, key=version_tuple) if candidates else None |
| 53 | + |
| 54 | + |
| 55 | +def version_tuple(version: str) -> tuple[int, ...]: |
| 56 | + core = re.split(r"[-+]", version, maxsplit=1)[0] |
| 57 | + return tuple(int(part) for part in core.split(".")) |
| 58 | + |
| 59 | + |
| 60 | +def compat_key(version: str) -> tuple[int, ...]: |
| 61 | + """Cargo's caret-compatibility class: the leftmost non-zero component.""" |
| 62 | + major, minor, patch = (*version_tuple(version), 0, 0, 0)[:3] |
| 63 | + if major: |
| 64 | + return (major,) |
| 65 | + if minor: |
| 66 | + return (0, minor) |
| 67 | + return (0, 0, patch) |
| 68 | + |
| 69 | + |
| 70 | +def registry_versions(lockfile: Path) -> dict[str, set[str]]: |
| 71 | + """Versions of registry (non-path) packages in a lockfile, by name.""" |
| 72 | + data = tomllib.loads(lockfile.read_text()) |
| 73 | + versions: dict[str, set[str]] = {} |
| 74 | + for package in data.get("package", []): |
| 75 | + if "source" in package: |
| 76 | + versions.setdefault(package["name"], set()).add(package["version"]) |
| 77 | + return versions |
| 78 | + |
| 79 | + |
| 80 | +def mismatches(root: Path, workspace: str) -> list[Mismatch]: |
| 81 | + root_versions = registry_versions(root / "Cargo.lock") |
| 82 | + found = [] |
| 83 | + for name, versions in sorted( |
| 84 | + registry_versions(root / workspace / "Cargo.lock").items() |
| 85 | + ): |
| 86 | + shared = root_versions.get(name) |
| 87 | + if shared is None: |
| 88 | + continue |
| 89 | + for version in sorted(versions - shared, key=version_tuple): |
| 90 | + found.append( |
| 91 | + Mismatch( |
| 92 | + workspace, name, version, tuple(sorted(shared, key=version_tuple)) |
| 93 | + ) |
| 94 | + ) |
| 95 | + return found |
| 96 | + |
| 97 | + |
| 98 | +def fix(root: Path, workspace: str) -> list[Mismatch]: |
| 99 | + """Re-lock the workspace starting from the root lockfile. |
| 100 | +
|
| 101 | + Coupled crates such as the `futures-*` family pin each other, so they |
| 102 | + cannot be moved one at a time. Seeding the workspace lockfile with the |
| 103 | + root's lets Cargo keep every root version that still fits, add only the |
| 104 | + crates the root does not have, and drop the rest. Returns what is still |
| 105 | + misaligned afterwards. |
| 106 | + """ |
| 107 | + shutil.copyfile(root / "Cargo.lock", root / workspace / "Cargo.lock") |
| 108 | + subprocess.run( |
| 109 | + [ |
| 110 | + "cargo", |
| 111 | + "metadata", |
| 112 | + "--format-version", |
| 113 | + "1", |
| 114 | + "--manifest-path", |
| 115 | + str(root / workspace / "Cargo.toml"), |
| 116 | + ], |
| 117 | + check=True, |
| 118 | + stdout=subprocess.DEVNULL, |
| 119 | + ) |
| 120 | + return mismatches(root, workspace) |
| 121 | + |
| 122 | + |
| 123 | +def report(problems: list[Mismatch]) -> None: |
| 124 | + for m in problems: |
| 125 | + target = m.target() |
| 126 | + hint = ( |
| 127 | + f"; root has {target}" |
| 128 | + if target |
| 129 | + else f"; root has {', '.join(m.root_versions)}" |
| 130 | + ) |
| 131 | + print( |
| 132 | + f"error: {m.workspace}: {m.name} {m.version} is not in the root Cargo.lock{hint}", |
| 133 | + file=sys.stderr, |
| 134 | + ) |
| 135 | + |
| 136 | + |
| 137 | +def main(argv: list[str] | None = None) -> int: |
| 138 | + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) |
| 139 | + parser.add_argument( |
| 140 | + "--fix", action="store_true", help="update aligned lockfiles to root versions" |
| 141 | + ) |
| 142 | + args = parser.parse_args(argv) |
| 143 | + |
| 144 | + root = Path( |
| 145 | + subprocess.run( |
| 146 | + ["git", "rev-parse", "--show-toplevel"], |
| 147 | + check=True, |
| 148 | + capture_output=True, |
| 149 | + text=True, |
| 150 | + ).stdout.strip() |
| 151 | + ) |
| 152 | + problems: list[Mismatch] = [] |
| 153 | + for workspace in ALIGNED_WORKSPACES: |
| 154 | + print(f"Checking {workspace}/Cargo.lock against Cargo.lock") |
| 155 | + problems += fix(root, workspace) if args.fix else mismatches(root, workspace) |
| 156 | + |
| 157 | + if problems: |
| 158 | + report(problems) |
| 159 | + if not args.fix: |
| 160 | + print( |
| 161 | + "Run `uv run tasks/scripts/check_cargo_lockfile_alignment.py --fix` to align them.", |
| 162 | + file=sys.stderr, |
| 163 | + ) |
| 164 | + return 1 |
| 165 | + return 0 |
| 166 | + |
| 167 | + |
| 168 | +if __name__ == "__main__": |
| 169 | + sys.exit(main()) |
0 commit comments