Skip to content

🐛 fix(dirs): create media dirs when ensure_exists is set - #560

Merged
gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:fix/ensure-exists-media-dirs
Sep 25, 2026
Merged

gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:fix/ensure-exists-media-dirs

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

ensure_exists creates user_data_dir and the other app directories on access, but it left the media directories such as user_documents_dir as they were. A path from XDG_DOCUMENTS_DIR or user-dirs.dirs did not exist after the call, although docs/parameters.rst promised creation on access. #558 reported it. 🐛

On Unix the media properties create the directory and its parents when an XDG variable or user-dirs.dirs sets the path. They create the ~/Documents style default too. On macOS they create the directory when an XDG variable sets it, and with ensure_exists=False platformdirs creates nothing.

The first commit is @ekanshul's work from #559, which had maintainer edits disabled. The second commit carries the review fixes, among them the changelog fragment name.

Windows and Android return their media directories without creating them. So does macOS without an XDG variable. #561 narrowed the Unix behaviour to directories the user configured, after the default-directory creation left stray folders in headless home directories.

ekanshul and others added 2 commits September 24, 2026 17:48
Media dirs from XDG_*_DIR and user-dirs.dirs skipped
_optionally_create_directory, so ensure_exists=True did
not create them or their parents.

Fixes tox-dev#558
The macOS test built its XDG value from a Windows tmp_path, which _xdg_dir
rejects because it only accepts POSIX absolute paths. All seven Windows
jobs failed on it. The test now mocks Path.mkdir with a fixed POSIX path,
the same boundary the site directory tests use.

The Unix tests covered three of nine media properties and only the created
case. They now cover every property with ensure_exists on and off, plus the
user-dirs.dirs and default fallbacks, so a wrong variable name or a
creation while off fails a test.

The changelog fragment moves to the PR number so towncrier links it, and
wraps at 120 columns so docstrfmt stops rewriting it in pre-commit.ci. The
two media helpers now sit below the properties that call them.
@gaborbernat
gaborbernat requested a review from ofek as a code owner September 25, 2026 04:35
@gaborbernat gaborbernat added the bug Something isn't working label Sep 25, 2026
@gaborbernat
gaborbernat merged commit 4440e9f into tox-dev:main Sep 25, 2026
36 checks passed
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 25, 2026
4.11.13 (tox-dev#560) made ensure_exists create every Unix media directory,
platform defaults such as ~/Templates and ~/Projects included. On a
headless box that fills the home directory with folders the user never
made. Without HOME it creates a literal ~ directory in the working
directory. A configured directory that is a dangling symlink to an
unmounted drive made the getter raise FileExistsError.

tox-dev#558 asked for the directory from XDG_DOCUMENTS_DIR to exist after
access. A configured XDG path tells platformdirs where the user wants
the directory, so it keeps creating those, whether they come from the
environment or from user-dirs.dirs, on Unix and macOS alike. Defaults
stay untouched, and a dangling link comes back unchanged as it did
before 4.11.13.

The ensure_exists docs now list which directories it creates.
@gaborbernat
gaborbernat deleted the fix/ensure-exists-media-dirs branch September 25, 2026 16:48
darrenhuai added a commit to darrenhuai/platformdirs that referenced this pull request Oct 28, 2026
Since tox-dev#560 the user_documents_dir property and the other eight media properties create a directory the user configured through an XDG_*_DIR variable or user-dirs.dirs when ensure_exists is set. The module-level functions for them still took no arguments, so a caller of user_documents_path() had no way to ask for that and got a path that may not exist, while user_data_path(ensure_exists=True) right next to it did create its directory.

Give the nine user_*_dir and user_*_path media functions a keyword-only ensure_exists, the way user_bin_dir got use_site_for_root in tox-dev#537. user_fonts_dir stays as it is: the property never creates the fonts directory, so there is nothing to forward.

The new test sets each XDG media variable and checks on disk that the directory appears only when the function is asked for it, so a signature that accepts the argument without passing it on fails too.
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.

2 participants