From 1d28e850affd8de651207355b80618c0e8e2ac5a Mon Sep 17 00:00:00 2001 From: Kadir Can Ozden <101993364+bysiber@users.noreply.github.com> Date: Fri, 20 Feb 2026 16:25:26 +0300 Subject: [PATCH 1/3] Respect XDG_CONFIG_HOME in _get_user_dirs_folder The _get_user_dirs_folder function hardcodes ~/.config as the location for user-dirs.dirs, ignoring XDG_CONFIG_HOME. According to the XDG Base Directory Specification, XDG_CONFIG_HOME should be checked first, falling back to ~/.config only when unset or empty. This is particularly relevant for users who have a custom XDG_CONFIG_HOME set, as the function would read the wrong user-dirs.dirs file (or fail to find it). --- src/platformdirs/unix.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/platformdirs/unix.py b/src/platformdirs/unix.py index 53cba33..0638700 100644 --- a/src/platformdirs/unix.py +++ b/src/platformdirs/unix.py @@ -272,7 +272,8 @@ def _get_user_dirs_folder(key: str) -> str | None: See https://freedesktop.org/wiki/Software/xdg-user-dirs/. """ - user_dirs_config_path = Path(os.path.expanduser("~/.config")) / "user-dirs.dirs" # noqa: PTH111 + config_home = os.environ.get("XDG_CONFIG_HOME", "").strip() or os.path.expanduser("~/.config") # noqa: PTH111 + user_dirs_config_path = Path(config_home) / "user-dirs.dirs" if user_dirs_config_path.exists(): parser = ConfigParser() From a669fec2f40d10d2aadb10cb0fb06dadc04c13da Mon Sep 17 00:00:00 2001 From: Kadir Can Ozden <101993364+bysiber@users.noreply.github.com> Date: Sat, 21 Feb 2026 21:50:21 +0300 Subject: [PATCH 2/3] Add test for XDG_CONFIG_HOME in _get_user_dirs_folder Verify that user-dirs.dirs is read from a custom XDG_CONFIG_HOME directory instead of always using ~/.config. --- tests/test_unix.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_unix.py b/tests/test_unix.py index 10c8246..43e5dfc 100644 --- a/tests/test_unix.py +++ b/tests/test_unix.py @@ -342,6 +342,20 @@ def test_user_media_dir_no_user_dirs_file( assert Unix().user_documents_dir == "/nonexistent/path/Documents" +def test_user_dirs_respects_xdg_config_home( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.delenv("XDG_DOCUMENTS_DIR", raising=False) + custom_config = tmp_path / "custom_config" + custom_config.mkdir() + user_dirs_file = custom_config / "user-dirs.dirs" + user_dirs_file.write_text('XDG_DOCUMENTS_DIR="$HOME/CustomDocs"\n') + monkeypatch.setenv("HOME", str(tmp_path)) + monkeypatch.setenv("USERPROFILE", str(tmp_path)) + monkeypatch.setenv("XDG_CONFIG_HOME", str(custom_config)) + assert Unix().user_documents_dir == f"{tmp_path}/CustomDocs" + + _SITE_REDIRECT_CASES: list[tuple[str, str]] = [ ("user_data_dir", os.path.join("/usr/local/share", "foo")), # noqa: PTH118 ("user_config_dir", os.path.join("/etc/xdg", "foo")), # noqa: PTH118 From c440414e5505f7e132840e721f9ebccb982802a7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 21 Feb 2026 18:51:48 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_unix.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_unix.py b/tests/test_unix.py index 43e5dfc..420b7a7 100644 --- a/tests/test_unix.py +++ b/tests/test_unix.py @@ -342,9 +342,7 @@ def test_user_media_dir_no_user_dirs_file( assert Unix().user_documents_dir == "/nonexistent/path/Documents" -def test_user_dirs_respects_xdg_config_home( - tmp_path: Path, monkeypatch: pytest.MonkeyPatch -) -> None: +def test_user_dirs_respects_xdg_config_home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.delenv("XDG_DOCUMENTS_DIR", raising=False) custom_config = tmp_path / "custom_config" custom_config.mkdir()