Skip to content

✨ feat(api): add use_site_for_root parameter#426

Merged
gaborbernat merged 2 commits intotox-dev:mainfrom
gaborbernat:90
Feb 13, 2026
Merged

✨ feat(api): add use_site_for_root parameter#426
gaborbernat merged 2 commits intotox-dev:mainfrom
gaborbernat:90

Conversation

@gaborbernat
Copy link
Copy Markdown
Member

@gaborbernat gaborbernat commented Feb 13, 2026

System daemons, package managers, and installers running as root currently get user directories under /root/.local/..., which is rarely the intended behavior. Callers have to manually detect uid 0 and switch to site_*_dir themselves, duplicating logic that belongs in the library. Closes #213.

✨ The new use_site_for_root constructor parameter (defaulting to False) redirects all six user_*_dir properties to their site_*_dir equivalents when the process is running as root (uid 0) on Unix. The redirect bypasses XDG user environment variables like XDG_DATA_HOME entirely, since those are typically inherited from the calling user via sudo and would defeat the purpose of writing to system-wide paths. The parameter follows the same cross-platform pattern as roaming — accepted everywhere, but only meaningful on Unix.

Media directories (user_documents_dir, user_downloads_dir, etc.) are not redirected because no site_* counterpart exists for them. The parameter is also exposed on all 12 affected standalone functions (user_data_dir, user_data_path, etc.) for convenience. 🔧 Default behavior is completely unchanged — the feature is strictly opt-in.

System daemons and installers running as root should write to system-wide
directories (/var/cache, /etc/xdg, etc.) rather than /root/.local/...,
but platformdirs had no built-in way to handle this.

The new opt-in use_site_for_root parameter redirects user_*_dir calls to
their site_*_dir equivalents when running as uid 0 on Unix. XDG user
environment variables (XDG_DATA_HOME, etc.) are bypassed during the
redirect since they are typically inherited from the calling user via
sudo. The parameter is accepted on all platforms but is a no-op outside
Unix, following the same pattern as roaming on non-Windows.
@gaborbernat gaborbernat requested a review from ofek as a code owner February 13, 2026 23:30
@gaborbernat gaborbernat changed the title ✨ feat(api): add use_site_for_root parameter (#213) ✨ feat(api): add use_site_for_root parameter Feb 13, 2026
@gaborbernat gaborbernat enabled auto-merge (squash) February 13, 2026 23:32
@gaborbernat gaborbernat merged commit 2b934a7 into tox-dev:main Feb 13, 2026
29 checks passed
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Feb 14, 2026
Merges changes from upstream/main including:
- use_site_for_root parameter (tox-dev#426)
- site_state_dir and site_log_dir APIs (tox-dev#424, tox-dev#425)
- user_bin_dir and user_applications_dir APIs (tox-dev#430, tox-dev#432)
- Windows environment variable overrides (tox-dev#427, tox-dev#428)
- Various bug fixes and improvements

Adds missing documentation for use_site_for_root parameter in the
Usage Guide, addressing issue tox-dev#433.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Feb 14, 2026
Issue tox-dev#433 reported that the use_site_for_root parameter was missing
from the Usage Guide despite being available in the API. This parameter
was added in tox-dev#426 but documentation was not included in the usage guide.

Added a new section documenting use_site_for_root alongside the other
parameters. This helps users discover the feature when they need to
configure system services running as root to use system-wide directories
instead of root's home directory.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Feb 14, 2026
The use_site_for_root parameter was added in tox-dev#426 but wasn't documented
in the Usage Guide. Users had no way to discover this feature when
configuring system services running as root that need to use system-wide
directories instead of root's home directory.

Added a dedicated section documenting use_site_for_root alongside other
parameters. The documentation explains the Unix-only behavior, the default
value for backwards compatibility, and includes a practical example showing
how root processes redirect to site directories.

Fixes tox-dev#433
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Feb 14, 2026
The use_site_for_root parameter was added in tox-dev#426 but wasn't documented
in the Usage Guide. Users had no way to discover this feature when
configuring system services running as root that need to use system-wide
directories instead of root's home directory.

Added a dedicated section documenting use_site_for_root alongside other
parameters. The documentation explains the Unix-only behavior, the default
value for backwards compatibility, and includes a practical example showing
how root processes redirect to site directories.

Fixes tox-dev#433
gaborbernat added a commit that referenced this pull request Feb 14, 2026
The `use_site_for_root` parameter was added in #426 but wasn't
documented in the Usage Guide. 📚 Users had no way to discover this
feature when configuring system services running as root that need to
use system-wide directories instead of root's home directory.

Added a dedicated section in the Usage Guide documenting
`use_site_for_root` alongside other parameters like `ensure_exists`,
`multipath`, and `roaming`. The documentation explains the Unix-only
behavior, the default value for backwards compatibility, and includes a
practical example showing how root processes redirect to site
directories.

Fixes #433
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.

Optionally redirect user_*_dir() calls to site_*_dir() for root on Unix

1 participant