Skip to content

fix: stop iter_*_dirs yielding the same directory twice - #524

Merged
gaborbernat merged 6 commits into
tox-dev:mainfrom
darrenhuai:fix/iter-dirs-duplicate-user-site
Aug 24, 2026
Merged

gaborbernat merged 6 commits into
tox-dev:mainfrom
darrenhuai:fix/iter-dirs-duplicate-user-site

Conversation

@darrenhuai

@darrenhuai darrenhuai commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

#520 fixed this for the Unix use_site_for_root case, but the problem is more general: whenever a site_*_dir resolves to the same string as its user_*_dir, the iterator hands the caller that directory twice. Code that merges config or data files, the use case docs/howto.rst recommends these iterators for, then reads and applies the same file twice.

The case most likely to bite is Unix iter_runtime_dirs() with XDG_RUNTIME_DIR set, the normal state on any systemd system:

>>> os.environ["XDG_RUNTIME_DIR"] = "/run/user/1000"
>>> list(Unix(appname="foo").iter_runtime_dirs())
['/run/user/1000/foo', '/run/user/1000/foo']

Both user_runtime_dir and site_runtime_dir read that variable, so the two entries are identical. It slipped through last time because the two tests #520 added both unset XDG_RUNTIME_DIR before asserting, leaving the common configuration untested.

Looking for the same shape elsewhere turned up more. Windows and macOS both define site_runtime_dir as user_runtime_dir, so their iter_runtime_dirs() duplicates too. Android defines every site_*_dir as its user_*_dir, so all six of its iterators returned a pair of identical paths.

Rather than override the iterators per platform a third time, PlatformDirsABC keeps the yield user, yield site logic in a protected _iter_*_dirs and deduplicates at the single public choke point. Unix and macOS override the protected hooks instead of the public methods, so their behaviour does not change and one place owns the invariant. The dedupe preserves order and still lets every distinct entry through; only exact repeats drop out.

Two things the dedupe rests on, each with a test:

  • The _use_site guards in _iter_config_dirs and _iter_data_dirs survive the dedupe rather than becoming redundant. Under multipath the user dir is an os.pathsep-joined string that equals no single site entry, so nothing would drop it.
  • _unique stays lazy. Under ensure_exists reading a site_*_dir creates it, so draining the source up front would create directories for a caller that stops after the first entry.

This is duplicate work rather than a wrong answer, so it stays latent for callers that only do existence checks. It matters for callers that accumulate.

Testing: full suite passes, plus a regression test per affected platform. I mutation-tested the new tests by replacing the dedupe with a passthrough and confirmed all nine fail, then restored it. ty check --error-on-warning, ruff format --check and the -W docs build are clean. Verified on Windows itself and the other platforms through the existing mocking fixtures.

One thing I noticed but did not touch: macOS site_state_dir is documented as "same as site_data_dir" but calls _base_site_dirs(), so it ignores $XDG_DATA_DIRS while site_data_dir honours it. That looks deliberate given there is no XDG_STATE_DIRS and Unix behaves the same way, so I left the code alone, but the docstring misleads either way. Happy to fix it here or separately.

tox-dev#520 fixed this for Unix's use_site_for_root case, but the underlying
problem is more general: whenever a site_*_dir resolves to the same
string as its user_*_dir, the iterator hands the caller that directory
twice, so anything merging config or data files reads and applies the
same file two times.

The case that will actually bite people is Unix iter_runtime_dirs with
XDG_RUNTIME_DIR set, which is the normal state of affairs on any systemd
system. Both user_runtime_dir and site_runtime_dir read that variable,
so the two entries are identical. It slipped through last time because
both of the tests added in tox-dev#520 unset XDG_RUNTIME_DIR before asserting.

The rest fell out of looking for the same shape elsewhere: Windows and
macOS define site_runtime_dir as user_runtime_dir, and Android defines
every site_*_dir as its user_*_dir, so all six of its iterators were
returning a pair of identical paths.

Rather than override the iterators per platform again, the ABC now keeps
the "yield user, yield site" logic in a protected _iter_*_dirs and
deduplicates at the single public choke point. Unix and macOS override
the protected hooks instead, so their existing behaviour is untouched
and there is one place responsible for the invariant.

Worth flagging: macOS site_state_dir is documented as "same as
site_data_dir" but ignores XDG_DATA_DIRS, which site_data_dir honours.
That looks deliberate given there is no XDG_STATE_DIRS, so I left it
alone, but the docstring is misleading either way.
pre-commit-ci Bot and others added 4 commits August 17, 2026 09:07
Order each public iter_*_dirs above the _iter_*_dirs hook it calls, so
PlatformDirsABC reads top-down.

Drop the getuid patch from the Unix test and the _resolve_win_folder
patch from the Windows test: neither is reached, and test_windows.py
already patches get_win_folder through an autouse fixture. Use the
_LOCAL constant the rest of test_windows.py uses for the expected path,
and give the Android cases readable parametrize ids.

Reword the changelog fragment and the docs/api.rst note to drop the
trailing sentence that restated the first one.
iter_config_dirs deduplicates by exact string, so under multipath the
os.pathsep-joined user_config_dir matches no single site entry and the
_use_site guard in _iter_config_dirs has to drop it. _unique stays lazy
because reading a site_*_dir under ensure_exists creates it, and a caller
that stops after the first entry must not pay for the rest.

Both were load-bearing but untested and uncommented; removing the guard
or swapping _unique for dict.fromkeys now fails a test.
test_unix.py patched getuid, os.access and XDG_RUNTIME_DIR inline across
eight tests; test_macos.py recomputed the home directory in nine and the
Homebrew sys.prefix in four; test_android.py patched the example app
folder in two. Each is a fixture now.

Also parametrize the multipath guard test over config and data, which
are the two iterators whose site dirs are lists.
@gaborbernat
gaborbernat merged commit c653668 into tox-dev:main Aug 24, 2026
36 checks passed
gaborbernat added a commit that referenced this pull request Aug 24, 2026
The example under "Merging config from multiple sources" merges in the
wrong direction. Its comment claims it iterates "from least specific
(site) to most specific (user)", but the iterators yield the user
directory first:

```pycon
>>> list(PlatformDirs("MyApp").iter_config_paths())
[PosixPath('/Users/me/Library/Application Support/MyApp'), PosixPath('/Library/Application Support/MyApp')]
```

So `config.update()` applies the site file last and the site defaults
overwrite the user's own config, the opposite of what the paragraph
above the block promises.

With a site `config.json` of `{"theme": "site-default", "lang": "en"}`
and a user one of `{"theme": "user-choice"}`, the documented loop yields
`theme: site-default`. Reversing it yields `theme: user-choice` and
still inherits `lang` from the site file.

Found while reviewing #524, which cites this example as the use case its
iterators serve. Docs only, no behaviour change.
gaborbernat added a commit that referenced this pull request Aug 27, 2026
Follow-up to #524, which landed and shipped in 4.11.4 before this review
finished.

`docs/api.rst` now claims:

> User directories come first, then site directories, and each distinct
directory appears once.

The first half is wrong when `use_site_for_root` is active and the
process is root. `_iter_*_dirs` skips the user directory outright rather
than yielding it first, so under `multipath` the first entry is a site
directory that does not equal `user_*_dir`:

```pycon
>>> dirs = Unix(appname="foo", multipath=True, use_site_for_root=True)   # as root, XDG_CONFIG_DIRS=/xdg/a:/xdg/b
>>> dirs.user_config_dir
'/xdg/a/foo:/xdg/b/foo'
>>> list(dirs.iter_config_dirs())
['/xdg/a/foo', '/xdg/b/foo']
```

`test_iter_dirs_as_root_with_multipath_skips_joined_user_dir` already
pins that behaviour, so the code is right and only the sentence is
wrong. "The most specific directory comes first" holds in every
configuration and still tells callers what they need to merge in the
right direction.

Two cleanups in the same area while I was there.
`test_use_site_for_root_bypasses_xdg_user_vars` kept an inline
`monkeypatch.delenv("XDG_RUNTIME_DIR", ...)` after gaining the
`_no_xdg_runtime_dir` fixture that does the same thing. And two test
signatures stayed exploded across four lines only because a magic
trailing comma survived the removal of their `mocker` and `monkeypatch`
parameters; both fit on one line now.

No behaviour change. `tox -e fix`, `-e type`, `-e docs` clean, full
suite passes.
darrenhuai added a commit to darrenhuai/platformdirs that referenced this pull request Sep 17, 2026
With ensure_exists=True, site_data_dir and site_config_dir create every
entry in the site list, not just the one they return. The lists are
built with _append_app_name_and_version, which runs mkdir as a side
effect, and the property only picks dirs[0] afterwards.

On Linux, Unix(appname="app", ensure_exists=True).site_data_dir returns
/usr/local/share/app but also creates /usr/share/app, and with
XDG_DATA_DIRS set it creates a directory in every entry of the list. On
a Homebrew Python on macOS it's worse: site_data_dir, site_config_dir
and site_state_dir return the user-owned $prefix/share/app, but first
try to create /Library/Application Support/app, which a normal user
can't write, so the call raises PermissionError over a directory nobody
asked for. site_cache_dir does the same thing with /Library/Caches.

The iterators hit the same problem one level down. tox-dev#524 kept _unique
lazy so that a caller who stops early doesn't get directories created
for entries it never read, but `yield from self._site_data_dirs` builds
(and so creates) the whole list the moment the first site entry is
requested.

The site lists are now built with a join that doesn't touch the disk.
site_*_dir creates exactly what it returns, which is the first entry or,
under multipath, all of them as before, and the iterators create each
site entry only as it is yielded. 4.0.0 still sliced the Unix list
before creating anything, so on Unix this puts back the old behaviour;
the eager macOS lists go back to the original Homebrew support.
gaborbernat added a commit that referenced this pull request Sep 18, 2026
With `ensure_exists=True`, `site_data_dir` and `site_config_dir` create
every entry in the site list, not only the one they return. The list
gets built with `_append_app_name_and_version`, which calls `mkdir`
along the way, and the property picks `dirs[0]` after that.

On Linux you get `/usr/share/app` next to the `/usr/local/share/app` you
asked for, plus a directory in every `XDG_DATA_DIRS` entry. Homebrew
Python on macOS just fails:

```pycon
>>> PlatformDirs("app", ensure_exists=True).site_data_dir
PermissionError: [Errno 13] Permission denied: '/Library/Application Support/app'
```

The dir it would return is `$(brew --prefix)/share/app`, which the user
owns, but building the list tries `/Library/Application Support/app`
first. `site_config_dir` and `site_state_dir` hit the same thing, and so
does `site_cache_dir` with `/Library/Caches`.

The iterators have it too. #524 kept `_unique` lazy so a caller that
stops early doesn't get directories made for entries it never looked at.
That doesn't help here, because `yield from self._site_data_dirs` builds
the whole list (creating all of it) when the first site entry is asked
for.

The fix adds `_join_app_name_and_version` to `api.py`. It builds the
path without touching the disk, and the site lists now use it.
`site_*_dir` creates exactly what it returns, so the first entry, or
every entry under `multipath` like before. The iterators create each
site entry as they yield it. On Unix this is back to how 4.0.0 behaved,
since it sliced the list before creating anything. The macOS lists have
been eager since Homebrew support went in.

I didn't touch the applications, bin, fonts and media site dirs.
`ensure_exists` never created those, and whether it should is its own
question.

The new tests in `test_unix.py` and `test_macos.py` record each `mkdir`
call and check that only the returned or yielded dirs get created. With
`src` reset to `main`, 13 of the new cases fail.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Bernát Gábor <[email protected]>
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.

2 participants