Skip to content

Commit 16d16c8

Browse files
authored
Merge pull request #10585 from pradyunsg/fix-config-paths
Rework MacOS configuration paths, to match documentation
2 parents ee9ac15 + 0819400 commit 16d16c8

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

docs/html/topics/configuration.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ complexity for backwards compatibility reasons.
2929
```{tab} Unix
3030
3131
Global
32-
: {file}`/etc/pip.conf`
32+
: In a "pip" subdirectory of any of the paths set in the environment variable
33+
`XDG_CONFIG_DIRS` (if it exists), for example {file}`/etc/xdg/pip/pip.conf`.
3334
34-
Alternatively, it may be in a "pip" subdirectory of any of the paths set
35-
in the environment variable `XDG_CONFIG_DIRS` (if it exists), for
36-
example {file}`/etc/xdg/pip/pip.conf`.
35+
This will be followed by loading {file}`/etc/pip.conf`.
3736
3837
User
3938
: {file}`$HOME/.config/pip/pip.conf`, which respects the `XDG_CONFIG_HOME` environment variable.

news/10585.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Restore compatibility of where configuration files are loaded from on MacOS (back to ``Library/Application Support/pip``, instead of ``Preferences/pip``).

src/pip/_internal/utils/appdirs.py

+25-13
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,36 @@ def user_cache_dir(appname: str) -> str:
1717
return _appdirs.user_cache_dir(appname, appauthor=False)
1818

1919

20+
def _macos_user_config_dir(appname: str, roaming: bool = True) -> str:
21+
# Use ~/Application Support/pip, if the directory exists.
22+
path = _appdirs.user_data_dir(appname, appauthor=False, roaming=roaming)
23+
if os.path.isdir(path):
24+
return path
25+
26+
# Use a Linux-like ~/.config/pip, by default.
27+
linux_like_path = "~/.config/"
28+
if appname:
29+
linux_like_path = os.path.join(linux_like_path, appname)
30+
31+
return os.path.expanduser(linux_like_path)
32+
33+
2034
def user_config_dir(appname: str, roaming: bool = True) -> str:
21-
path = _appdirs.user_config_dir(appname, appauthor=False, roaming=roaming)
22-
if sys.platform == "darwin" and not os.path.isdir(path):
23-
path = os.path.expanduser("~/.config/")
24-
if appname:
25-
path = os.path.join(path, appname)
26-
return path
35+
if sys.platform == "darwin":
36+
return _macos_user_config_dir(appname, roaming)
37+
38+
return _appdirs.user_config_dir(appname, appauthor=False, roaming=roaming)
2739

2840

2941
# for the discussion regarding site_config_dir locations
3042
# see <https://github.com/pypa/pip/issues/1733>
3143
def site_config_dirs(appname: str) -> List[str]:
32-
dirval = _appdirs.site_config_dir(appname, appauthor=False, multipath=True)
3344
if sys.platform == "darwin":
34-
# always look in /Library/Application Support/pip as well
35-
return dirval.split(os.pathsep) + ["/Library/Application Support/pip"]
36-
elif sys.platform == "win32":
45+
return [_appdirs.site_data_dir(appname, appauthor=False, multipath=True)]
46+
47+
dirval = _appdirs.site_config_dir(appname, appauthor=False, multipath=True)
48+
if sys.platform == "win32":
3749
return [dirval]
38-
else:
39-
# always look in /etc directly as well
40-
return dirval.split(os.pathsep) + ["/etc"]
50+
51+
# Unix-y system. Look in /etc as well.
52+
return dirval.split(os.pathsep) + ["/etc"]

tests/unit/test_appdirs.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,17 @@ def test_site_config_dirs_osx(self, monkeypatch: pytest.MonkeyPatch) -> None:
9999
monkeypatch.setenv("HOME", "/home/test")
100100

101101
assert appdirs.site_config_dirs("pip") == [
102-
"/Library/Preferences/pip",
103102
"/Library/Application Support/pip",
104103
]
105104

106105
@pytest.mark.skipif(sys.platform != "linux", reason="Linux-only test")
107106
def test_site_config_dirs_linux(self, monkeypatch: pytest.MonkeyPatch) -> None:
108107
monkeypatch.delenv("XDG_CONFIG_DIRS", raising=False)
109108

110-
assert appdirs.site_config_dirs("pip") == ["/etc/xdg/pip", "/etc"]
109+
assert appdirs.site_config_dirs("pip") == [
110+
"/etc/xdg/pip",
111+
"/etc",
112+
]
111113

112114
@pytest.mark.skipif(sys.platform != "linux", reason="Linux-only test")
113115
def test_site_config_dirs_linux_override(
@@ -129,7 +131,10 @@ def test_site_config_dirs_linux_empty(
129131
) -> None:
130132
monkeypatch.setattr(os, "pathsep", ":")
131133
monkeypatch.setenv("XDG_CONFIG_DIRS", "")
132-
assert appdirs.site_config_dirs("pip") == ["/etc/xdg/pip", "/etc"]
134+
assert appdirs.site_config_dirs("pip") == [
135+
"/etc/xdg/pip",
136+
"/etc",
137+
]
133138

134139

135140
class TestUserConfigDir:

0 commit comments

Comments
 (0)