Skip to content

🐛 fix(windows): avoid FileNotFoundError in sandboxed environments#422

Merged
gaborbernat merged 3 commits intotox-dev:mainfrom
gaborbernat:421
Feb 13, 2026
Merged

🐛 fix(windows): avoid FileNotFoundError in sandboxed environments#422
gaborbernat merged 3 commits intotox-dev:mainfrom
gaborbernat:421

Conversation

@gaborbernat
Copy link
Member

@gaborbernat gaborbernat commented Feb 13, 2026

platformdirs 4.7.0 throws FileNotFoundError on Windows when USERPROFILE points to a directory without AppData\Local. This regression was introduced in #380, which replaced SHGetFolderPathW with SHGetKnownFolderPath. 🐛 Unlike the old API, SHGetKnownFolderPath verifies folder existence by default and raises when the directory is missing — breaking sandboxed environments, CI containers, and custom user profiles.

The fix passes KF_FLAG_DONT_VERIFY (0x00004000) as the dwFlags parameter, telling SHGetKnownFolderPath to return the expected path without checking whether it physically exists. This restores the 4.6.0 behavior while keeping the modern API. The flag is defined as a Final[int] module constant since CPython doesn't expose Windows SDK known-folder flags.

Closes #421

@gaborbernat gaborbernat requested a review from ofek as a code owner February 13, 2026 16:35
@gaborbernat gaborbernat changed the title 🐛 fix(windows): pass KF_FLAG_DONT_VERIFY to SHGetKnownFolderPath 🐛 fix(windows): avoid FileNotFoundError in sandboxed environments Feb 13, 2026
SHGetKnownFolderPath with default flags (0) verifies that the target
folder physically exists, raising FileNotFoundError in sandboxed
Windows environments where USERPROFILE points to a directory without
AppData\Local. This was a regression introduced in 4.7.0 when PR tox-dev#380
replaced SHGetFolderPathW with SHGetKnownFolderPath.

Passing KF_FLAG_DONT_VERIFY (0x00004000) restores the pre-4.7.0
behavior of returning the expected path without existence checks,
matching how the old SHGetFolderPathW API worked.

Closes tox-dev#421

Signed-off-by: Bernát Gábor <bgabor8@bloomberg.net>
On Windows, call get_win_folder_via_ctypes with the real Windows API for
each CSIDL name to verify integration. Reserve mock-based tests for
non-Windows where ctypes Windows bindings don't exist.
Move ctypes.WinDLL mock assignment into _setup_ctypes_mocks so the
type: ignore comment only exists in one place that ty can reason about.
@gaborbernat gaborbernat merged commit fe5bf83 into tox-dev:main Feb 13, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exception when querying user dirs in sandboxed Windows environments [regression in 4.7.0]

1 participant