Skip to content

Commit

Permalink
Merge pull request #3151 from hexagonrecursion/fix-editable
Browse files Browse the repository at this point in the history
Fix editable --user installs with build isolation
  • Loading branch information
jaraco authored Apr 3, 2022
2 parents b58bdca + c2f4907 commit fd632df
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
1 change: 1 addition & 0 deletions changelog.d/3151.breaking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Made ``setup.py develop --user`` install to the user site packages directory even if it is disabled in the current interpreter.
18 changes: 6 additions & 12 deletions setuptools/command/easy_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,8 @@ def initialize_options(self):
self.install_data = None
self.install_base = None
self.install_platbase = None
if site.ENABLE_USER_SITE:
self.install_userbase = site.USER_BASE
self.install_usersite = site.USER_SITE
else:
self.install_userbase = None
self.install_usersite = None
self.install_userbase = site.USER_BASE
self.install_usersite = site.USER_SITE
self.no_find_links = None

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

if site.ENABLE_USER_SITE:
self.config_vars['userbase'] = self.install_userbase
self.config_vars['usersite'] = self.install_usersite

elif self.user:
self.config_vars['userbase'] = self.install_userbase
self.config_vars['usersite'] = self.install_usersite
if self.user and not site.ENABLE_USER_SITE:
log.warn("WARNING: The user site-packages directory is disabled.")

self._fix_install_dir_for_user_site()
Expand Down Expand Up @@ -375,7 +369,7 @@ def _fix_install_dir_for_user_site(self):
"""
Fix the install_dir if "--user" was used.
"""
if not self.user or not site.ENABLE_USER_SITE:
if not self.user:
return

self.create_home_path()
Expand Down
43 changes: 43 additions & 0 deletions setuptools/tests/test_easy_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import pathlib
import warnings
from collections import namedtuple
from pathlib import Path

import pytest
from jaraco import path
Expand Down Expand Up @@ -1166,3 +1167,45 @@ def test_use_correct_python_version_string(tmpdir, tmpdir_cwd, monkeypatch):
assert cmd.config_vars['py_version'] == '3.10.1'
assert cmd.config_vars['py_version_short'] == '3.10'
assert cmd.config_vars['py_version_nodot'] == '310'


def test_editable_user_and_build_isolation(setup_context, monkeypatch, tmp_path):
''' `setup.py develop` should honor `--user` even under build isolation'''

# == Arrange ==
# Pretend that build isolation was enabled
# e.g pip sets the environment varible PYTHONNOUSERSITE=1
monkeypatch.setattr('site.ENABLE_USER_SITE', False)

# Patching $HOME for 2 reasons:
# 1. setuptools/command/easy_install.py:create_home_path
# tries creating directories in $HOME
# 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
# 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
# 2. We are going to force `site` to update site.USER_BASE and site.USER_SITE
# To point inside our new home
monkeypatch.setenv('HOME', str(tmp_path / 'home'))
monkeypatch.setattr('site.USER_BASE', None)
monkeypatch.setattr('site.USER_SITE', None)
user_site = Path(site.getusersitepackages())
user_site.mkdir(parents=True, exist_ok=True)

sys_prefix = (tmp_path / 'sys_prefix')
sys_prefix.mkdir(parents=True, exist_ok=True)
monkeypatch.setattr('sys.prefix', str(sys_prefix))

# == Sanity check ==
assert list(sys_prefix.glob("*")) == []
assert list(user_site.glob("*")) == []

# == Act ==
run_setup('setup.py', ['develop', '--user'])

# == Assert ==
# Should not install to sys.prefix
assert list(sys_prefix.glob("*")) == []
# Should install to user site
installed = {f.name for f in user_site.glob("*")}
# sometimes easy-install.pth is created and sometimes not
installed = installed - {"easy-install.pth"}
assert installed == {'UNKNOWN.egg-link'}

0 comments on commit fd632df

Please sign in to comment.