Repository navigation
fix: only create the site dirs a call hands back - #550
Merged
gaborbernat merged 6 commits intoSep 18, 2026
Merged
Conversation
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
approved these changes
Sep 18, 2026
gaborbernat
enabled auto-merge (squash)
September 18, 2026 01:31
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With
ensure_exists=True,site_data_dirandsite_config_dircreate every entry in the site list, not only the one they return. The list gets built with_append_app_name_and_version, which callsmkdiralong the way, and the property picksdirs[0]after that.On Linux you get
/usr/share/appnext to the/usr/local/share/appyou asked for, plus a directory in everyXDG_DATA_DIRSentry. Homebrew Python on macOS just fails:The dir it would return is
$(brew --prefix)/share/app, which the user owns, but building the list tries/Library/Application Support/appfirst.site_config_dirandsite_state_dirhit the same thing, and so doessite_cache_dirwith/Library/Caches.The iterators have it too. #524 kept
_uniquelazy so a caller that stops early doesn't get directories made for entries it never looked at. That doesn't help here, becauseyield from self._site_data_dirsbuilds the whole list (creating all of it) when the first site entry is asked for.The fix adds
_join_app_name_and_versiontoapi.py. It builds the path without touching the disk, and the site lists now use it.site_*_dircreates exactly what it returns, so the first entry, or every entry undermultipathlike 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_existsnever created those, and whether it should is its own question.The new tests in
test_unix.pyandtest_macos.pyrecord eachmkdircall and check that only the returned or yielded dirs get created. Withsrcreset tomain, 13 of the new cases fail.