fix(xpip): patch in user site packages for xpip where needed (#5238)

This commit is contained in:
Gil Forsyth 2023-12-05 10:46:49 -05:00 committed by GitHub
parent f4e979a2f3
commit 7a5d7c2886
Failed to generate hash of commit
2 changed files with 40 additions and 0 deletions

View file

@ -0,0 +1,24 @@
**Added:**
* <news item>
**Changed:**
* <news item>
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* ``xonsh`` now adds the user site packages directory to ``sys.path`` where
required for proper ``xontrib`` discovery
**Security:**
* <news item>

View file

@ -3,6 +3,7 @@ import contextlib
import importlib
import importlib.util
import json
import os
import sys
import typing as tp
from enum import IntEnum
@ -81,8 +82,22 @@ def get_xontribs() -> dict[str, Xontrib]:
return dict(_get_installed_xontribs())
def _patch_in_userdir():
"""
Patch in user site packages directory.
If xonsh is installed in non-writeable location, then xontribs will end up
there, so we make them accessible."""
if not os.access(os.path.dirname(sys.executable), os.W_OK):
from site import getusersitepackages
if (user_site_packages := getusersitepackages()) not in set(sys.path):
sys.path.append(user_site_packages)
def _get_installed_xontribs(pkg_name="xontrib"):
"""List all core packages + newly installed xontribs"""
_patch_in_userdir()
spec = importlib.util.find_spec(pkg_name)
def iter_paths():
@ -111,6 +126,7 @@ def _get_installed_xontribs(pkg_name="xontrib"):
def find_xontrib(name, full_module=False):
"""Finds a xontribution from its name."""
_patch_in_userdir()
# here the order is important. We try to run the correct cases first and then later trial cases
# that will likely fail