Skip to content

Commit fd632df

Browse files
authored
Merge pull request #3151 from hexagonrecursion/fix-editable
Fix editable --user installs with build isolation
2 parents b58bdca + c2f4907 commit fd632df

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

changelog.d/3151.breaking.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Made ``setup.py develop --user`` install to the user site packages directory even if it is disabled in the current interpreter.

setuptools/command/easy_install.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,8 @@ def initialize_options(self):
169169
self.install_data = None
170170
self.install_base = None
171171
self.install_platbase = None
172-
if site.ENABLE_USER_SITE:
173-
self.install_userbase = site.USER_BASE
174-
self.install_usersite = site.USER_SITE
175-
else:
176-
self.install_userbase = None
177-
self.install_usersite = None
172+
self.install_userbase = site.USER_BASE
173+
self.install_usersite = site.USER_SITE
178174
self.no_find_links = None
179175

180176
# Options not specifiable via command line
@@ -253,11 +249,9 @@ def finalize_options(self): # noqa: C901 # is too complex (25) # FIXME
253249
getattr(sys, 'windir', '').replace('.', ''),
254250
)
255251

256-
if site.ENABLE_USER_SITE:
257-
self.config_vars['userbase'] = self.install_userbase
258-
self.config_vars['usersite'] = self.install_usersite
259-
260-
elif self.user:
252+
self.config_vars['userbase'] = self.install_userbase
253+
self.config_vars['usersite'] = self.install_usersite
254+
if self.user and not site.ENABLE_USER_SITE:
261255
log.warn("WARNING: The user site-packages directory is disabled.")
262256

263257
self._fix_install_dir_for_user_site()
@@ -375,7 +369,7 @@ def _fix_install_dir_for_user_site(self):
375369
"""
376370
Fix the install_dir if "--user" was used.
377371
"""
378-
if not self.user or not site.ENABLE_USER_SITE:
372+
if not self.user:
379373
return
380374

381375
self.create_home_path()

setuptools/tests/test_easy_install.py

+43
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import pathlib
2020
import warnings
2121
from collections import namedtuple
22+
from pathlib import Path
2223

2324
import pytest
2425
from jaraco import path
@@ -1166,3 +1167,45 @@ def test_use_correct_python_version_string(tmpdir, tmpdir_cwd, monkeypatch):
11661167
assert cmd.config_vars['py_version'] == '3.10.1'
11671168
assert cmd.config_vars['py_version_short'] == '3.10'
11681169
assert cmd.config_vars['py_version_nodot'] == '310'
1170+
1171+
1172+
def test_editable_user_and_build_isolation(setup_context, monkeypatch, tmp_path):
1173+
''' `setup.py develop` should honor `--user` even under build isolation'''
1174+
1175+
# == Arrange ==
1176+
# Pretend that build isolation was enabled
1177+
# e.g pip sets the environment varible PYTHONNOUSERSITE=1
1178+
monkeypatch.setattr('site.ENABLE_USER_SITE', False)
1179+
1180+
# Patching $HOME for 2 reasons:
1181+
# 1. setuptools/command/easy_install.py:create_home_path
1182+
# tries creating directories in $HOME
1183+
# given `self.config_vars['DESTDIRS'] = "/home/user/.pyenv/versions/3.9.10 /home/user/.pyenv/versions/3.9.10/lib /home/user/.pyenv/versions/3.9.10/lib/python3.9 /home/user/.pyenv/versions/3.9.10/lib/python3.9/lib-dynload"`` # noqa: E501
1184+
# it will `makedirs("/home/user/.pyenv/versions/3.9.10 /home/user/.pyenv/versions/3.9.10/lib /home/user/.pyenv/versions/3.9.10/lib/python3.9 /home/user/.pyenv/versions/3.9.10/lib/python3.9/lib-dynload")`` # noqa: E501
1185+
# 2. We are going to force `site` to update site.USER_BASE and site.USER_SITE
1186+
# To point inside our new home
1187+
monkeypatch.setenv('HOME', str(tmp_path / 'home'))
1188+
monkeypatch.setattr('site.USER_BASE', None)
1189+
monkeypatch.setattr('site.USER_SITE', None)
1190+
user_site = Path(site.getusersitepackages())
1191+
user_site.mkdir(parents=True, exist_ok=True)
1192+
1193+
sys_prefix = (tmp_path / 'sys_prefix')
1194+
sys_prefix.mkdir(parents=True, exist_ok=True)
1195+
monkeypatch.setattr('sys.prefix', str(sys_prefix))
1196+
1197+
# == Sanity check ==
1198+
assert list(sys_prefix.glob("*")) == []
1199+
assert list(user_site.glob("*")) == []
1200+
1201+
# == Act ==
1202+
run_setup('setup.py', ['develop', '--user'])
1203+
1204+
# == Assert ==
1205+
# Should not install to sys.prefix
1206+
assert list(sys_prefix.glob("*")) == []
1207+
# Should install to user site
1208+
installed = {f.name for f in user_site.glob("*")}
1209+
# sometimes easy-install.pth is created and sometimes not
1210+
installed = installed - {"easy-install.pth"}
1211+
assert installed == {'UNKNOWN.egg-link'}

0 commit comments

Comments
 (0)