Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix editable --user installs with build isolation #3151

Merged
merged 7 commits into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -373,7 +367,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 @@ -1109,3 +1110,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'}