From b50cf4da1b52ab01c75e7ee349e677c0f86e2d5d Mon Sep 17 00:00:00 2001 From: Roger Zhang Date: Tue, 13 Oct 2026 17:33:41 +0000 Subject: [PATCH 1/2] fix: align executable lookup on Windows with other platforms which() no longer prepends the working directory to the search path on Windows, ignores empty PATH entries, and returns absolute paths. The Python UV workflow now uses the same lookup. Adds unit tests. --- aws_lambda_builders/utils.py | 9 ++-- .../workflows/python_uv/utils.py | 7 ++- tests/unit/test_utils.py | 46 ++++++++++++++++++- tests/unit/workflows/python_uv/test_utils.py | 5 ++ 4 files changed, 59 insertions(+), 8 deletions(-) diff --git a/aws_lambda_builders/utils.py b/aws_lambda_builders/utils.py index dc3dac294..a0cb43bb5 100644 --- a/aws_lambda_builders/utils.py +++ b/aws_lambda_builders/utils.py @@ -175,10 +175,6 @@ def _access_check(fn, mode): path = executable_search_paths + path if sys.platform == "win32": - # The current directory takes precedence on Windows. - if os.curdir not in path: - path.insert(0, os.curdir) - # PATHEXT is necessary to check on Windows. pathext = os.environ.get("PATHEXT", "").split(os.pathsep) # See if the given file matches any of the expected path @@ -198,13 +194,16 @@ def _access_check(fn, mode): paths = [] for dir in path: + if not dir: + # Skip empty entries (e.g. from a trailing path separator), which would resolve to the working directory + continue normdir = os.path.normcase(dir) if normdir not in seen: seen.add(normdir) for thefile in files: name = os.path.join(dir, thefile) if _access_check(name, mode): - paths.append(name) + paths.append(os.path.abspath(name)) return paths diff --git a/aws_lambda_builders/workflows/python_uv/utils.py b/aws_lambda_builders/workflows/python_uv/utils.py index 39bba89a0..4f7b3a39d 100644 --- a/aws_lambda_builders/workflows/python_uv/utils.py +++ b/aws_lambda_builders/workflows/python_uv/utils.py @@ -5,10 +5,10 @@ import logging import os import re -import shutil import subprocess from typing import List, Optional +from aws_lambda_builders.utils import which from aws_lambda_builders.workflows.python_pip.utils import OSUtils as BaseOSUtils LOG = logging.getLogger(__name__) @@ -26,7 +26,10 @@ class OSUtils(BaseOSUtils): def which(self, executable): """Find executable in PATH.""" - return shutil.which(executable) + paths = which(executable) + if isinstance(paths, str): + return paths + return paths[0] if paths else None def run_subprocess(self, cmd, cwd=None, env=None): """Run subprocess and return result.""" diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index d7c801f63..a56cfa71a 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -1,5 +1,6 @@ import os import platform +import stat import tempfile from pathlib import Path @@ -7,7 +8,7 @@ from unittest.mock import patch, Mock, MagicMock from aws_lambda_builders import utils -from aws_lambda_builders.utils import decode +from aws_lambda_builders.utils import decode, which class Test_create_symlink_or_copy(TestCase): @@ -268,3 +269,46 @@ def test_escaping_with_parent_reference_is_not_within(self): @patch("aws_lambda_builders.utils.os.path.commonpath", side_effect=ValueError("different drives")) def test_paths_on_different_drives_are_not_within(self, _): self.assertFalse(utils._is_within_directory("C:\\my\\path", "D:\\file")) + + +class TestWhich(TestCase): + def setUp(self): + self.temp_dir = tempfile.TemporaryDirectory() + root = Path(self.temp_dir.name) + self.working_dir = root / "working" + self.path_dir = root / "bin" + self.working_dir.mkdir() + self.path_dir.mkdir() + self.original_cwd = os.getcwd() + os.chdir(self.working_dir) + + def tearDown(self): + os.chdir(self.original_cwd) + self.temp_dir.cleanup() + + @staticmethod + def _create_executable(path): + path.write_text("") + path.chmod(path.stat().st_mode | stat.S_IXUSR) + + @patch("aws_lambda_builders.utils.sys") + def test_windows_lookup_uses_only_search_paths(self, mock_sys): + mock_sys.platform = "win32" + self._create_executable(self.working_dir / "tool.CMD") + self._create_executable(self.path_dir / "tool.CMD") + env = {"PATH": str(self.path_dir), "PATHEXT": os.pathsep.join([".EXE", ".CMD"])} + + with patch.dict(os.environ, env): + self.assertEqual(which("tool"), [os.path.abspath(self.path_dir / "tool.CMD")]) + + def test_ignores_empty_path_entries(self): + self._create_executable(self.working_dir / "tool") + + with patch.dict(os.environ, {"PATH": str(self.path_dir) + os.pathsep}): + self.assertEqual(which("tool"), []) + + def test_returns_absolute_paths(self): + self._create_executable(self.working_dir / "tool") + + with patch.dict(os.environ, {"PATH": str(self.path_dir)}): + self.assertEqual(which("tool", executable_search_paths=["."]), [os.path.join(os.getcwd(), "tool")]) diff --git a/tests/unit/workflows/python_uv/test_utils.py b/tests/unit/workflows/python_uv/test_utils.py index e1966a7a4..f4e65102c 100644 --- a/tests/unit/workflows/python_uv/test_utils.py +++ b/tests/unit/workflows/python_uv/test_utils.py @@ -28,6 +28,11 @@ def test_which_nonexistent_command(self): result = self.osutils.which("nonexistent_command_12345") self.assertIsNone(result) + @patch("aws_lambda_builders.workflows.python_uv.utils.which") + def test_which_returns_first_match(self, mock_which): + mock_which.return_value = ["/first/uv", "/second/uv"] + self.assertEqual(self.osutils.which("uv"), "/first/uv") + def test_run_subprocess_success(self): rc, stdout, stderr = self.osutils.run_subprocess(["echo", "hello"]) self.assertEqual(rc, 0) From bf6d36e20423139e85e649034b4a173bf002f2fb Mon Sep 17 00:00:00 2001 From: Roger Zhang Date: Mon, 16 Nov 2026 19:42:16 +0000 Subject: [PATCH 2/2] test: use the file name that which() looks for on each platform --- tests/unit/test_utils.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index a56cfa71a..328141294 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -1,6 +1,7 @@ import os import platform import stat +import sys import tempfile from pathlib import Path @@ -291,6 +292,16 @@ def _create_executable(path): path.write_text("") path.chmod(path.stat().st_mode | stat.S_IXUSR) + @staticmethod + def _tool_file_and_env(): + """ + Return the file name that which("tool") looks for on this platform, and the environment it needs. + On Windows, which() only tries the name with each PATHEXT extension. + """ + if sys.platform == "win32": + return "tool.CMD", {"PATHEXT": os.pathsep.join([".EXE", ".CMD"])} + return "tool", {} + @patch("aws_lambda_builders.utils.sys") def test_windows_lookup_uses_only_search_paths(self, mock_sys): mock_sys.platform = "win32" @@ -302,13 +313,17 @@ def test_windows_lookup_uses_only_search_paths(self, mock_sys): self.assertEqual(which("tool"), [os.path.abspath(self.path_dir / "tool.CMD")]) def test_ignores_empty_path_entries(self): - self._create_executable(self.working_dir / "tool") + tool_file, env = self._tool_file_and_env() + self._create_executable(self.working_dir / tool_file) + env["PATH"] = str(self.path_dir) + os.pathsep - with patch.dict(os.environ, {"PATH": str(self.path_dir) + os.pathsep}): + with patch.dict(os.environ, env): self.assertEqual(which("tool"), []) def test_returns_absolute_paths(self): - self._create_executable(self.working_dir / "tool") + tool_file, env = self._tool_file_and_env() + self._create_executable(self.working_dir / tool_file) + env["PATH"] = str(self.path_dir) - with patch.dict(os.environ, {"PATH": str(self.path_dir)}): - self.assertEqual(which("tool", executable_search_paths=["."]), [os.path.join(os.getcwd(), "tool")]) + with patch.dict(os.environ, env): + self.assertEqual(which("tool", executable_search_paths=["."]), [os.path.join(os.getcwd(), tool_file)])