Skip to content

🐛 fix(dirs): refuse to create a literal ~ dir without a home - #578

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/tilde-home
Sep 25, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/tilde-home

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

With HOME unset and no passwd entry for the uid, os.path.expanduser returns ~/... unchanged. Under ensure_exists the Unix and macOS app directories then created a literal ~ tree, such as ./~/.local/share/app, in the working directory. #561 stopped creating the media defaults. A user-dirs.dirs entry of $HOME/Documents resolved to ~/Documents and hit the same bug. 🏠

Under ensure_exists platformdirs raises RuntimeError for a path that starts with ~, the error pathlib.Path.expanduser raises in the same situation and the Windows user_publicshare_dir raises for an unknown home (#546). A caller that sets ensure_exists asks for a directory to write to. Without a home any path is a guess, and a temporary directory would lose the data on reboot. App directories and configured media directories share one check, and absolute site directories pass it.

Reads without ensure_exists return the ~ path as before, so callers that compute a path and create nothing see no change. docs/parameters.rst describes the behavior.

@gaborbernat
gaborbernat requested a review from ofek as a code owner September 25, 2026 14:33
@gaborbernat gaborbernat added the bug Something isn't working label Sep 25, 2026
@gaborbernat
gaborbernat force-pushed the fix/tilde-home branch 5 times, most recently from 0e96dc0 to 4c46ecc Compare September 25, 2026 15:53
When HOME is unset and the uid has no passwd entry, os.path.expanduser
returns "~/..." unchanged. With ensure_exists the Unix and macOS app
directories then created ./~/.local/share/app and similar trees in the
working directory. Media directories had the same bug. tox-dev#561 stopped
creating their defaults, yet a user-dirs.dirs entry of "$HOME/Documents"
resolved to ~/Documents, and ensure_exists made it.

Raise RuntimeError from the shared ensure_exists step when the path
starts with "~", as pathlib.Path.expanduser does. Route media creation
through that step. Reads return the "~" path as before.
@gaborbernat
gaborbernat merged commit 4ac34b8 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
With HOME set to an empty string, os.path.expanduser puts the path
under /, so Unix returned /.config/app and macOS returned
/Library/Application Support/app, the shared site directory. With HOME
unset and no passwd entry for the uid, it returns ~/... unchanged and
the properties handed back relative paths such as ~/.config/app. tox-dev#578
guarded ensure_exists only.

Rust's std::env::home_dir treats an empty HOME as unset, and
platformdirs now does the same, so the password database supplies the
home. When neither gives one, each Unix and macOS directory under the
home raises RuntimeError on read, where Go's os.UserHomeDir also returns
an error. Site directories and absolute XDG values keep resolving. One
helper expands ~ for both platforms; the tox-dev#578 check under ensure_exists
has nothing left to catch and goes away.
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 26, 2026
With HOME set to an empty string, os.path.expanduser puts the path
under /, so Unix returned /.config/app and macOS returned
/Library/Application Support/app, the shared site directory. With HOME
unset and no passwd entry for the uid, it returns ~/... unchanged and
the properties handed back relative paths such as ~/.config/app. tox-dev#578
guarded ensure_exists only.

Rust's std::env::home_dir treats an empty HOME as unset, and
platformdirs now does the same, so the password database supplies the
home. When neither gives one, each Unix and macOS directory under the
home raises RuntimeError on read, where Go's os.UserHomeDir also returns
an error. Site directories and absolute XDG values keep resolving. One
helper expands ~ for both platforms; the tox-dev#578 check under ensure_exists
has nothing left to catch and goes away.
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 26, 2026
With HOME set to an empty string, os.path.expanduser puts the path under
/, so Unix returned /.config/app and macOS returned /Library/Application
Support/app, the shared site directory. With HOME unset and no passwd
entry for the uid, it returns ~/... unchanged and the properties handed
back relative paths such as ~/.config/app. tox-dev#578 guarded ensure_exists
only.

Rust's std::env::home_dir treats an empty HOME as unset, and
platformdirs does the same, so the password database supplies the home.
When neither gives one, each Unix and macOS directory under the home
raises RuntimeError on read, where Go's os.UserHomeDir also returns an
error. Site directories and absolute XDG values keep resolving. One
helper expands ~ for both platforms; the tox-dev#578 check under ensure_exists
has nothing left to catch and goes away.
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 26, 2026
With HOME set to an empty string, os.path.expanduser puts the path under
/, so Unix returned /.config/app and macOS returned /Library/Application
Support/app, the shared site directory. With HOME unset and no passwd
entry for the uid, it returns ~/... unchanged and the properties handed
back relative paths such as ~/.config/app. tox-dev#578 guarded ensure_exists
only.

Rust's std::env::home_dir treats an empty HOME as unset, and
platformdirs does the same, so the password database supplies the home.
When neither gives one, each Unix and macOS directory under the home
raises RuntimeError on read, where Go's os.UserHomeDir also returns an
error. Site directories and absolute XDG values keep resolving. One
helper expands ~ for both platforms; the tox-dev#578 check under ensure_exists
has nothing left to catch and goes away.
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 26, 2026
With HOME set to an empty string, os.path.expanduser puts the path under
/, so Unix returned /.config/app and macOS returned /Library/Application
Support/app, the shared site directory. With HOME unset and no passwd
entry for the uid, it returns ~/... unchanged and the properties handed
back relative paths such as ~/.config/app. tox-dev#578 guarded ensure_exists
only.

Rust's std::env::home_dir treats an empty HOME as unset, and
platformdirs does the same, so the password database supplies the home.
When neither gives one, each Unix and macOS directory under the home
raises RuntimeError on read, where Go's os.UserHomeDir also returns an
error. Site directories and absolute XDG values keep resolving. One
helper expands ~ for both platforms; the tox-dev#578 check under ensure_exists
stays for iOS, which still calls os.path.expanduser.
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 26, 2026
With HOME set to an empty string, os.path.expanduser puts the path under
/, so Unix returned /.config/app and macOS returned /Library/Application
Support/app, the shared site directory. With HOME unset and no passwd
entry for the uid, it returns ~/... unchanged and the properties handed
back relative paths such as ~/.config/app. tox-dev#578 guarded ensure_exists
only.

Rust's std::env::home_dir treats an empty HOME as unset, and
platformdirs does the same, so the password database supplies the home.
When neither gives one, each Unix and macOS directory under the home
raises RuntimeError on read, where Go's os.UserHomeDir also returns an
error. Site directories and absolute XDG values keep resolving. One
helper expands ~ for both platforms; the tox-dev#578 check under ensure_exists
stays for iOS, which still calls os.path.expanduser.
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 26, 2026
With HOME set to an empty string, os.path.expanduser puts the path under
/, so Unix returned /.config/app and macOS returned /Library/Application
Support/app, the shared site directory. With HOME unset and no passwd
entry for the uid, it returns ~/... unchanged and the properties handed
back relative paths such as ~/.config/app. tox-dev#578 guarded ensure_exists
only.

Rust's std::env::home_dir treats an empty HOME as unset, and
platformdirs does the same, so the password database supplies the home.
When neither gives one, each Unix and macOS directory under the home
raises RuntimeError on read, where Go's os.UserHomeDir also returns an
error. Site directories and absolute XDG values keep resolving. One
helper expands ~ for both platforms; the tox-dev#578 check under ensure_exists
stays for iOS, which calls os.path.expanduser directly.
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 26, 2026
With HOME set to an empty string, os.path.expanduser puts the path under
/, so Unix returned /.config/app and macOS returned /Library/Application
Support/app, the shared site directory. With HOME unset and no passwd
entry for the uid, it returns ~/... unchanged and the properties handed
back relative paths such as ~/.config/app. tox-dev#578 guarded ensure_exists
only.

Rust's std::env::home_dir treats an empty HOME as unset, and
platformdirs does the same, so the password database supplies the home.
When neither gives one, each Unix and macOS directory under the home
raises RuntimeError on read, where Go's os.UserHomeDir also returns an
error. Site directories and absolute XDG values keep resolving. One
helper expands ~ for both platforms; the tox-dev#578 check under ensure_exists
stays for iOS, which calls os.path.expanduser itself.
@gaborbernat
gaborbernat deleted the fix/tilde-home 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