Skip to content

🐛 fix(windows): treat empty PUBLIC and fallback env vars as unset - #571

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/win-empty-env
Sep 25, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/win-empty-env

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

On Windows, an empty PUBLIC made user_publicshare_dir() return .. On interpreters that resolve folders from environment variables, an empty LOCALAPPDATA made user_data_dir("MyApp", "Acme") return .\Acme\MyApp, and an empty USERPROFILE gave a relative media folder such as Documents. 🐛 Each of these points at the working directory. #546 moved the PUBLIC lookup ahead of the home directory and noted the empty case without fixing it.

platformdirs treats an empty value as unset in each lookup. user_publicshare_dir() falls back to the Public folder next to the home directory. In the environment variable fallback an empty variable fails with the error an unset one raises, ValueError for LOCALAPPDATA and KeyError for USERPROFILE and APPDATA, and an empty PROGRAMDATA falls through to ALLUSERSPROFILE and C:\ProgramData.

PUBLIC keeps its place ahead of the home directory lookup, so the #546 case of PUBLIC set with no resolvable home keeps returning PUBLIC.

@gaborbernat gaborbernat added the bug Something isn't working label Sep 25, 2026
@gaborbernat
gaborbernat requested a review from ofek as a code owner September 25, 2026 14:28
@gaborbernat
gaborbernat force-pushed the fix/win-empty-env branch 5 times, most recently from 21d009c to dfe31b8 Compare September 25, 2026 15:57
On Windows, PUBLIC="" made user_publicshare_dir return "." and
LOCALAPPDATA="" made the environment variable fallback give
".\Acme\MyApp". The same fallback turned an empty USERPROFILE or APPDATA
into relative media and Start Menu folders such as Documents, and
ignored the ALLUSERSPROFILE default when PROGRAMDATA was empty. These
lookups checked for a missing variable only. The tox-dev#546 PR noted the empty
PUBLIC case without fixing it.

Each lookup treats an empty value as unset. user_publicshare_dir falls
back to the home-based Public folder, LOCALAPPDATA raises the ValueError
of a missing variable, and USERPROFILE and APPDATA raise the KeyError
that os.environ raises when they are unset. An empty PROGRAMDATA falls
through to ALLUSERSPROFILE and C:\ProgramData. The property keeps
reading PUBLIC before it touches the home directory, so the tox-dev#546 fix
holds when Python cannot find the home.
@gaborbernat
gaborbernat merged commit c78e917 into tox-dev:main Sep 25, 2026
36 checks passed
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 26, 2026
user_templates_dir and user_bin_dir joined fixed names onto APPDATA and
LOCALAPPDATA, and user_publicshare_dir read the PUBLIC variable. Windows
lets a user redirect per-user known folders such as FOLDERID_Templates
and FOLDERID_UserProgramFiles; after such a move platformdirs kept
returning the default location. Qt asks for FOLDERID_Templates and
FOLDERID_Public.

The three now share the resolver of the other folders, so a matching
WIN_PD_OVERRIDE_* variable wins. Without ctypes, the registry answers
for Templates. The environment fallback keeps the old joins and reads
PUBLIC before the home directory, treating an empty value as unset as
tox-dev#546 and tox-dev#571 require.
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 26, 2026
user_templates_dir and user_bin_dir joined fixed names onto APPDATA and
LOCALAPPDATA, and user_publicshare_dir read the PUBLIC variable. Windows
lets a user redirect per-user known folders such as FOLDERID_Templates
and FOLDERID_UserProgramFiles; after such a move platformdirs kept
returning the default location. Qt asks for FOLDERID_Templates and
FOLDERID_Public.

The three now share the resolver of the other folders, so a matching
WIN_PD_OVERRIDE_* variable wins. Without ctypes, the registry answers
for Templates. The environment fallback keeps the old joins and reads
PUBLIC before the home directory, treating an empty value as unset as
tox-dev#546 and tox-dev#571 require.

Four tests restored os.environ in the wrong order and left HOME,
USERPROFILE, PUBLIC or APPDATA changed for later tests. On Windows,
SHGetKnownFolderPath expanded the leaked USERPROFILE, so the check
against the default layout needs a clean environment.
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 26, 2026
user_templates_dir and user_bin_dir joined fixed names onto APPDATA and
LOCALAPPDATA, and user_publicshare_dir read the PUBLIC variable. Windows
lets a user redirect per-user known folders such as FOLDERID_Templates
and FOLDERID_UserProgramFiles; after such a move platformdirs kept
returning the default location. Qt asks for FOLDERID_Templates and
FOLDERID_Public.

The three now share the resolver of the other folders, so a matching
WIN_PD_OVERRIDE_* variable wins. Without ctypes, the registry answers
for Templates. The environment fallback keeps the old joins and reads
PUBLIC before the home directory, treating an empty value as unset as
tox-dev#546 and tox-dev#571 require.

Four tests restored os.environ in the wrong order and left HOME,
USERPROFILE, PUBLIC or APPDATA changed for later tests. On Windows,
SHGetKnownFolderPath expanded the leaked USERPROFILE, so the check
against the default layout needs a clean environment.
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 26, 2026
user_templates_dir and user_bin_dir joined fixed names onto APPDATA and
LOCALAPPDATA, and user_publicshare_dir read the PUBLIC variable, so
platformdirs did not ask Windows for these folders. A user can redirect
FOLDERID_Templates and FOLDERID_UserProgramFiles, and after such a move
platformdirs kept returning the default location. Qt asks for
FOLDERID_Templates and FOLDERID_Public.

The three share the resolver of the other folders, so a matching
WIN_PD_OVERRIDE_* variable wins. Without ctypes, the registry answers
for Templates. The environment fallback keeps the old joins and reads
PUBLIC before the home directory, treating an empty value as unset as
tox-dev#546 and tox-dev#571 require.

Four tests restored os.environ in the wrong order. They left HOME,
USERPROFILE, PUBLIC or APPDATA changed for the tests after them, and on
Windows SHGetKnownFolderPath expanded the leaked USERPROFILE.
@gaborbernat
gaborbernat deleted the fix/win-empty-env branch October 3, 2026 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant