Skip to content

fix: only create the site dirs a call hands back - #550

Merged
gaborbernat merged 6 commits into
tox-dev:mainfrom
darrenhuai:fix-ensure-exists-site-dirs
Sep 18, 2026
Merged

gaborbernat merged 6 commits into
tox-dev:mainfrom
darrenhuai:fix-ensure-exists-site-dirs

Conversation

@darrenhuai

@darrenhuai darrenhuai commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

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:

>>> 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.

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
Every release commit leaves `docs/changelog.rst` in a shape `docstrfmt`
disagrees with: towncrier's header overline is one character short of
what `docstrfmt -l 120` wants, and its line wrapping doesn't match
either. Nothing runs `docstrfmt` before that commit lands on `main`, so
the reformat shows up later as an unrelated diff in whichever PR
pre-commit.ci next touches. #549 and #550 each carry exactly that
reformat, with nothing to do with their actual change.

The fix runs the `docstrfmt` pre-commit hook against the generated
changelog in `prepare-release.yaml`, right after `towncrier build` and
before the release commit, so the formatting is already correct when it
lands. `pre-commit` reports a hook that rewrites a file as a failure, so
the step is allowed to fail without breaking the job.

Verified locally: replaying `towncrier build` plus this `docstrfmt` step
against the pre-#549 changelog produces output identical to what
pre-commit.ci fixed up afterward, and a dry-run release from current
`main` needs no follow-up reformat.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 18, 2026
towncrier's header overline and line wrapping don't match docstrfmt's
120-column style, so every release commit left docs/changelog.rst in
a shape docstrfmt wanted to rewrite. Nothing ran docstrfmt before that
commit landed on main, so pre-commit.ci reformatted it on whichever PR
happened to touch the repo next, carrying an unrelated diff into that
PR's changes (tox-dev#549, tox-dev#550).

Running the docstrfmt hook on the generated changelog right after
towncrier build, before the release commit, fixes the formatting at
the source instead of downstream.
site_data_path, site_config_path, and site_cache_path called into
site_*_dir, which creates every entry in the list under multipath even
though the *_path property only ever returns the first one. On
Homebrew Python, asking for the path form still hit the same
PermissionError this PR already fixed for the plain site_*_dir form.

Both platforms now build the site dir list without touching disk and
create only the entry the *_path property returns. Bundled in: a
macOS site_state_dir docstring that claimed multipath behavior it
doesn't have, and missing parametrize ids on an existing Unix test.
@gaborbernat
gaborbernat enabled auto-merge (squash) September 18, 2026 01:31
@gaborbernat
gaborbernat merged commit 7d5c85d into tox-dev:main Sep 18, 2026
36 checks passed
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 26, 2026
Apps that layer a user config over site defaults had to walk
iter_config_paths and test each candidate themselves. Under
ensure_exists=True that walk writes to disk: looking up an app.toml
that lived only in the second XDG_CONFIG_DIRS entry created the user
config dir and the first site dir, because the iterators create each
directory they yield (tox-dev#550).

The new pair per kind follows the XDG Base Directory rule that the
user base directory outranks the site ones, the order pyxdg's
load_first_config uses too. I reused the iter_*_paths order on a copy
of the instance with ensure_exists off, so precedence and dedupe stay
defined in one place and the lookup only reads. Only regular files
match. The name goes through the escape check appname uses; an
absolute path or a .. part raises ValueError.
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 26, 2026
Apps that layer a user config over site defaults had to walk
iter_config_paths and test each candidate themselves. Under
ensure_exists=True that walk writes to disk. Looking up an app.toml that
lived only in the second XDG_CONFIG_DIRS entry created the user config
dir and the first site dir, because the iterators create each directory
they yield (tox-dev#550).

The new pair per kind follows the XDG Base Directory rule that the user
base directory outranks the site ones, the order pyxdg's
load_first_config uses too. I reused the iter_*_paths order on a copy of
the instance with ensure_exists off, so precedence and dedupe stay
defined in one place and the lookup only reads. Only regular files
match. The name goes through the escape check appname uses; an absolute
path or a .. part raises ValueError.
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 26, 2026
Apps that layer a user config over site defaults had to walk
iter_config_paths and test each candidate themselves. Under
ensure_exists=True that walk writes to disk. Looking up an app.toml that
lived only in the second XDG_CONFIG_DIRS entry created the user config
dir and the first site dir, because the iterators create each directory
they yield (tox-dev#550).

The new pair per kind follows the XDG Base Directory rule that the user
base directory outranks the site ones, the order pyxdg's
load_first_config uses too. I reused the iter_*_paths order on a copy of
the instance with ensure_exists off, so precedence and dedupe stay
defined in one place and the lookup only reads. Only regular files
match. The name goes through the escape check appname uses; an absolute
path or a .. part raises ValueError.
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 26, 2026
Apps that layer a user config over site defaults had to walk
iter_config_paths and test each candidate themselves. Under
ensure_exists=True that walk writes to disk. Looking up an app.toml that
lived only in the second XDG_CONFIG_DIRS entry created the user config
dir and the first site dir, because the iterators create each directory
they yield (tox-dev#550).

The new pair per kind follows the XDG Base Directory rule that the user
base directory outranks the site ones, the order pyxdg's
load_first_config uses too. I reused the iter_*_paths order on a copy of
the instance with ensure_exists off, so precedence and dedupe stay
defined in one place and the lookup only reads. Only regular files
match. The name goes through the escape check appname uses; an absolute
path or a .. part raises ValueError.
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