Skip to content

Commit 9eb525b

Browse files
fix(shared): keep LAMBDA_TASK_ROOT POSIX-shaped off Linux
abs_lambda_path joined LAMBDA_TASK_ROOT with pathlib.Path, which is WindowsPath off Linux, so str(Path("/var/task", "")) returned "\var\task". The docstring promises the environment variable is used as given, and the repo's own test_abs_lambda_path_empty_envvar fails on an unmodified Windows checkout. CI is ubuntu-latest only, so it never surfaced there. The Lambda runtime is always Linux, so use PurePosixPath when LAMBDA_TASK_ROOT is set and keep the existing Path behaviour for the unset local case. Behaviour on Linux is unchanged. test_abs_lambda_path_w_filename_envvar built its expected value with the same platform-dependent Path call, so it passed either way and masked the bug; it now asserts the POSIX result directly.
1 parent 51c89a3 commit 9eb525b

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

‎aws_lambda_powertools/shared/functions.py‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import re
88
import warnings
99
from binascii import Error as BinAsciiError
10-
from pathlib import Path
10+
from pathlib import Path, PurePosixPath
1111
from typing import TYPE_CHECKING, Any, TypeGuard, overload
1212

1313
from aws_lambda_powertools.shared import constants
@@ -280,10 +280,17 @@ def abs_lambda_path(relative_path: str = "") -> str:
280280
Otherwise, it will use the current working directory.
281281
If the path is empty, it will return the current working directory.
282282
"""
283-
# Retrieve the LAMBDA_TASK_ROOT environment variable or default to an empty string
284-
current_working_directory = os.environ.get("LAMBDA_TASK_ROOT", "") or str(Path.cwd())
285-
286-
return str(Path(current_working_directory, relative_path))
283+
# The Lambda runtime is Linux, so LAMBDA_TASK_ROOT is always a POSIX path. Joining it with
284+
# pathlib.Path rewrites it with the separators of whatever platform this code runs on, so a
285+
# developer running the suite on Windows would get "\var\task" instead of "/var/task".
286+
# PurePosixPath keeps the value the runtime gave us intact on every platform.
287+
lambda_task_root = os.environ.get("LAMBDA_TASK_ROOT", "")
288+
if lambda_task_root:
289+
return str(PurePosixPath(lambda_task_root, relative_path))
290+
291+
# Off Lambda there is no task root, so fall back to the current working directory and let
292+
# pathlib use the local platform's separators.
293+
return str(Path(Path.cwd(), relative_path))
287294

288295

289296
def sanitize_xray_segment_name(name: str) -> str:

‎tests/unit/test_shared_functions.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,10 @@ def test_abs_lambda_path_w_filename_envvar(default_lambda_path):
177177
# Given Env is set and relative_path provided
178178
relative_path = "cert/pub.cert"
179179
os.environ["LAMBDA_TASK_ROOT"] = default_lambda_path
180-
# Then path = env + relative_path
181-
assert abs_lambda_path(relative_path="cert/pub.cert") == str(Path(os.environ["LAMBDA_TASK_ROOT"], relative_path))
180+
# Then path = env + relative_path, joined with POSIX separators on every platform because the
181+
# Lambda runtime is Linux. Building the expectation with Path() would hide a native-separator
182+
# rewrite, since both sides would be rewritten the same way.
183+
assert abs_lambda_path(relative_path=relative_path) == f"{default_lambda_path}/{relative_path}"
182184

183185

184186
def test_sanitize_xray_segment_name():

0 commit comments

Comments
 (0)