From 1a0aa700013b97ecdfc961695585bde02a2c695b Mon Sep 17 00:00:00 2001 From: narayana006 <69020110+narayana006@users.noreply.github.com> Date: Mon, 30 Nov 2026 16:13:20 -0500 Subject: [PATCH] fix(python-uv): use configured indexes when generating locks --- .../workflows/python_uv/packager.py | 1 + .../workflows/python_uv/utils.py | 10 +- .../workflows/python_uv/test_indexes.py | 98 +++++++++++++++++++ 3 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 tests/functional/workflows/python_uv/test_indexes.py diff --git a/aws_lambda_builders/workflows/python_uv/packager.py b/aws_lambda_builders/workflows/python_uv/packager.py index b704acf58..93d22c810 100644 --- a/aws_lambda_builders/workflows/python_uv/packager.py +++ b/aws_lambda_builders/workflows/python_uv/packager.py @@ -663,6 +663,7 @@ def _build_from_pyproject( lock_args = ["lock", "--no-progress"] if python_version: lock_args.extend(["--python", python_version]) + lock_args.extend(config.index_args()) # uv lock also needs a writable cache, same as export and pip install. lock_args.extend(config.cache_args()) diff --git a/aws_lambda_builders/workflows/python_uv/utils.py b/aws_lambda_builders/workflows/python_uv/utils.py index 4f7b3a39d..aafc1a79d 100644 --- a/aws_lambda_builders/workflows/python_uv/utils.py +++ b/aws_lambda_builders/workflows/python_uv/utils.py @@ -195,16 +195,18 @@ def cache_args(self) -> List[str]: args.append("--no-cache") return args - def to_uv_args(self) -> List[str]: - """Convert configuration to UV command line arguments.""" + def index_args(self) -> List[str]: + """Share package index arguments between lock generation and installation.""" args = [] - if self.index_url: args.extend(["--index-url", self.index_url]) - for extra_url in self.extra_index_urls: args.extend(["--extra-index-url", extra_url]) + return args + def to_uv_args(self) -> List[str]: + """Convert configuration to UV command line arguments.""" + args = self.index_args() args.extend(self.cache_args()) if self.prerelease != "disallow": diff --git a/tests/functional/workflows/python_uv/test_indexes.py b/tests/functional/workflows/python_uv/test_indexes.py new file mode 100644 index 000000000..b097b10b4 --- /dev/null +++ b/tests/functional/workflows/python_uv/test_indexes.py @@ -0,0 +1,98 @@ +import base64 +import hashlib +import os +import shutil +import subprocess +import sys +import zipfile + +import pytest + +from aws_lambda_builders.workflows.python_uv.exceptions import UvBuildError +from aws_lambda_builders.workflows.python_uv.packager import PythonUvDependencyBuilder +from aws_lambda_builders.workflows.python_uv.utils import UvConfig + + +@pytest.fixture +def indexed_project(tmp_path, monkeypatch): + project = tmp_path / "project" + project.mkdir() + manifest = project / "pyproject.toml" + manifest.write_text( + '[project]\nname="function-demo"\nversion="0.0.0"\nrequires-python=">=3.10"\n' + 'dependencies=["lambda-builders-index-demo==1.0.0"]\n', + encoding="utf-8", + ) + index = tmp_path / "index" + package = index / "lambda-builders-index-demo" + package.mkdir(parents=True) + wheel = package / "lambda_builders_index_demo-1.0.0-py3-none-any.whl" + with zipfile.ZipFile(wheel, "w") as archive: + archive.writestr("lambda_builders_index_demo.py", "VALUE = 42\n") + archive.writestr( + "lambda_builders_index_demo-1.0.0.dist-info/METADATA", + "Metadata-Version: 2.1\nName: lambda-builders-index-demo\nVersion: 1.0.0\n", + ) + archive.writestr( + "lambda_builders_index_demo-1.0.0.dist-info/WHEEL", + "Wheel-Version: 1.0\nRoot-Is-Purelib: true\nTag: py3-none-any\n", + ) + records = [] + for name in archive.namelist(): + content = archive.read(name) + digest = base64.urlsafe_b64encode(hashlib.sha256(content).digest()).rstrip(b"=").decode("ascii") + records.append(f"{name},sha256={digest},{len(content)}\n") + record_path = "lambda_builders_index_demo-1.0.0.dist-info/RECORD" + archive.writestr(record_path, "".join(records) + f"{record_path},,\n") + (package / "index.html").write_text(f'wheel\n', encoding="utf-8") + artifacts = tmp_path / "artifacts" + artifacts.mkdir() + scratch = tmp_path / "scratch" + scratch.mkdir() + for name in list(os.environ): + if name.startswith("UV_"): + monkeypatch.delenv(name) + monkeypatch.setenv("UV_OFFLINE", "1") + monkeypatch.setenv("UV_PYTHON_DOWNLOADS", "never") + monkeypatch.setenv("UV_NO_CONFIG", "1") + monkeypatch.setenv("UV_CACHE_DIR", str(tmp_path / "cache")) + return project, manifest, index, artifacts, scratch + + +@pytest.mark.skipif(shutil.which("uv") is None, reason="uv not available") +@pytest.mark.parametrize("locked", [False, True]) +@pytest.mark.parametrize("index_option", ["index_url", "extra_index_urls"]) +def test_build_pyproject_with_configured_index(indexed_project, index_option, locked): + project, manifest, index, artifacts, scratch = indexed_project + python_version = f"{sys.version_info.major}.{sys.version_info.minor}" + index_value = index.as_uri() if index_option == "index_url" else [index.as_uri()] + config = UvConfig(**{index_option: index_value}) + lock_path = project / "uv.lock" + if locked: + # Independently establish that the fixture is valid and preserve this lock during the build. + subprocess.run( + ["uv", "lock", "--python", python_version, "--index-url", index.as_uri()], + cwd=project, + check=True, + capture_output=True, + ) + original_lock = lock_path.read_bytes() + + PythonUvDependencyBuilder(runtime=f"python{python_version}").build_dependencies( + str(artifacts), str(scratch), str(manifest), config=config + ) + + assert (artifacts / "lambda_builders_index_demo.py").read_text(encoding="utf-8") == "VALUE = 42\n" + if locked: + assert lock_path.read_bytes() == original_lock + + +@pytest.mark.skipif(shutil.which("uv") is None, reason="uv not available") +def test_unconfigured_index_does_not_resolve_offline_package(indexed_project): + _, manifest, _, artifacts, scratch = indexed_project + python_version = f"{sys.version_info.major}.{sys.version_info.minor}" + with pytest.raises(UvBuildError): + PythonUvDependencyBuilder(runtime=f"python{python_version}").build_dependencies( + str(artifacts), str(scratch), str(manifest), config=UvConfig() + ) + assert not (artifacts / "lambda_builders_index_demo.py").exists()